-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat request: .ap (apply) on ADTs #43
Comments
Hi! Yes, I believe these are applicatives. so if I look for instance at your crocks link: // add :: Number -> Number -> Number
const add =
x => y => x + y
Maybe.of(add)
.ap(Just(5))
.ap(Just(27))
//=> Just 32 It seemed to me that the 'lift' approach is easier to handle with typescript (but this can be discussed). So one way to express this same thing in prelude today is like that: const lifted = Option.liftA2((x:number,y:number) => x+y);
lifted(Option.of(5), Option.of(6));
=> Option.of(11)
const lifted2 = Option.liftA2((x:number,y:number) => x+y);
lifted2(Option.of(5), Option.none<number>());
=> Option.none() For that specific use-case we achieve the same outcome. liftA2 is obviously limited to only two parameters. For more, there is liftAp. const lifted = Option.liftAp((x:{a:number,b:number,c:number}) => x.a+x.b+x.c);
lifted({a:Option.of(5), b:Option.of(6), c:Option.of(3)});
=> Option.of(14) The lift functions are also available for other functors, like Either and so on. As per your research, it seems that the |
I'm not an expert on ADTs, but I am used to seeing things like I found this from some googling:
I'll keep doing some more digging and see if I can do what I want with |
I think I was able to wrap my head around how to use Consider this workflow (using crocks): import Maybe from 'crocks/Maybe'
// define two people, one with a defined age and one without
const personOne = Maybe.of({
name: 'Bob',
})
const personTwo = Maybe.of({
name: 'Jane',
age: '33'
})
// this function returns Just the age or Nothing
const getAge = x =>
x.age ? Maybe.Just(x.age) : Maybe.Nothing()
// get the age and log it, or log that age is not defined
const logAge = x =>
x.chain(getAge)
.coalesce(
() => console.log('age is not defined'),
console.log
)
logAge(personOne)
// age is not defined
logAge(personTwo)
// 33 This is a pretty common pattern for me (I use it for rendering a react component if data exists, or rendering an empty state). Without Perhaps this is a different way of thinking that doesn't line up with Since I am expanding the scope of my issue, should I rename or change this issue into something else? |
I think instead of The concepts are mostly the same, it's just one style or the other I think. |
Ah, that makes sense. Here is what I've come up with: import { Option } from 'prelude-ts'
const { log } = console
const personOne = Option.of({
name: 'Bob',
})
const personTwo = Option.of({
name: 'Jane',
age: '33'
})
const getAge = (x: { age?: number }) =>
x.age ? Option.some(x.age) : Option.none()
const logAge = (x: Option<any>) =>
x.flatMap(getAge)
.ifNone(() => console.log('age is not defined'))
.map(log)
logAge(personOne)
// age is not defined
logAge(personTwo)
// 33 Is there a language or source for the inspiration of |
Prelude-ts is inspired by the vavr Java library, itself inspired by the Scala language. Did you see the prelude user guide? |
I did take a look through that guide. I can try and wrap my head around Scala better, but I'm not sure I'd be able to sell I would love to be able to use something like I'll keep an eye on this library and keep playing around with it, but I am not sure it meets my needs at this point. |
No problem! I think it's a matter of which library gives you what you need. What prelude does better than the others, it seems to me, would be on the collections side, especially the proper hashmap with hashing & equality, enabling a proper groupBy for instance, and typescript types (but fp-ts may be even better on that, not sure). Certainly I would keep this bug open, regarding the In the end, I think the differences here are really more cosmetic, these are all functors, applicatives, monads. So i'm really happy to see that ecosystem of solutions! |
I agree about the collections! That is one of the really appealing things to me. |
I couldn't find an equivalent function in
prelude-ts
so please let me know if this exists and I am just missing it.A common pattern on ADTs (algebraic data types) is
ap
(short for apply).From the (Mostly Adequate Guide to Functional Programming)[https://mostly-adequate.gitbooks.io/mostly-adequate-guide/ch10.html#ships-in-bottles]:
Examples:
crocks
fp-ts
monet.js
The text was updated successfully, but these errors were encountered: