Created
February 13, 2020 15:18
-
-
Save Willmo36/1e80974f5efcf814950c067fc3585592 to your computer and use it in GitHub Desktop.
fp-ts map map invert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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