These are chat archives for ramda/ramda

24th
May 2016
Christian Bongiorno
@chb0github
May 24 2016 00:22
I am trying to figure out how I can recursively apply an evolve function. I have objects with special named properties and if during evolution it stumbles on them I want it to DFS and apply the evolve function
Taylor Hurt
@thurt
May 24 2016 03:47
@chb0github do you have some sample input data and desired output data?
Taylor Hurt
@thurt
May 24 2016 09:15
@hkrutzer I thought about your problem and wrote a recursive function. http://goo.gl/rcFNWs On a side note, I am hard pressed to go full adoption of all ramda functions. I still find it easier to read and write in ES5/6, but I am adopting some ramda functions that I find are more intuitive and semantic
Taylor Hurt
@thurt
May 24 2016 09:23
small correction to output http://goo.gl/YghFnE
Christian Bongiorno
@chb0github
May 24 2016 15:19
@thurt Yeah, let me put together a sample.
LeonineKing1199
@LeonineKing1199
May 24 2016 15:57
So, I'm using sortBy to sort a list of objects by a boolean property. Is it weird that in this example sorting by `=== true` puts the elements that match that predicate last?
I assumed sortBy would put the truthy ones first for some reason.
Christian Bongiorno
@chb0github
May 24 2016 15:58
Why? The keys in an Object are done by hash
hash is, by nature, unordered
random key distribution
LeonineKing1199
@LeonineKing1199
May 24 2016 15:59
Is that @ me?
Christian Bongiorno
@chb0github
May 24 2016 15:59
@LeonineKing1199 yeah :)
LeonineKing1199
@LeonineKing1199
May 24 2016 15:59
I'd hope that all sorting algorithms are ordered O_o
Christian Bongiorno
@chb0github
May 24 2016 15:59
NO!
Sorting algos can be categorrized into 'stable' and 'unstable'
LeonineKing1199
@LeonineKing1199
May 24 2016 16:00
That is indeed true.
Anyway, I was just kind of surprised that Ramda puts the false evaluations first and the true evaluations last. For some reason, it seems like it should be the other way around.
Christian Bongiorno
@chb0github
May 24 2016 16:00
stable means that: Given 2 elements to compare 'a' and 'a', the first 'a' will be first in the sorted output
'false' is lexically before 'true'
unstable, means that: either condition can hold
@thurt here is my example from earlier: http://goo.gl/tT72kj
LeonineKing1199
@LeonineKing1199
May 24 2016 16:02
``````const data = [ {boolProp: true}, {a: 1}, {boolProp: true}, {b: 2}]
sortBy(obj => obj.boolProp === true, data)``````
Yields: `[{"a": 1}, {"b": 2}, {"boolProp": true}, {"boolProp": true}]`
To me, it seems like it should yield: `[{"boolProp": true}, {"boolProp": true}, {"a": 1}, {"b": 2}]`
Christian Bongiorno
@chb0github
May 24 2016 16:05
nope. 'a' is lexically before 'boolprop'
It's lexically sorted.
LeonineKing1199
@LeonineKing1199
May 24 2016 16:07
Maybe I'm misunderstanding this but I'm attempting to sort by a predicate which is based on the boolean value of a property. I don't see how lexicographically this matters. It should be partitioning elements based on their evaluation of the predicate.
Christian Bongiorno
@chb0github
May 24 2016 16:08
Let me read the docs closely here
LeonineKing1199
@LeonineKing1199
May 24 2016 16:13

This is the signature: `Ord b => (a → b) → [a] → [a]`

I'm assuming that in this scenario, false is default sorted before true. Hence my "unusual" behavior.

Maybe I don't need `sortBy` and am instead looking for `sortByPredicate`.
Christian Bongiorno
@chb0github
May 24 2016 16:13
Ask yourself this question: As you go through this list of elements, and the sort comes upon '{a:1}' how do you think it will respond
look at partition
LeonineKing1199
@LeonineKing1199
May 24 2016 16:14
Yeah, I think I'm better off at using `concat` and `partition`.
But `sortBy` even has the name in it! I wanna sort by a boolean predicate :P
Christian Bongiorno
@chb0github
May 24 2016 16:14
'{a:1}' has no field 'boolPro'
@LeonineKing1199 I literally changed your function to partition and it's what you want
Keep in mind though: Anything in the 'false' category can be in any order.
as this includes: 'undefined', 'null', 'false'
LeonineKing1199
@LeonineKing1199
May 24 2016 16:25
Well, it half-does what I want. I'd like to keep 1 array instead of an array of arrays. Regardless, I simply inverted my predicate and it's working fine. I think that Ramda just goes off the default JS runtime implementation. Por ejemplo,
``````var a = [true, false, true, false, true, false];
a.sort()
// [false, false, false, true, true, true]``````
So it looks like it's native JS to put false before true.
Maybe I could somehow use `chain` to flatten the results of a `partition`? I feel like this is a lot of cognitive overhead though...
Christian Bongiorno
@chb0github
May 24 2016 16:28
well, it's 'lexical' so ask yourself which is 'less' than the other? 'true' or 'false'
LeonineKing1199
@LeonineKing1199
May 24 2016 16:29
It's not lexical, it's whatever the supplied functor returns.
Christian Bongiorno
@chb0github
May 24 2016 16:29
do this math and tell me your answer: 'true' - 'false'
LeonineKing1199
@LeonineKing1199
May 24 2016 16:29
(a -> b)
Christian Bongiorno
@chb0github
May 24 2016 16:30
I think you misunderstand. You're supplying a function to get at value to compare
try your sort with just 'obj.boolProp'
not '=== true'
LeonineKing1199
@LeonineKing1199
May 24 2016 16:31
I think `sort` might actually do what I want better...
Christian Bongiorno
@chb0github
May 24 2016 16:32
It could, but then you need to decide: 'true' - 'false' = ?
LeonineKing1199
@LeonineKing1199
May 24 2016 16:32
Hmm...
Christian Bongiorno
@chb0github
May 24 2016 16:33
sort by is returning a value: true/false and then you're leaving it up to ramda to decide HOW 'true' -'false' behaves
and, it's not unreasonable to simply sort by their string equivalent
LeonineKing1199
@LeonineKing1199
May 24 2016 16:33
Bro, I'm gonna post some code. And it's gonna blow your mind.
'Cause, like, just, bro.
Actually, using `sort` is even worse than just inverting my predicate.
Christian Bongiorno
@chb0github
May 24 2016 16:35
You just aren't getting sorting
Not trying to be condescending :)
LeonineKing1199
@LeonineKing1199
May 24 2016 16:35
Oh, the ironing.
Christian Bongiorno
@chb0github
May 24 2016 16:36
you have these cases to consider for a sort:
obj.boolProp === [true|false|null|undefined]
if you have 'false' and 'true', which do you want to come first?
answer some questions and let's get the function that works
LeonineKing1199
@LeonineKing1199
May 24 2016 16:39
I appreciate the help but I already know what's going on and have completed my task. I'm moving on. I will petition for a flat version of `partition` though.
Christian Bongiorno
@chb0github
May 24 2016 16:46
Good luck.
LeonineKing1199
@LeonineKing1199
May 24 2016 16:51
Oh man, you could very easily implement a `sortByPredicate` just using native array methods, `Array.prototype.unshift()` and `Array.prototype.push()`. You `unshift` all elements whose predicate evaluates to true and `push` all elements who evaluate to false using the supplied predicate.
Christian Bongiorno
@chb0github
May 24 2016 16:54
Just tell me which comes first: true or [false,undefined,null] and you can supply a compare easy and skip the shifting and pushing
LeonineKing1199
@LeonineKing1199
May 24 2016 16:55
It might actually be easier to pass those values into `Boolean` first because the ultimate goal is comparing just booleans, not these weird polymorphic types.
Christian Bongiorno
@chb0github
May 24 2016 16:59
until you resolve true - false = ? You're gonna get surprises.
LeonineKing1199
@LeonineKing1199
May 24 2016 16:59
I don't get where you're getting true - false from.
Christian Bongiorno
@chb0github
May 24 2016 17:00
obj.booleanProp === true
You have objects in your list that don't even have the property
which means you have a predicate of 'object.undefined === true'
Taylor Hurt
@thurt
May 24 2016 18:48
@chb0github I'm not sure if I understand all of your requirements but a recursive evolve could be something like http://goo.gl/R94v7W
Christian Bongiorno
@chb0github
May 24 2016 22:16
I literally implemented something identical. I was wondering if there was a ramda-recursive means to do it. Thought for sure there was. Thanks @thurt
Taylor Hurt
@thurt
May 24 2016 22:34
@chb0github it may be possible. I am new to ramda so not sure
Taylor Hurt
@thurt
May 24 2016 23:20
I am frequently running into an issue when trying to make compositions where i have a beginning 2 arity function that returns a single value, but later down a pipe/compose i need to refer back to one of the original parameters. Your recursive evolve is a good example of this
Christian Bongiorno
@chb0github
May 24 2016 23:21
What you have then is a partial
You would basically setup the partial with the first bit of data you have -- it's a partial or a partialRight
Taylor Hurt
@thurt
May 24 2016 23:52
Interesting i will have to read up on partial. Please share of you can come up with a composed version of the recursive evolve