Guest User

Untitled

a guest
May 27th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. export const contramap = <A, B>(f: Fn<B, A>, O: Ord<A>): Ord<B> => ({
  2. compare: (x: B, y: B) => O.compare(f(x), f(y))
  3. });
  4.  
  5. const stringOrd: Ord<string> = ({
  6. compare: (x: string, y: string) => x < y ? Ordering.LT : x > y ? Ordering.GT : Ordering.EQ
  7. })
  8.  
  9. //extending stringOrd to work on Person objects
  10. const firstName: Ord<Person> = contramap(x => x.first, stringOrd);
  11. const lastName: Ord<Person> = contramap(x => x.last, stringOrd);
  12.  
  13. //combining Ord<Person> instances
  14. const lastThenFirst: Ord<Person> = append(lastName, firstName);
  15.  
  16. //composing Ord<Person> instances, inverting the lastName sort
  17. const firstThenLastDescending: Ord<Person> = append(firstName, invert(lastName))
Add Comment
Please, Sign In to add comment