These are chat archives for ramda/ramda

7th
Mar 2017
Galileo Sanchez
@galileopy
Mar 07 2017 12:12
Between map(identity, arguments), slice(0, Infinity, arguments) and values(arguments), which one should I choose? I am concerned that perhaps values wont respect order, slice is more idiomatic but also longer, and map is there for completeness.
James Forbes
@JAForbes
Mar 07 2017 12:42
Array.from?
akilesh
@akileshv
Mar 07 2017 13:01
how to merge [{ a: 'hello', b: 'hi' }, { a:'hi', b:1 }] [{ a: 'hello', b: '1' }, { a:'hiii', b:1 }] and get result [{ a: 'hello', b: 1 }, { a:'hi', b:1 }, { a: 'hiii', b: 1 } ] when Object key 'a' duplicate found first object replace with second object value.
Galileo Sanchez
@galileopy
Mar 07 2017 13:15
@JAForbes thanks!!!
Stephan Meijer
@smeijer
Mar 07 2017 14:03
@akhileshv , I'm sure it could be done better; but to get you started:
const pre = [
  { a: 'hello', b: 'hi' },
  { a: 'hi', b: 1 },
  { a: 'hello', b: '1' },
  { a:'hiii', b:1 }
];

const upsert = (item, list, id = 'a') => {
  if (!item[id]) {
    throw new Error(`upsert requires a unique ${id}`);
  }

  const idx = R.findIndex(R.propEq(id, item[id]), list);
  const fn = idx === -1 ? R.append(item) : R.update(idx, R.merge(list[idx], item));
  return fn(list);
};

upsert ({ a: 'hello', b: 'foobar' }, pre);
Adam Szaraniec
@mimol91
Mar 07 2017 14:04
Hey, I;ve got collection of object. Each of them has status.
I need to determine if there is at least 1 object with status = 'ACTIVE'
Should I use filter and length or there is better way?
Stephan Meijer
@smeijer
Mar 07 2017 14:04
@mimol91 any ?
Adam Szaraniec
@mimol91
Mar 07 2017 14:05
oh, I m blind :D thanks
Stephan Meijer
@smeijer
Mar 07 2017 14:07
const hasActive = any(propEq('status', 'ACTIVE'));
hasActive(myCollection);
Adam Szaraniec
@mimol91
Mar 07 2017 14:07
Thanks
AFortune
@AFortune
Mar 07 2017 14:17
@akhileshv you might want to look at R.uniqWith it prefers the first element though so you might have to play around with things some.
Mick Dekkers
@mickdekkers
Mar 07 2017 15:29
is there a version of evolve that also lets you add new properties with the value based on other properties in the object?
Bravi
@Bravilogy
Mar 07 2017 16:08

hi guys

const formatParameter = (key, value) => `${key}=${encodeURIComponent(value)}`;
const encodeParams = compose(join('&'), values, mapObjIndexed(flip(formatParameter)));
const encodeUrl = useWith(concat, [when(complement(contains('?')), concat(__, '?')), encodeParams]);

any suggestions perhaps?

AFortune
@AFortune
Mar 07 2017 16:18
@Bravilogy could you show me your input and desired output?
it works, I was just wondering if there's more ramda-y version out there
actually if I could make things a bit simpler perhaps
AFortune
@AFortune
Mar 07 2017 16:33
@Bravilogy you might want to check out toPairs()
Bravi
@Bravilogy
Mar 07 2017 16:34
oh u mean to use it instead of mapObjIndexed?
and then values. hmm yeah you're right
I think I could do that
Stephan Meijer
@smeijer
Mar 07 2017 16:36
@Bravilogy, for the params, I use:
const encodePairs = map(map(encodeURIComponent));
const toParams = pipe(map(join('=')), join('&'));
const encodeParams = pipe(toPairs, encodePairs, toParams);

const params = {
    id: 1,
    topic: 'Javascript',
    chapter: 'Functional & Awesome'
};

encodeParams(params);
Bravi
@Bravilogy
Mar 07 2017 16:37
@smeijer thanks
Travis LaDuke
@laduke
Mar 07 2017 18:18
One thing that trips me up, and it mostly comes up when I'm doing Hackerrank type stuff, is stuff that involves two lists. It usually makes me revert back to imperative style. Do any of the ramda and co blogs have any examples in them. Let me try to find an example problem.
Gabe Johnson
@gabejohnson
Mar 07 2017 18:36
@laduke zipWith?
@ram-bot zipWith
ram-bot
@ram-bot
Mar 07 2017 18:36
[Function: f3]
Travis LaDuke
@laduke
Mar 07 2017 18:37
That's one of the things I was thinking about, not having a zip in plain JS
So it's common to make a zip pass as the first step?
Gabe Johnson
@gabejohnson
Mar 07 2017 18:38
I don't know if it's common, but it's the first thing I thought of
Travis LaDuke
@laduke
Mar 07 2017 18:39
If I don't, I end up referencing one array as a global inside the reduce callback of the other, which makes me uneasy.
Gabe Johnson
@gabejohnson
Mar 07 2017 18:40
I would just do multiple passes. Unless you had a reason to optimize.
Haven't used transducers in Ramda, but that might be another option
Robert Mennell
@skatcat31
Mar 07 2017 19:47

https://www.hackerrank.com/challenges/compare-the-triplets

@laduke is this the problem you're having trouble solving or are you showing it as a suggestion of a common practice that causes you to use closures?

Travis LaDuke
@laduke
Mar 07 2017 19:53
the latter. Solution is like a 4 line for loop... I thought I remembered zip being complicated to implement, but looking again it's simple
maybe it was chunk that was hard
Robert Mennell
@skatcat31
Mar 07 2017 20:01
chunk as in Arrray.slice?
Travis LaDuke
@laduke
Mar 07 2017 20:04
More like ramda groupBy
anyways
Nate Shoemaker
@nshoes
Mar 07 2017 20:17
can I pick from a nested object?
e.g. picking foo from { a: { foo: 'bar' } }
Denis Stoyanov
@xgrommx
Mar 07 2017 20:18
Nate Shoemaker
@nshoes
Mar 07 2017 20:20
right, but with an API like pick, where i can pass an array of keys?
maybe i need to do a pick and then path and then merge them.
Robert Mennell
@skatcat31
Mar 07 2017 20:21
@ram-bot R.path
ram-bot
@ram-bot
Mar 07 2017 20:21
[Function: f2]
Nate Shoemaker
@nshoes
Mar 07 2017 20:22
yeah merge will work
Robert Mennell
@skatcat31
Mar 07 2017 20:22
@ram-bot R.path(['a', 'foo'], { a: { foo: 'bar' } })
ram-bot
@ram-bot
Mar 07 2017 20:22
'bar'
Nate Shoemaker
@nshoes
Mar 07 2017 20:29
what about conditionally adding keys to an object, but only if they exist?
Nate Shoemaker
@nshoes
Mar 07 2017 20:35
i am trying to get rid of those nasties at the bottom.
export default function compressCameraForPouch(camera) {
  return mergeAll([
    pick(
      [
        'cameraHeight', 'distance', 'fovMode', 'imagerCount',
        'horizontalAngleOfView', 'horizontalResolution', 'imagerSize',
        'sceneHeight', 'tilt', 'unitOfMeasurement', 'verticalResolution', 'name',
        'active', 'id', 'location', 'heading', 'backgroundPosition', 'sceneName',
        'locked', 'streetViewActive', 'streetViewAvailable', 'streetViewUrl',
        'userUploadedSceneActive', 'userUploadedSceneFov', 'userUploadedSceneUrl'
      ],
      camera
    ),
    camera.model && { modelId: path(['camera', 'model', 'id'], camera) },
    camera.model && camera.model.aov && { modelAovId: path(['camera', 'model', 'aov', 'id'], camera) }
  ])
}
it feels wrong, but i am unsure what to do about it
AFortune
@AFortune
Mar 07 2017 21:10
@nshoes what about mergeWith?
Nate Shoemaker
@nshoes
Mar 07 2017 21:30
@AFortune nope, these keys don't exist in both objects; there is just one object here. i am finding more luck composing a filter chain.
Jonas Windey
@jonaswindey
Mar 07 2017 23:26
how could I replace this ternary condition with an ifElse? https://goo.gl/lO3k1e
tried with
const diffWhenPositive = curry((from, to) => ifElse(
  gt,
  (subtract(to, from)),
  0
)(to, 0))
but get First argument to _arity must be a non-negative integer no greater than ten
Ian Hofmann-Hicks
@evilsoft
Mar 07 2017 23:33
something like this?
const fn = (from, to) =>
  ifElse(
    flip(gt)(0),
    flip(subtract)(from),
    always(0)
  )(to)
Robert Mennell
@skatcat31
Mar 07 2017 23:35
@evilsoft darn beat me to it...
Jonas Windey
@jonaswindey
Mar 07 2017 23:35
is flip(gt)(0)the same as gt(__, 0) ?
Robert Mennell
@skatcat31
Mar 07 2017 23:37
no, but it should fill a similar role
Travis LaDuke
@laduke
Mar 07 2017 23:38
is flip(gt) the same at lt ? o_o
seems like it
Jonas Windey
@jonaswindey
Mar 07 2017 23:42
thanks a lot guys
Travis LaDuke
@laduke
Mar 07 2017 23:42
@ram-bot clamp
ram-bot
@ram-bot
Mar 07 2017 23:42
[Function: f3]
Travis LaDuke
@laduke
Mar 07 2017 23:56
const fn = R.clamp(0, undefined)

fn(1000)
heh, not sure if that's intended, but it seems to work