These are chat archives for ramda/ramda

16th
Sep 2017
Sourav Chatterjee
@chattes
Sep 16 2017 08:55
Guys hello!
Quick Question R.composeP(getSessionInfo,ReadData)() so ReadData returns a Promise, which Resoves or Rejects and then getSessionInfo is called how to detect for resolve or reject in such cases?
Gunar Gessner
@gunar
Sep 16 2017 15:22
Hi all, going to post my quick question on top of @chattes as I can't answer his.
Shouldn't this work? R.chain(maybeProp('id'), anInstanceOfFuture)
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 15:23
@gunar where is maybeProp coming from?
Gunar Gessner
@gunar
Sep 16 2017 15:23
@alexandrethsilva const maybeProp = R.pipe(R.prop, Maybe.fromNullable)
From folktale/maybe (and folktale/concurrency/future)
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 15:24
ah okay, that was gonna be my next question. if it was from ramda-fantasy or folktale :smile:
Gunar Gessner
@gunar
Sep 16 2017 15:25
ramda-fantasy looks nice but is in alpha(?), so I went for Folktale.
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 15:25
so, I may be tripping, but I don't see how that's gonna work without you first forking it
ramda-fantasy looks nice but is in alpha(?), so I went for Folktale.
don't think it's really alpha. i'm using it for a while in prod
Gunar Gessner
@gunar
Sep 16 2017 15:26
As far as I understand, forking means executing the code, not what I want. I want to compose a sequence of computations.
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 15:26
i mean, it is, but i'm using it without problems for a while. i guess that's what i meant
yeah, but the way you're doing it seems odd to me. maybe because i'm seeing it out of context
can you show a bit of how you're using that?
but yeah, thinking about it... seems like it should work
Gunar Gessner
@gunar
Sep 16 2017 15:30
Sure! The way I am approaching this is probably wrong, though.
A have a Future of { id: XXX }. I want to create a function to check if it matches id.
If this were a Promise, I would do something like this const check = async id => id === (await promise).id
But that's ugly and can't compose. I want this in order to use composeK.
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 15:36
k, I'm trying to come up with an example of what I meant
Gunar Gessner
@gunar
Sep 16 2017 15:40
So I guess I can't convert Future to Maybe without forking, but I could keep the computation inside a Future until the very end. That is, it would be fine for my check function to return Future[Maybe].
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 15:40
exactly
Gunar Gessner
@gunar
Sep 16 2017 15:41
Cool! So, what do?
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 15:41
ha, I was trying to put something together to tell you exactly what you said in your 1st sentence
Gunar Gessner
@gunar
Sep 16 2017 15:41
hehe yeah I've read some on the topic, but this is the first time I am trying to apply it.
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 15:43
what you really want to do is to have safety when getting the id at the end of it all, right?
Gunar Gessner
@gunar
Sep 16 2017 15:43
I would expect R.map(maybeProp('id'), Future.of({ id: 5 })) to return Future[Maybe] but it throws instead.
eahm, yes and no. What I really what is that check function so I can use it in multiple place (with composeK?).
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 15:45

that is, if you have

Future.of({id: 5})
  .map(maybeProp('id')) // --> Future[Maybe[Number]]

and

Future.of({id: 5})
  .chain(maybeProp('id')) // --> Maybe[Number]
you mean check being this, right?
const check = R.chain(maybeProp('id'), anInstanceOfFuture)
Gunar Gessner
@gunar
Sep 16 2017 15:46
Nice! So folktale has a bug, because your first example throws.
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 15:47
It throws when you fork you mean?
Gunar Gessner
@gunar
Sep 16 2017 15:48
Well check would still need an equality comparator (to compare to input).
I don't even have to fork it, for it to throw. Am using the first snippet exactly. (Accidentally removed last message).
Hmm, the problem might lie in
const maybeProp = R.pipe(R.prop, Maybe.fromNullable)
which is something I came up with myself
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 15:50
what version of folktale are you on again?
Gunar Gessner
@gunar
Sep 16 2017 15:50
2.0.1
Guess I have been thinking of folktale.Future as Data.Task, which is different.
console.log(R.map(R.prop('id'), Future.of({id: 5})))
// folktale:Future(folktale:ExecutionState.Resolved({ value: 5 }), 0 listeners)
It has "listeners".... hmm
I have no attachments to folktale so lemme try ramda-fantasy real quick
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 15:54
k, I'm looking here too
Gunar Gessner
@gunar
Sep 16 2017 15:56
This works now with ramda-fantasy. My guess is that folktale v2 mixed up Future and Task.
R.map(R.prop('id'), Future.of({id: 5})).fork(console.log, console.error)
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 15:57
yeah, I didn't really get the changes of Folktale when moving to v2 really
got a bit confusing in my opinion, so i decided to stick with ramda-fantasy for most of it
Gunar Gessner
@gunar
Sep 16 2017 15:58
This still throws though
R.map(maybeProp('id'), Future.of({id: 5}))
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 15:58
as a side note, your fork is switched, as it's right biased
Gunar Gessner
@gunar
Sep 16 2017 15:58
Oh, ok.
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 16:00
I guess you'd be better off with
Future.of({id: 5}).map(maybeProp('id')).fork(console.error, console.log)
Gunar Gessner
@gunar
Sep 16 2017 16:02
One thing was that I had to rename fromNullable to toMaybe. Now it seems I have a currying problem still.
const maybeProp = R.pipe(R.prop, Maybe.toMaybe)
won't work because prop expects 2 args and pipe expects to pass a single arg
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 16:03
yeah, so that was gonna be my next point
:smile:
Gunar Gessner
@gunar
Sep 16 2017 16:04
const maybeProp = prop => obj => Maybe.toMaybe(R.prop(prop, obj))
This works but is ugly haha. Any way around it?
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 16:05

you could also get away with something like

const maybeProp = R.curry((key, value) => R.pipe(R.prop(key), Maybe.toMaybe)(value))

or

const maybeProp = R.curry((key, value) => R.compose(Maybe.toMaybe, R.prop(key))(value))
Gunar Gessner
@gunar
Sep 16 2017 16:06
@alexandrethsilva do you usually define your own maybeProp, or consume from some library?
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 16:09
That's a great question, but tbh, now that I'm thinking about it, I usually do a bit of repetition there, in that I make an equivalent of toMaybe every time
a maybeProp like you're doing is a better solution
Gunar Gessner
@gunar
Sep 16 2017 16:10
Got it
Well, thank you so much, sir!
Unfortunate that ramda-fantasydoes not include Future.fromPromise
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 16:11
Yeah, that's true
And no problem! Hope it was of some help :smile:
Alexandre Theodoro da Silva
@alexandrethsilva
Sep 16 2017 16:19

And @gunar, as a last note regarding

Unfortunate that ramda-fantasydoes not include Future.fromPromise

Maybe it's worth asking around there if they'd accept a PR on this?