Advertisement
DoctorRynerNew

Untitled

Oct 4th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // findByIndex :: Int -> [a] -> Maybe a
  2.  
  3. //           0  1  2
  4. const arr = [2, 4, 6]
  5.  
  6. const testJust = findByIndex (2) (arr)
  7. // => { val = 2 }
  8.  
  9. const testNothing = findByIndex (3) (arr)
  10. // => { val = null }
  11.  
  12. // mapMaybe :: (a -> b) -> Maybe a -> Maybe b
  13.  
  14. const succ = x => x + 1
  15.  
  16. mapMaybe (succ) (testJust)
  17. // => { val = 3 }
  18.  
  19. mapMaybe (succ) (testNothing)
  20. // => { val = null }
  21.  
  22. // isJust :: Maybe a -> Bool
  23.  
  24. isJust (testJust)
  25. // => true
  26.  
  27. isJust (testNothing)
  28. // => false
  29.  
  30. // fromJust :: Maybe a -> a
  31.  
  32. fromJust (testJust)
  33. // => 2
  34.  
  35. fromJust (testNothing)
  36. // => ОШИБКА!
  37.  
  38. // withDefault  :: a -> Maybe a -> a
  39.  
  40. withDefault (0) (testJust)
  41. // => 2
  42.  
  43. withDefault (0) (testNothing)
  44. // => 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement