-
-
Notifications
You must be signed in to change notification settings - Fork 502
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
Question: How to "yield" more than one Option? #1236
Comments
What you're looking for is import * as O from 'fp-ts/lib/Option'
import { pipe } from 'fp-ts/lib/pipeable'
import { sequenceT } from 'fp-ts/lib/Apply'
const foo: O.Option<string> = O.some('foo');
const bar: O.Option<string> = O.some('foo');
sequenceT(O.option)([foo, bar]) // Option<[string, string]> |
A few options Option 1 (liftA2) import * as O from 'fp-ts/lib/Option'
import { pipe } from 'fp-ts/lib/function'
const concat = (a: string) => (b: string) => a + b
pipe(O.some(concat), O.ap(O.some('foo')), O.ap(O.some('bar'))) Option 2 (sequenceT) import * as O from 'fp-ts/lib/Option'
import * as A from 'fp-ts/lib/Apply'
import { pipe } from 'fp-ts/lib/function'
pipe(
A.sequenceT(O.option)(O.some('foo'), O.some('bar')),
O.map(([foo, bar]) => foo + bar)
) Option 3 (sequenceS) import * as O from 'fp-ts/lib/Option'
import * as A from 'fp-ts/lib/Apply'
import { pipe } from 'fp-ts/lib/function'
pipe(
A.sequenceS(O.option)({ foo: O.some('foo'), bar: O.some('bar') }),
O.map(({ foo, bar }) => foo + bar)
) Option 4 (chain + map) import * as O from 'fp-ts/lib/Option'
import { pipe } from 'fp-ts/lib/function'
pipe(
O.some('foo'),
O.chain((foo) =>
pipe(
O.some('bar'),
O.map((bar) => foo + bar)
)
)
) |
Another option you may like, though it's not contained in import * as O from 'fp-ts/lib/Option'
import { Do } from 'fp-ts-contrib/lib/Do'
Do(O.option)
.bind('foo', O.some('foo'))
.bind('bar', O.some('bar'))
.return(({ foo, bar }) => foo + bar) |
Nice. Thank you all ❤️ . I close the issue. |
Thank you for helpful issue and answers. import * as O from 'fp-ts/Option'
import { pipe } from 'fp-ts/function'
pipe(
O.bindTo('foo')(O.some('foo')),
O.bind('bar', () => O.some('bar')),
O.map(({ foo, bar }) => f(foo, bar))
) |
Problem
I have two
Option
s and I want to do an operation only if both aresome
. I found this issue #322 but failed to get it working 😢 .Example in typescript
Example in scala
The text was updated successfully, but these errors were encountered: