These are chat archives for ramda/ramda

28th
Nov 2017
dcmwong
@dcmwong
Nov 28 2017 15:39
Hi, I’m not sure how to word my question but I’m trying to write a compose but the first function can validate and stop the rest of the compose from running and effectively just “return”. Can some one help me.
Mike Lambert
@lambert-velir
Nov 28 2017 15:42
you probably want to look into "Maybe"
dcmwong
@dcmwong
Nov 28 2017 15:43
thanks for replying. It felt like that but I need to return a value with the other branch
I’m composing with Eithers at the moment
Mike Lambert
@lambert-velir
Nov 28 2017 15:43
is Either not working for you?
dcmwong
@dcmwong
Nov 28 2017 15:44
I need to just return and stop execution though at the first function. Can I do that with Either?
Mike Lambert
@lambert-velir
Nov 28 2017 15:44
yeah, i think that's the behavior or "Left"
or Result in folktale
alternatively, you can use .getOrElse with Maybe to provide a default value in the case of Nothing
dcmwong
@dcmwong
Nov 28 2017 15:46
So each of my functions check for the Left and Right. if the first function was a Right it would still pass down wouldn’t it? Same goes for the Left. I just need it to stop anything and give me the value at the first function
Syaiful Bahri
@syaiful6
Nov 28 2017 15:46
@dcmwong you need to use composeK
all your function should return Either
Mike Lambert
@lambert-velir
Nov 28 2017 15:47
if you start your composition with an Either you can just map over it
map will ignore the computation if it encounters a Left
dcmwong
@dcmwong
Nov 28 2017 15:50
So to get that clear if i care about Left and Right parts I do a cata? if I don’t care about the Left I do a map?
so looked at composeK and I need a value coming back at the beginning not Nothing
Syaiful Bahri
@syaiful6
Nov 28 2017 15:55
composeK should also work with Either.
dcmwong
@dcmwong
Nov 28 2017 15:58
Thanks for that. Maybe I can give you more detail
I get value from the url that should be a date
My first function does this: if (value instanceof Date) { return value.toISOString() }
if it isn’t an instance of Date I parse it as a string as the rest of the pipeline.
So the first function I trust will give me a Date and I don’t need to validate it any further
Mike Lambert
@lambert-velir
Nov 28 2017 16:02
i would probably do it as a separate pipeline. You can have your first function return a Result. in the case of Ok, you can just extract the date, in the case of Error, you can try to parse it as a string
dcmwong
@dcmwong
Nov 28 2017 16:04
Yes but it’s not an Error at that point because it could be a valid datetime as a string
Mike Lambert
@lambert-velir
Nov 28 2017 16:06
you could also just do your string parsing in the else branch
maybe we're over thinking it
dcmwong
@dcmwong
Nov 28 2017 16:07
haha. we’re concluding the same thing
Syaiful Bahri
@syaiful6
Nov 28 2017 16:07
why not just use continuation, it more easy to compose. so your function should take a continuation and value.
const maybeAdate = (k, x) => x instanceof Date ? x : k(x)

// the identity of our Compose operation, just call the continuation
const identity = (k, x) => k(x);

const composeCont = (f, g) => (k, x) => f(g.bind(null, k), x)
that way you can just return by not calling the continuation.
Mike Lambert
@lambert-velir
Nov 28 2017 16:11
i'm not really following that
dcmwong
@dcmwong
Nov 28 2017 16:11
Sorry me neither. Can you send the code that I will call to that?
Mike Lambert
@lambert-velir
Nov 28 2017 16:12
i would just have 2 functions, getISOString and parseDate, each of these return Results
dcmwong
@dcmwong
Nov 28 2017 16:13
thanks @lambert-velir I think that’s what we’re going to do
@syaiful6 thanks. I’m going to study your suggestion and come back to at later date.
Mike Lambert
@lambert-velir
Nov 28 2017 16:26
i'm not sure how you can use this in the same pipeline, you probably need to use nested .matchWiths
Syaiful Bahri
@syaiful6
Nov 28 2017 16:33
Mike Lambert
@lambert-velir
Nov 28 2017 16:45
I just clarified some things with Result over in the folktale/discussion gitter
if you have those 2 functions that return results, you can do getISOString(dateString).orElse(parseDate)
dcmwong
@dcmwong
Nov 28 2017 21:05
Fantastic I’ll give that a go tomorrow
Sean Cannon
@SeanCannon
Nov 28 2017 21:50
Any python devs in here might appreciate this : https://github.com/jackfirth/pyramda