I have two Options and I want to do an operation only if both are some. I found this issue #322 but failed to get it working 馃槩 .
import { some } from 'fp-ts/lib/Option';
import { pipe } from 'fp-ts/lib/pipeable';
const foo: Option<string> = some('foo');
const bar: Option<string> = some('foo');
// ??
pipe([foo, bar], ???);
for (
foo <- Some("foo");
bar <- Some("bar")
) yield foo + bar
What you're looking for is sequenceT.
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 fp-ts's core (yet): in fp-ts-contrib there's a simulation of Haskell do notation
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.
Most helpful comment
A few options
Option 1 (liftA2)
Option 2 (sequenceT)
Option 3 (sequenceS)
Option 4 (chain + map)