Skip to content

Instantly share code, notes, and snippets.

@Willmo36
Created February 13, 2020 15:18
Show Gist options
  • Save Willmo36/1e80974f5efcf814950c067fc3585592 to your computer and use it in GitHub Desktop.
Save Willmo36/1e80974f5efcf814950c067fc3585592 to your computer and use it in GitHub Desktop.
fp-ts map map invert
export const invert = <A,B,C>(ordA: Ord<A>, ordB:Ord<B>) => (map:Map<A,Map<B,C>>): Map<B,Map<A,C>> => {
const arrayABC = pipe(
map,
M.toArray(ordA),
Array.map(([a, mapBC]) => pipe(
mapBC,
M.toArray(ordB),
Array.map(([b,c]) => [a,b,c] as const)
)),
Array.flatten
)
const monoidMapAC = M.getMonoid<A,C>(ordA, getMagmaRight());
const mapBCA = pipe(
arrayABC,
Array.map(([a,b,c]) => pair(b, M.singleton(a,c))),
M.fromFoldable(ordB, monoidMapAC, Array.array)
)
return mapBCA;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment