These are chat archives for ramda/ramda

15th
Jul 2016
Andrzej Koper
@endziu
Jul 15 2016 00:26

Hi everybody. Can you help me with this problem?

I'm mapping over collection of objects and I want to set the value of one property, to be the same as the value of another property of that object.

(If artwork_url is equal to null, I want to set it to the value of userPic)

I'm almost there.

http://goo.gl/QnM8YT

David Langford
@That-David-Guy
Jul 15 2016 00:35
@ram-bot
const obj1 = {
    "id": 272034628,
    "artwork_url": "https://i1.sndcdn.com/artworks-000169997054-zvmgco-large.jpg",
    "userPic": "https://i1.sndcdn.com/avatars-000048739118-kdh3w7-large.jpg"
  }
const obj2 = {
    "id": 271784579,
    "artwork_url": null,
    "userPic": "https://i1.sndcdn.com/avatars-000048739118-kdh3w7-large.jpg"
}
const getValue = x => R.defaultTo( R.prop('userPic', x), R.prop('artwork_url', x) )
console.log({
  example1: getValue(obj1),
  example2: getValue(obj2)
})
ram-bot
@ram-bot
Jul 15 2016 00:35
console is not defined
David Langford
@That-David-Guy
Jul 15 2016 00:35
Something like that?
Sorry, that isn't working, let me tidy it up a bit
Andrzej Koper
@endziu
Jul 15 2016 00:37
:smile:
David Langford
@That-David-Guy
Jul 15 2016 00:37
@ram-bot
const obj1 = {
    "id": 272034628,
    "artwork_url": "artwork_urlValue",
    "userPic": "userPicValue"
  }
const obj2 = {
    "id": 271784579,
    "artwork_url": null,
    "userPic": "userPicValue"
}
const getValue = x => R.defaultTo( R.prop('userPic', x), R.prop('artwork_url', x) )

getValue(obj1)
getValue(obj2)
ram-bot
@ram-bot
Jul 15 2016 00:37
'userPicValue'
David Langford
@That-David-Guy
Jul 15 2016 00:38
There is a way to tidy up the defaulTo, so you don't need the x => part but I can't think of it off the top of my head sorry
Andrzej Koper
@endziu
Jul 15 2016 00:39
thx! I'll try that.
David Langford
@That-David-Guy
Jul 15 2016 00:39
That is the get part though, not the set
Michael Hurley
@buzzdecafe
Jul 15 2016 00:42
const getValue = compose(
  apply(defaultTo), 
  ap([prop('userPic'), prop('artwork_url')]), 
  of)
?
David Langford
@That-David-Guy
Jul 15 2016 00:43
The set in the map could be like this
@ram-bot
const obj1 = {
    "id": 272034628,
    "artwork_url": "artwork_urlValue",
    "userPic": "userPicValue"
  }
const obj2 = {
    "id": 271784579,
    "artwork_url": null,
    "userPic": "userPicValue"
}
const objs = [obj1, obj2]

const getValue = x => R.defaultTo( R.prop('userPic', x), R.prop('artwork_url', x) )
const updateObj = x =>
  R.merge(x,
    {
      'artwork_url': getValue(x)
    }
  )

map(updateObj, objs)
ram-bot
@ram-bot
Jul 15 2016 00:43
[ { id: 272034628,
    artwork_url: 'artwork_urlValue',
    userPic: 'userPicValue' },
  { id: 271784579,
    artwork_url: 'userPicValue',
    userPic: 'userPicValue' } ]
David Langford
@That-David-Guy
Jul 15 2016 00:43
If you are wanting to set the artwork_url to userPic if artwork_url is null
Andrzej Koper
@endziu
Jul 15 2016 00:44
;) exactly what I need. Thank You!
David Langford
@That-David-Guy
Jul 15 2016 00:45
@buzzdecafe has a better getValue than mine. He probably knows a better way to update as well. I'm still pretty new
David Langford
@That-David-Guy
Jul 15 2016 00:51
@endziu You could make it point free so its reusable
@ram-bot
const defaultObjPropValue = (defaultKey, key, obj) =>
  R.compose( R.apply(defaultTo), R.ap([R.prop(defaultKey), R.prop(key)]), R.of)(obj)
ram-bot
@ram-bot
Jul 15 2016 00:51
undefined
David Langford
@That-David-Guy
Jul 15 2016 00:52
This message was deleted
ram-bot
@ram-bot
Jul 15 2016 00:52
undefined
David Langford
@That-David-Guy
Jul 15 2016 00:52
@ram-bot
const obj2 = {
    "id": 271784579,
    "artwork_url": null,
    "userPic": "userPicValue"
}

const defaultObjPropValue = (defaultKey, key, obj) => 
  R.compose( R.apply(defaultTo), R.ap([R.prop(defaultKey), R.prop(key)]), R.of)(obj)

defaultObjPropValue('userPic', 'artwork_url', obj2)
ram-bot
@ram-bot
Jul 15 2016 00:52
'userPicValue'
David Langford
@That-David-Guy
Jul 15 2016 05:04
Is there a single method that does the same as my flattenUniqValues() below?
@ram-bot
const data = {
  'key1': ['event1', 'event2', 'event3', 'event4', 'event5'] ,
  'key2': ['event2', 'event4', 'event6'] 
}
const expected = 
 ['event1', 'event2', 'event3', 'event4', 'event5', 'event6']


const flattenUniqValues = compose(uniq, flatten, values)
flattenUniqValues(data) // == expected
ram-bot
@ram-bot
Jul 15 2016 05:04
[ 'event1', 'event2', 'event3', 'event4', 'event5', 'event6' ]
David Langford
@That-David-Guy
Jul 15 2016 05:35
New question: What is the corresponding ramda function to delete.
ie. how do I do this:
@ram-bot
const data = {
  'key1': ['event1', 'event2', 'event3', 'event4', 'event5'] ,
  'key2': ['event2', 'event4', 'event6'] 
}

// how do I do this with immutably?
delete data.key1
ram-bot
@ram-bot
Jul 15 2016 05:35
true
Aldwin Vlasblom
@Avaq
Jul 15 2016 05:52
R.dissoc
Eshan Singh
@naiveaiguy
Jul 15 2016 06:25
R.filter
David Langford
@That-David-Guy
Jul 15 2016 06:39
Works like a charm @Avaq .
@eshansingh I couldn't get filter to work because it gives me the prop values, not the prop keys
Morten Poulsen
@mspoulsen
Jul 15 2016 07:49
Hi guys....How can I flatten arrays only one level? Thanks!
Aldwin Vlasblom
@Avaq
Jul 15 2016 07:51
R.flatten
Aldwin Vlasblom
@Avaq
Jul 15 2016 07:52
R.unnest
Aldwin Vlasblom
@Avaq
Jul 15 2016 07:52
flatten arrays only one level -- @mspoulsen
  • R.flatten if you meant to one level
  • R.unnest if you meant by one level
Eshan Singh
@naiveaiguy
Jul 15 2016 07:57
@ram-bot
Morten Poulsen
@mspoulsen
Jul 15 2016 08:00
@Avaq unnest was what I was looking for - excellent! Thanks :thumbsup:
Eshan Singh
@naiveaiguy
Jul 15 2016 08:00
R.filter(x => x % 2, map(prop('num'), [{ num: 2 }, { num: 5 }]))
@ram-bot R.filter(x => x % 2, map(prop('num'), [{ num: 2 }, { num: 5 }]))
ram-bot
@ram-bot
Jul 15 2016 08:00
[ 5 ]
Eshan Singh
@naiveaiguy
Jul 15 2016 08:01
Why is this happening?
Aldwin Vlasblom
@Avaq
Jul 15 2016 08:02
@ram-bot [2, 5].map(x => x % 2)
ram-bot
@ram-bot
Jul 15 2016 08:02
[ 0, 1 ]
Aldwin Vlasblom
@Avaq
Jul 15 2016 08:02
@ram-bot [0, 1].map(Boolean)
ram-bot
@ram-bot
Jul 15 2016 08:02
[ false, true ]
Eshan Singh
@naiveaiguy
Jul 15 2016 08:02
Oh, I see. Thanks!
So then, this would work for what I'm looking for...
@ram-bot R.filter(x =>! (x % 2), map(prop('num'), [{ num: 2 }, { num: 5 }]))
ram-bot
@ram-bot
Jul 15 2016 08:04
[ 2 ]
Eshan Singh
@naiveaiguy
Jul 15 2016 08:04
Yep. Thanks again!
Aldwin Vlasblom
@Avaq
Jul 15 2016 08:05
If I were you I wouldn't do this implicit boolean conversion. Instead validate the output of the x % 2 expression to be what you want it to be: x % 2 === 0
But yeah. :)
Eshan Singh
@naiveaiguy
Jul 15 2016 08:06
@ram-bot R.filter(x => x % 2 === 0, map(prop('num'), [{ num: 2 }, { num: 5 }]))
ram-bot
@ram-bot
Jul 15 2016 08:06
[ 2 ]
Eshan Singh
@naiveaiguy
Jul 15 2016 08:07
Yeah, that works too. Actually, it's much more clear. :thumbsup:
@ram-bot console.log('This bot is pretty cool.')
ram-bot
@ram-bot
Jul 15 2016 08:09
console is not defined
Eshan Singh
@naiveaiguy
Jul 15 2016 08:09
Woops
Anyway, sorry.
Aldwin Vlasblom
@Avaq
Jul 15 2016 08:10
Haha, the irony.
But yeah, the bot is handy. :D
Jigar Gosar
@jigargosar
Jul 15 2016 08:29

quick question

var variadicJoin = unapply(join("-"))

is there an alternative function that takes all arguments and and returns an arry so that I could write

var variadicJoin = compose(join("-"), argsToArray)

or should I go ahead an implement my own?

or is there a way to write following code using compose?
_.binary(_.unapply(_.join("-")))
Martin Broder
@mrtnbroder
Jul 15 2016 10:00
Ramda, y u no have trace?
const trace = (...args) => {
  console.log(args)
  return args
}
using this all the time
Eshan Singh
@naiveaiguy
Jul 15 2016 10:01
PR it.
Martin Broder
@mrtnbroder
Jul 15 2016 10:02
well it's more or less just for internal debugging, sure it wouldnt be accepted
at least, just like that.
Eshan Singh
@naiveaiguy
Jul 15 2016 10:02
It's a useful enough idea
Martin Broder
@mrtnbroder
Jul 15 2016 10:02
but having something like import trace from 'ramda/debug/trace' would be cool
Eshan Singh
@naiveaiguy
Jul 15 2016 10:02
Needs to be curried though
Eshan Singh
@naiveaiguy
Jul 15 2016 10:33
I'm trying to do this and have no idea which curry to use.. the internal _curryN function expects number of arguments and we don't know that
I'm also having trouble seeing how we could test it
Right now, I'm just using
var curry = require('./curry.js');

module.exports = curry(function trace(args) {
    if (typeof console !== "undefined") {
        args.forEach(x => console.log(x));
    }
});
May need to replace that arrow function with ES5 stuff, as I'm not sure if you're using ES6
Eshan Singh
@naiveaiguy
Jul 15 2016 10:44
The trouble is, if R.trace is passed an array, it should log the array, not the elements of it
And it should be able to log multiple things. R.trace(1, 2) should not behave the same as R.trace([1, 2]).
This message was deleted
Eshan Singh
@naiveaiguy
Jul 15 2016 10:51
Essentially, it's a curried console.log with a check for the existence of console. Maybe compose can help here?
Eshan Singh
@naiveaiguy
Jul 15 2016 11:00
This thing semi-works:
let trace = R.compose(console.log, function(x) {
  if (typeof console === "undefined")
    return undefined;
  return x;
});
Two problems though: A) This still relies on whether console.log exists, and B) It's still not curried
Martin Broder
@mrtnbroder
Jul 15 2016 11:54
not sure what you mean with curried
This message was deleted
it behaves kinda_ like identity, it just returns _all of its arguments instead of just one + a side-effect
This message was deleted
const trace = function trace() {
  for (let _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
    args[_key] = arguments[_key];
  }

  console.log('trace', args)
  return args
}
es5 version.
Eshan Singh
@naiveaiguy
Jul 15 2016 13:37
Curried means you can call it like this: R.trace(1)(2)([3, 4])('5') //=> Logs 1 2 [3, 4] '5'
Gabe Johnson
@gabejohnson
Jul 15 2016 13:52
@eshansingh how does Ramda know to stop currying in that case?
Gabe Johnson
@gabejohnson
Jul 15 2016 14:06
@brianmd did you ever get any traction w/ your curryx idea?
Brad Compton (he/him)
@Bradcomp
Jul 15 2016 15:30
@mrtnbroder What about R.tap(console.log)?
Eshan Singh
@naiveaiguy
Jul 15 2016 15:44
R.tap
Eshan Singh
@naiveaiguy
Jul 15 2016 15:46
Looks good. Maybe I could figure out a way to curry it. @gabejohnson's concern is correct, maybe I could have some sort of indicator for 'done'. Or maybe I could just skip currying altogether.
Gabe Johnson
@gabejohnson
Jul 15 2016 15:52
@eshansingh I remember following a conversation on this channel about varaidic currying in the last month or two
it’s definitely a thing that a few people are interested in
Michael Hurley
@buzzdecafe
Jul 15 2016 23:24
variadic currrying would be a tricky problem. we considered doing something like curryWhile :: (a -> bool) -> (...a -> b) -> (...a -> b) or some such; not sure exactly how to express the sig
essentially you would get a function that would keep taking more arguments until the predicate was no longer satisfied
then it would evaluate
but there was no real call for it
Michael Hurley
@buzzdecafe
Jul 15 2016 23:29
pardon the pun