These are chat archives for ramda/ramda

9th
Aug 2016
Martin Broder
@mrtnbroder
Aug 09 2016 15:17
ugh
my brain is currently not functioning
how can I refactor this to use the byDescDate?
export const byDescDate = (a, b) =>
  new Date(a).getTime() > new Date(b).getTime()
    ? -1
    : new Date(a).getTime() < new Date(b).getTime()
      ? 1
      : 0

export const sortByDescDate = sort(byDescDate)

export const sortTripsByEndTimestamp = (entities = []) => entities.sort((a, b) =>
  new Date(a.payload.endTimestamp).getTime() > new Date(b.payload.endTimestamp).getTime()
    ? -1
    : new Date(a.payload.endTimestamp).getTime() < new Date(b.payload.endTimestamp).getTime()
      ? 1
      : 0
)

export const sortTripsByCreationDate = (entities = []) => entities.sort((a, b) =>
  new Date(a.created).getTime() > new Date(b.created).getTime()
    ? -1
    : new Date(a.created).getTime() < new Date(b.created).getTime()
      ? 1
      : 0
)
Denis Stoyanov
@xgrommx
Aug 09 2016 15:33
This message was deleted
Martin Broder
@mrtnbroder
Aug 09 2016 15:34
I've actually came up with this now
export const byDescDate = (a, b) =>
  +new Date(a) > +new Date(b)
    ? -1
    : +new Date(a) < +new Date(b)
      ? 1
      : 0

export const sortByDescDate = sort(byDescDate)

export const sortByEndTimestamp = compose(sortByDescDate, map(path(['payload', 'endTimestamp'])))

export const sortByCreationDate = compose(sortByDescDate, map(path(['created'])))
not sure it works
its a bit tricky as I have to unwrap an object of array, sort the arrays, and return an object
Rick Medina
@rickmed
Aug 09 2016 17:38
hello guys! I'm learning fp and I'm a bit confused of what adt libraries should I use (sanctaury, ramda-fantasy, fantsasy-land, then there is daggy, etc.)? any guidance...?
Brad Compton (he/him)
@Bradcomp
Aug 09 2016 17:48

@rickmed Sanctuary and Ramda-Fantasy both implement the Fantasy Land spec and are thus interoperable. Use Sanctuary if you want to have runtime type checking of your parameters. Otherwise, Ramda-Fantasy is a good choice, as is Folktale (it has Maybe, Either, and Future (called Task), as well as a Validation applicative).

What structures are you looking to use?

Rick Medina
@rickmed
Aug 09 2016 17:50
just the basics as for now since I'm just learning
so maybe use sanctuary (if I need runtime type checking) or ramda fantasy (if I don't) and use the separate module folktale/validation if I need validation?
Brad Compton (he/him)
@Bradcomp
Aug 09 2016 17:52
Yeah. I think both are available in the Ramda Repl, which is super helpful. Sanctuary doesn't have Future but that's no biggie
Rick Medina
@rickmed
Aug 09 2016 17:53
will that setup all play well with ramda?
Brad Compton (he/him)
@Bradcomp
Aug 09 2016 17:53
Yeah, or just use Either for validations
Rick Medina
@rickmed
Aug 09 2016 17:53
right
Brad Compton (he/him)
@Bradcomp
Aug 09 2016 17:53
For sure!
Rick Medina
@rickmed
Aug 09 2016 17:53
awesome thanks!
Brad Compton (he/him)
@Bradcomp
Aug 09 2016 17:53
Ramda works very nicely with FL compliant types
Rick Medina
@rickmed
Aug 09 2016 17:55
is there an alternative for daggy?
James Grayling
@jamesgrayling
Aug 09 2016 17:58
paldepind/union-type?
Rick Medina
@rickmed
Aug 09 2016 18:09
looks good, thanks! @jamesbrown0
Vladimir Starkov
@iamstarkov
Aug 09 2016 18:29
hi
can anybody help me to understand how to write tests for curry and pipe?
Brad Compton (he/him)
@Bradcomp
Aug 09 2016 18:45
@iamstarkov What do you mean? Have you taken a look at Ramda's test suite?
Vladimir Starkov
@iamstarkov
Aug 09 2016 18:46
but tests there for curried function, not curry itself
it seems like implicit testing
Brad Compton (he/him)
@Bradcomp
Aug 09 2016 18:48
I think you would have to do implicit testing for higher order functions.
Vladimir Starkov
@iamstarkov
Aug 09 2016 19:06
Rafe
@rjmk
Aug 09 2016 19:49
I suppose it would be possible to declare the uncurried functions and then test the equality of the (fully applied) curried version to the (fully applied) uncurried
Martin Broder
@mrtnbroder
Aug 09 2016 20:01
hey everyone
I need to repeat my question
I have this basic sorting algorhytm
export const byDescDate = (a, b) =>
  +new Date(a) > +new Date(b)
    ? -1
    : +new Date(a) < +new Date(b)
      ? 1
      : 0
and my array looks like this:
[{ payload: { endTimestamp: "2016-08-04T08:29:16.279755" } }, { payload: { endTimestamp: "2016-08-04T07:29:16.279755" } }, ...]
now I need the exact same structure back, but sorted by the endTimestamp prop
Vladimir Starkov
@iamstarkov
Aug 09 2016 20:16
@rjmk i love this approach
Kevin Wallace
@kedashoe
Aug 09 2016 20:34
@mrtnbroder something like this? http://goo.gl/NyqBu8
Martin Broder
@mrtnbroder
Aug 09 2016 20:42
hmm
Brad Compton (he/him)
@Bradcomp
Aug 09 2016 20:44
R.sort
Ryan Zeigler
@rzeigler
Aug 09 2016 20:46
wasn't there a combinator that extracted an Orderable from the structures to be sorted?
R.sortBy
Martin Broder
@mrtnbroder
Aug 09 2016 20:47
i guess I have to combine it somehow with path(['payload','endTimestamp'])
grml.
Brad Compton (he/him)
@Bradcomp
Aug 09 2016 20:48
@mrtnbroder Something like this? http://goo.gl/XMhpE3
where on is just a helper, and you could use useWith directly
Ryan Zeigler
@rzeigler
Aug 09 2016 20:48
ie.e
let dateFromTs = R.compose(R.construct(Date), R.path(['payload', 'endTimestamp']));

R.sortBy(dateFromTs, x);
@mrtnbroder
i think it actually needs to be constructN(1, Date) because variadic
Martin Broder
@mrtnbroder
Aug 09 2016 20:54
lemme see
seems to work
but
R.groupBy is not considering the order?
@arzig @Bradcomp both work, awesome!
Martin Broder
@mrtnbroder
Aug 09 2016 21:06
nvm me
all good! thanks!
Martin Broder
@mrtnbroder
Aug 09 2016 21:19
@arzig that construct thing is awesome
Ryan Zeigler
@rzeigler
Aug 09 2016 21:20
it is that. one of my favorite things about ramda is how they reify bunches of syntactic constructs into functions
especially since my most common use of lamda's before was just because I needed to call new somewhere
Martin Broder
@mrtnbroder
Aug 09 2016 21:59
any ideas on how to make this point-free? I'm not yet that familiar with R.__ or R.flip which is something I guess I'd use here.
const eqCarId = pathEq(['payload', 'carId'])
const carFilter = (carId, trips) => filter(eqCarId(carId))(trips)

export const tripsOfCarSelector = createSelector(
  (_, { carId }) => +carId,
  tripsSelector,
  curry(carFilter)
)
(talking about the carFilter here)
Martin Broder
@mrtnbroder
Aug 09 2016 22:05
actually
David Chambers
@davidchambers
Aug 09 2016 22:16

Sanctuary doesn't have Future but that's no biggie

Particularly now that we have @Avaq's excellent Fluture. ;)

Martin Broder
@mrtnbroder
Aug 09 2016 22:26
argh
halp
I almost have it
Brad Compton (he/him)
@Bradcomp
Aug 09 2016 22:28
Yes, Fluture is great!
@mrtnbroder Are you looking for filtering based on the bar value of the first item in the list?
Martin Broder
@mrtnbroder
Aug 09 2016 22:30
yes
its just that
the last function within the createSelector
gets the return values of each function before that
in this case there are two, so I get two values
(res1, res2) => {}
figured out I could just use compose(filter, pathEq(['foo', 'bar']) and use that as the last function
but I have to uncurry it somehow
idk why it doesn't work though
does that make sense?
so compose(filter, pathEq(['foo', 'bar'])(res1)(res2) would work but actually it is called like this: compose(filter, pathEq(['foo', 'bar'])(res1, res2)
Brad Compton (he/him)
@Bradcomp
Aug 09 2016 22:39
Care for a converge solution? http://goo.gl/MktmUA
Yet another case where on is a helpful function
Oh wait
you have two arrays?
Martin Broder
@mrtnbroder
Aug 09 2016 22:40
no
res1 = Number, res2 = Array
Rafe
@rjmk
Aug 09 2016 22:41
@mrtnbroder I believe Ramda's helpful/unhelpful implementation of compose has struck again
It's probably the right choice for Ramda, because I think it's what most people picking up the library once, but the way it handles variadic functions at the front of the compose make higher order functions very tricky
Martin Broder
@mrtnbroder
Aug 09 2016 22:43
yeah but... Im..so..close...I..can..feel..it.
at least, it looks just like it :D
Rafe
@rjmk
Aug 09 2016 22:43
If instead we use S.B (an implementation of compose from Sanctuary, named thus because it is the B combinator) and uncurry, all should be well
Brad Compton (he/him)
@Bradcomp
Aug 09 2016 22:43
Brad Compton (he/him)
@Bradcomp
Aug 09 2016 22:43
like this?
Martin Broder
@mrtnbroder
Aug 09 2016 22:44
yes to both of you
isn't there something like S.B in ramda?
Rafe
@rjmk
Aug 09 2016 22:44
@Bradcomp I need to learn how useWith works
I couldn't get it going in this case
Also I think you might be able to compose compose with curryN(2) to define one
Brad Compton (he/him)
@Bradcomp
Aug 09 2016 22:46
The items in the array correspond to the parameters you want to pass in. So The first parameter gets passed into the first function, etc. The results then get passed into the first parameter you pass to useWith
Martin Broder
@mrtnbroder
Aug 09 2016 22:47
Rafe
@rjmk
Aug 09 2016 22:47
@Bradcomp Ah, I see. Like the marriage of converge and zip!
Brad Compton (he/him)
@Bradcomp
Aug 09 2016 22:47
yeah!
Martin Broder
@mrtnbroder
Aug 09 2016 22:47
works but looks ugly
Rafe
@rjmk
Aug 09 2016 22:49
@mrtnbroder Yeah. There's a tough balance to strike between (a) polyadic functions, (b) variadic functions and (c) curried functions
In that most devs want to be able to write f(a, b, c) and not have to write f(a)(b)(c), but also some functions take arbitrarily many arguments and many find it confusing for the function at the end of the composition to be partially applied to the first argument and then passed into the next function in the composition
You can define B = compose(uncurryN(1), compose)
Martin Broder
@mrtnbroder
Aug 09 2016 22:55
actually 2, no? :)
if we wanna mimic S.B
anyhoo, thanks a lot for that.
fortunately and unfortunately that you can do (a, ...b) where b can be of x length...
in javascript.
Rafe
@rjmk
Aug 09 2016 23:08
I think you want it to be 1, because you want to set the result of the composition to expect one argument, rather than setting compose itself to expect 2