Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export const contramap = <A, B>(f: Fn<B, A>, O: Ord<A>): Ord<B> => ({
- compare: (x: B, y: B) => O.compare(f(x), f(y))
- });
- const stringOrd: Ord<string> = ({
- compare: (x: string, y: string) => x < y ? Ordering.LT : x > y ? Ordering.GT : Ordering.EQ
- })
- //extending stringOrd to work on Person objects
- const firstName: Ord<Person> = contramap(x => x.first, stringOrd);
- const lastName: Ord<Person> = contramap(x => x.last, stringOrd);
- //combining Ord<Person> instances
- const lastThenFirst: Ord<Person> = append(lastName, firstName);
- //composing Ord<Person> instances, inverting the lastName sort
- const firstThenLastDescending: Ord<Person> = append(firstName, invert(lastName))
Add Comment
Please, Sign In to add comment