-
-
Notifications
You must be signed in to change notification settings - Fork 502
ReadonlyArray
Giulio Canti edited this page Sep 2, 2022
·
3 revisions
Problem
How can I go from
ReadonlyNonEmptyArray<A>
toArray<A>
?
Solution
import * as RA from 'fp-ts/ReadonlyArray'
const solution: <A>(as: ReadonlyArray<A>) => Array<A> = RA.toArray
Problem
I'm trying to generate every possible object, from a list of possible keys
const COLORS = ['red', 'blue'] as const;
const THINGS = ['ball', 'box'] as const;
interface ColoredThing {
color: typeof COLORS[number];
thing: typeof THINGS[number];
}
const ALL_POSSIBLE_COLORED_THINGS = ?
Solution
import * as RA from 'fp-ts/ReadonlyArray'
import { pipe } from 'fp-ts/function'
const COLORS = ['red', 'blue'] as const
const THINGS = ['ball', 'box'] as const
interface ColoredThing {
color: typeof COLORS[number]
thing: typeof THINGS[number]
}
const ALL_POSSIBLE_COLORED_THINGS: ReadonlyArray<ColoredThing> = pipe(
RA.Do,
RA.apS('color', COLORS),
RA.apS('thing', THINGS)
)
Alternatively you can use "array comprehension"
const ALL_POSSIBLE_COLORED_THINGS = RA.comprehension(
[COLORS, THINGS],
(color, thing) => ({
color,
thing
})
)