These are chat archives for ramda/ramda

27th
Apr 2017
Joey Figaro
@joeyfigaro
Apr 27 2017 15:33
I have an array of objects - anyone have any tips on how I might omit keys from each of them that has a value that matches x?
Just RAG
@justrag
Apr 27 2017 15:36
I'm not a pro at this, but something like: "map on array elements; reject on object elements" should probably work.
Joey Figaro
@joeyfigaro
Apr 27 2017 15:37
@justrag Thanks man. What would I provide for map on the array?
Joey Figaro
@joeyfigaro
Apr 27 2017 15:45
Perfecto. What if those objects in the array have a key with an array of objects with keys I want to remove?
Just RAG
@justrag
Apr 27 2017 15:45
Lost me.
Joey Figaro
@joeyfigaro
Apr 27 2017 15:46
[{ metrics: [{ period: 'all time' }, { .period: 'last 6 months' }] }]
[{ metrics: [{ period: 'all time' }] }]
I want to filter that to remove objects with a period key with a value of 'last 6 months'
Brad Compton (he/him)
@Bradcomp
Apr 27 2017 15:46
@joeyfigaro The nice thing about libraries like Ramda is that they are super composable. So you can start by looking at one object, and figure out how you want to transform it. Once you have that, you can compose that with another function that will let you operate on a collection of those objects, etc.
So first figure out how you want to modify your inner most structure
propEq('period', 'all time')
Then you are going to want to filter those values
filter(propEq('period', 'all time'))
Thoes exist in the metrics field, so you can use evolve to run a function over a field in an object
evolve({
    metrics: filter(propEq('period', 'all time'))
})
And all your objects exist in an array, so you can take that whole function, stick it in a map and bob's your uncle :)
Michael Zinn
@RedNifre
Apr 27 2017 15:51
...or if you like lenses you could over(metrics, filter(propEq('period', 'all time')), thing)
as in var polishLogs = map(over(metrics, filter(propEq('period', 'all time'))))
Joey Figaro
@joeyfigaro
Apr 27 2017 15:52
@RedNifre That's super clean! Thanks for chiming in. @Bradcomp That is exactly what I needed - it makes sense to begin at the innermost object. Super clean/concise - thank you!
@RedNifre :100: :100:
Michael Zinn
@RedNifre
Apr 27 2017 15:53
...where metrics = lensProp("metrics");
Joey Figaro
@joeyfigaro
Apr 27 2017 15:53
I haven't touched lenses yet
Michael Zinn
@RedNifre
Apr 27 2017 15:53
I'm glad I could help :)
Joey Figaro
@joeyfigaro
Apr 27 2017 15:53
but that's some decent motivation
Michael Zinn
@RedNifre
Apr 27 2017 15:54
Lenses are super simple in Ramda, to get started you only need to understand that you create a lens like var someProp = lensProp("someProp") and then look up how set, view and over work. That's enough to get productive with it.
Joey Figaro
@joeyfigaro
Apr 27 2017 15:55
You guys are fantastic. I can't thank you enough. :school:
Michael Zinn
@RedNifre
Apr 27 2017 16:00
If you want to see a fun example project you can checkout my ramda sokoban game here https://github.com/RedNifre/fpsokobanjs , you can play it with docker or by installing it through npm. Have a look at the game.js file :)
Just RAG
@justrag
Apr 27 2017 16:07
exports.won = R.none(R.contains(Cell.MISPLACED_CRATE));
It reminds me that Ramda's a great match for Redux selectors.
Just connect({isGameWon: won}) and you can go all {isGameWon && <p>Congratulations, you won!</p>} in your component.
Joey Figaro
@joeyfigaro
Apr 27 2017 16:11
^ ^ love that
@RedNifre Thanks for sharing Michael - I'll check it out!
{
  name: "Joey's tire shack",
  metrics: [
    {
      period: "last 6 months",
      users: 138
    },
    {
      period: "all time",
      users: 12
    }
  ]
}

...to...

{
  "Joey's Tire Shack": {
    "last 6 months": {
      users: 138
    },
    "all time": {
      users: 12
    }
 }

Which function(s) would best serve me?

Brad Compton (he/him)
@Bradcomp
Apr 27 2017 16:17
R.groupBy
Joey Figaro
@joeyfigaro
Apr 27 2017 16:17
Beautiful. :school:
Brad Compton (he/him)
@Bradcomp
Apr 27 2017 16:17
@ram-bot
groupBy(prop('period'), [
    {
      period: "last 6 months",
      users: 138
    },
    {
      period: "all time",
      users: 12
    }
  ])
ram-bot
@ram-bot
Apr 27 2017 16:17
{ 'last 6 months': [ { period: 'last 6 months', users: 138 } ],
  'all time': [ { period: 'all time', users: 12 } ] }
Brad Compton (he/him)
@Bradcomp
Apr 27 2017 16:18
If you know the keys are going to be unique you can use
R.indexBy
Joey Figaro
@joeyfigaro
Apr 27 2017 16:21
Thanks a million!
Stephan Meijer
@smeijer
Apr 27 2017 16:38

what's the easiest way to make const compact = R.reject(R.isNil) work on nested structures? So that a

const obj = {
  a: {
    b: null,
    c: [],
  }
  a2: {
    foo: 'bar'
  }
}

results in { a2: { foo: 'bar' } }

Michael Zinn
@RedNifre
Apr 27 2017 16:49
maybe filter(compose(complement(isNil), prop("foo")) ?
There might be a simpler way though.
Brad Compton (he/him)
@Bradcomp
Apr 27 2017 16:53
@smeijer You'll need recursion if you don't know the structure
Please note that due to how JS evaluates you can't make a naively recursive function point free
Stephan Meijer
@smeijer
Apr 27 2017 17:38
aah, okay. I also came up with a recursive function, but was wrestling to make it point free. Didn't realize it wasn't possible at all.
thanks though :)
Matthew Willhite
@miwillhite
Apr 27 2017 18:02
I don’t think it is possible because you need a function name in order to call it recursively, which you can’t get from inside the function…
Just RAG
@justrag
Apr 27 2017 18:16
Is there a way to use property key in reject predicate?
R.reject(somePredicateFunction, {"leavethis":1,"removethis":2})
Just RAG
@justrag
Apr 27 2017 18:29
@ram-bot const rejectObj = R.addIndex(R.reject); rejectObj((val, key) => R.equals("removeme",key), {"leaveme":1,"removeme":2,"leavemetoo":3})
ram-bot
@ram-bot
Apr 27 2017 18:29
{ leaveme: 1, removeme: 2, leavemetoo: 3 }
Gabe Johnson
@gabejohnson
Apr 27 2017 18:30
R.pipe(R.toPairs, R.reject(R.propEq(0, 'removethis')), R.fromPairs)
Just RAG
@justrag
Apr 27 2017 18:32
Oooh... Separate keys and values, reject by key, join back. Nice, thanks!
Gabe Johnson
@gabejohnson
Apr 27 2017 18:32
@justrag the problem is that reject (like filter) doesn't look at the key
:thumbsup:
Just RAG
@justrag
Apr 27 2017 18:32
Tried to force it with addIndex above, but it doesn't seem to work.
Kurt Milam
@kurtmilam
Apr 27 2017 18:34

@justrag Here's what it looks like using L.remove from partial.lenses:

L.remove( 'removeme', { "leaveme": 1, "removeme": 2, "leavemetoo": 3 } )
// -> { "leaveme": 1, "leavemetoo": 3 }

REPL

Gabe Johnson
@gabejohnson
Apr 27 2017 18:36
R.dissoc("removeme")
Kurt Milam
@kurtmilam
Apr 27 2017 18:36
Here it is using Ramda's R.dissoc:
R.dissoc( 'removeme', { "leaveme": 1, "removeme": 2, "leavemetoo": 3 } )
// -> { "leaveme": 1, "leavemetoo": 3 }
:+1:
Just RAG
@justrag
Apr 27 2017 18:39
Oh :)