Guest User

Untitled

a guest
Mar 17th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. const Nothing = () => ({
  2. isJust: () => false,
  3. fold: (b, f) => b,
  4. inspect: () => `Nothing`
  5. });
  6.  
  7. const Just = x => ({
  8. isJust: () => true,
  9. fold: (b, f) => f(x),
  10. inspect: () => `Just${x}`
  11. });
  12.  
  13. const isJust = x => x.isJust();
  14.  
  15. const isNothing = x => !x.isJust();
  16.  
  17. const maybeCatamorph = b => f => x => x.fold(b, f);
  18.  
  19. const identity = x => x;
  20.  
  21. const fromMaybe = b => x => x.fold(b, identity);
  22.  
  23. const listToMaybe = ([head, ...tail]) => (head ? Just(head) : Nothing());
  24.  
  25. const _ = "";
  26.  
  27. const maybeToList = x => (isJust(x) ? [fromMaybe(_)(x)] : []);
Add Comment
Please, Sign In to add comment