These are chat archives for ramda/ramda

30th
Jun 2016
David Langford
@That-David-Guy
Jun 30 2016 01:00
Is there a method, or way to use defaultTo that accepts functions instead of values?
This message was deleted
@ram-bot
const obj = { odds: 1 }

defaultTo(prop('odds'), prop('updatedOdds'))(obj)
// Returns 'undefined' but I want it to return 1
// I want defaultTo to accept functions instead of values?
ram-bot
@ram-bot
Jun 30 2016 01:01
undefined
David Langford
@That-David-Guy
Jun 30 2016 01:01
My use case is, get prop value if exists, otherwise get another prop value as default
I don't want to set any values
Brad Compton (he/him)
@Bradcomp
Jun 30 2016 01:02
@ram-bot
const obj = { odds: 1 }

either(prop('updateOdds'), prop('odds'))(obj)
ram-bot
@ram-bot
Jun 30 2016 01:02
1
Brad Compton (he/him)
@Bradcomp
Jun 30 2016 01:02
You could use either!
David Langford
@That-David-Guy
Jun 30 2016 01:02
Of course!
Thank's @Bradcomp . I knew there must of been a way :)
Brad Compton (he/him)
@Bradcomp
Jun 30 2016 01:04
:+1:
You might get bad behavior if you have falsey values in your object though
In which case you could use something like R.ifElse
David Langford
@That-David-Guy
Jun 30 2016 01:07
Thanks for the heads up, this particular use case will never have boolean values
David Langford
@That-David-Guy
Jun 30 2016 01:14
@Bradcomp But if I did, would it be something like this?
@ram-bot

let obj = { odds: 1 }
obj.updatedOdds = undefined

const hasValue = R.curry((p, o) => 
  R.compose(R.not, R.isNill, R.prop(p))(obj))

var getValue = R.ifElse(
  hasValue('updatedOdds'),
  R.prop('updatedOdds'),
  R.prop('odds')
);

getValue(obj);
ram-bot
@ram-bot
Jun 30 2016 01:14
Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
David Langford
@That-David-Guy
Jun 30 2016 01:14
I get an error though, not sure why
Brad Compton (he/him)
@Bradcomp
Jun 30 2016 01:23
@ram-bot
var obj = { odds: 1 }
obj.updatedOdds = undefined

var hasValue = R.curry((p, o) => 
  R.compose(R.not, R.isNil, R.prop(p))(obj))

var getValue = R.ifElse(
  hasValue('updatedOdds'),
  R.prop('updatedOdds'),
  R.prop('odds')
);

getValue(obj);
ram-bot
@ram-bot
Jun 30 2016 01:23
1
Brad Compton (he/him)
@Bradcomp
Jun 30 2016 01:24
isNil only has one l ;)
but yes, just like that!
Although you can always replace R.compose(R.not, f) withR.complement
David Langford
@That-David-Guy
Jun 30 2016 01:40
Thanks!
Jack Jennings
@jackjennings
Jun 30 2016 04:25
What's the most efficient way to do this transformation?
foo([1, 2, 3]);
// => [1, 2, 3, -3, -2, -1]
i.e. The same sequence, reversed, with some transformation applied (in this case multiply by -1)
Jack Jennings
@jackjennings
Jun 30 2016 04:42
I can do something like concat(column, map(multiply(-1), reverse(column))) but not sure if there's a better/pointfree/faster way…?
David Langford
@That-David-Guy
Jun 30 2016 06:33
My brain must be off today. What should r.something be in the following
const list1 = [
  {id: 4, a: 'base', c: 'other'},
  {id: 2, a: 'base', c: 'other'},
  {id: 23, a: 'base', c: 'other'},
  {id: 1, a: 'base', c: 'other'}
]
const list2 = [  
  {id: 23, a: 'new23'},
  {id: 4, a: 'new4'},
  {id: 1, a: 'new1'},
  {id: 2, a: 'new2'},
]

const expected = [
  {id: 23, a: 'new23', c: 'other'},
  {id: 4, a: 'new4', c: 'other'},
  {id: 1, a: 'new1', c: 'other'},
  {id: 2, a: 'new2', c: 'other'},
]


expect(R.something(list1, list2)).toBe(expected)
I want to R.merge the object in list1 with the corresponding object in list2. Using the id as the key
I looked through the list methods and in the cookbook and couldn't find anything
David Langford
@That-David-Guy
Jun 30 2016 06:52
This is what I've got
@ram-bot
const list1 = [
  {id: 4, a: 'base', c: 'other'},
  {id: 2, a: 'base', c: 'other'},
  {id: 23, a: 'base', c: 'other'},
  {id: 1, a: 'base', c: 'other'}
]

const list2 = [  
  {id: 23, a: 'new23'},
  {id: 4, a: 'new4'},
  {id: 1, a: 'new1'},
  {id: 2, a: 'new2'},
]

const expected = [
  {id: 23, a: 'new23', c: 'other'},
  {id: 4, a: 'new4', c: 'other'},
  {id: 1, a: 'new1', c: 'other'},
  {id: 2, a: 'new2', c: 'other'},
]


// expect(R.something(list1, list2)).toBe(expected)

const findObj = (xs, key, value) => R.find(R.propEq(key, value), xs)
const mergeObj = (x, xs, key) => R.merge(x, findObj(xs, key, x[key]))
const mergeObjsInArray = (xs, ys, key) => map(x=>mergeObj(x, ys, key), xs)

mergeObjsInArray(list1, list2, 'id')
ram-bot
@ram-bot
Jun 30 2016 06:52
[ { id: 4, a: 'new4', c: 'other' },
  { id: 2, a: 'new2', c: 'other' },
  { id: 23, a: 'new23', c: 'other' },
  { id: 1, a: 'new1', c: 'other' } ]
Barney Carroll
@barneycarroll
Jun 30 2016 10:48
Hi guys, I'd like to refactor the Ramda website source
At the moment the HTML assumes Javascript (search box) and the Javascript provides a very light enhancement on the HTML
What I'd like is to have search query descriptions and sort results
So a more flexible approach would be necessary
My instinct is to reach for Mithril such that the content can be parsed once on page load, and thereafter managed more flexibly
Barney Carroll
@barneycarroll
Jun 30 2016 10:54
Any strong feelings about this? I realise dropping in a framework might not be seen as the most conscientious kind of drive-by PR
Bravi
@Bravilogy
Jun 30 2016 11:22
how can I remove an object from the array? I have a users list and I need to remove a specific user from the array
I've done R.find(R.propEq('id', user.id))(users)
But it doesn't get me anywhere :D
Barney Carroll
@barneycarroll
Jun 30 2016 12:50
@Bravilogy you want without
Bravi
@Bravilogy
Jun 30 2016 13:40
I actually solved it by this one:
R.reject(R.propEq('id', user.id))(users);
I'll look into without now
Barney Carroll
@barneycarroll
Jun 30 2016 13:40
Your solution is better
R.without would be needlessly verbose — you'd need to reference the collection and iterate through it twice
Bravi
@Bravilogy
Jun 30 2016 13:42

The only thing is that I had to create a separate function for it:

function removeUserFromList(user) {
   return R.reject(R.propEq('id', user.id));
}

and then I do:

users = removeUserFromList(user)(users);
I guess that's not too bad :D
Bravi
@Bravilogy
Jun 30 2016 14:58

I'm trying to refactor this using ramda:

return m('ul.list-inline.text-center', [
     ctrl.items.map(item => R.allPass(item.policies)(item) ? m('li', m('a', {
          href: item.uri,
          config: m.route
      }, item.name)) : null)
]);

So basically I want to filter down ctrl.items array before passing to this map function. So I tried doing this:

var filterItems = R.filter(
   R.pipe(
      R.prop('policies'),
      R.allPass
   )
);

but obviously that doesn't work. I guess I still need to use the map after all? To be able to pass item to R.allPassfunction?

LeonineKing1199
@LeonineKing1199
Jun 30 2016 16:18

So basically I want to filter down ctrl.items array before passing to this map function. So I tried doing this:

I'm not sure if this is applicable (I'm incredibly spaced out right now) but any time I see filtering and mapping, my knee-jerk reaction is "transducer" so I have to ask, would transduction be applicable in your case?

Bravi
@Bravilogy
Jun 30 2016 16:29
@LeonineKing1199 hey thanks for a reply! I'll look into it now :)
this is my current solution:
var testForPolicies = function (item) {
    return R.allPass(item.policies)(item);
}

var generateListItem = function (item) {
    return m('li', m('a', {
        href: item.uri,
        config: m.route
    }, item.name));
};

var filterAndRenderItems = R.pipe(
    R.filter(testForPolicies),
    R.map(generateListItem)
);

// and then a view in mithril is as simple as:
 view (ctrl) {
     return m('ul.list-inline.text-center', filterAndRenderItems(ctrl.items));
}
LeonineKing1199
@LeonineKing1199
Jun 30 2016 16:37
Oh yeah, if you're piping a map into a filter, I think you can for sure use a transducer.
But I could be wrong. Like I said, I'm, uh, spacy.
Bravi
@Bravilogy
Jun 30 2016 16:38
Kevin Spacey?
:D
Chet Harrison
@ChetHarrison
Jun 30 2016 17:02
does a Ramda lens operate the say way updates to an Elm record does? In other words if we were to change 1 attr of a 1000 attr object would the original and new copy share the unchanged attrs?
David Chambers
@davidchambers
Jun 30 2016 22:14
No, @ChetHarrison, R.lensProp uses R.assoc which copies.