Guest User

Untitled

a guest
Jan 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. // 02
  2. // const Box = x => ({
  3. // map: f => Box(f(x)),
  4. // fold: f => f(x),
  5. // inspect: () => `Box(${x})`
  6. // })
  7.  
  8. // const moneyToFloat = str =>
  9. // Box(str)
  10. // .map(s => s.replace(/\$/g, ''))
  11. // .map(r => parseFloat(r))
  12.  
  13. // const percentToFloat = x =>
  14. // Box(x.replace(/\%/g, ''))
  15. // .map(replace => parseFloat(replace))
  16. // .map(number => number * 0.01)
  17.  
  18. // const applyDiscount = (price, discount) =>
  19. // moneyToFloat(price)
  20. // .fold(cost =>
  21. // percentToFloat(discount)
  22. // .fold(savings => cost - cost * savings
  23. // ))
  24.  
  25. // const result = applyDiscount('$5.00', '20%');
  26.  
  27.  
  28. // const result = Box(3).map(num => num * 3);
  29.  
  30. // console.log(result);
  31.  
  32. //03
  33.  
  34. const Right = x => ({
  35. map: f => Right(f(x)),
  36. fold: (f, g) => g(x),
  37. inspect: () => `Right(${x})`
  38. });
  39.  
  40. const Left = x => ({
  41. map: () => Left(x),
  42. fold: (f, g) => f(x),
  43. inpspect: () => `Left(${x})`
  44. });
  45.  
  46. const result = Right(3)
  47. .map(x => x + 1)
  48. .map(x => x/2)
  49. .fold(x => 'error', x => x);
  50.  
  51. const fromNullable = x =>
  52. x != null ? Right(x) : Left(null)
  53.  
  54. console.log(result);
  55.  
  56. const findColor = name =>
  57. ({red: '#ff4444', blue: '#3b5988', yellow: '#ffff68t'})[name]
  58.  
  59. const findColor_Refactor = name => {
  60. const found = ({red: '#ff4444', blue: '#3b5988', yellow: '#ffff68t'})[name]
  61. return found ? Right(found) : Left(null)
  62. }
  63.  
  64. const findColor_Refactor_Three = name =>
  65. fromNullable({red: '#ff4444', blue: '#3b5988', yellow: '#ffff68t'}[name])
  66.  
  67.  
  68.  
  69. // const result2 = findColor('red');
  70. // console.log(result2);
  71. const result_two = findColor('red').slice(1).toUpperCase();
  72. console.log(result_two);
  73.  
  74. const result_three = findColor_Refactor('red')
  75. .map(c => c.slice(1))
  76. .fold(e => 'no color',
  77. c => c.toUpperCase())
  78. console.log(result_three);
  79.  
  80. const result_four = findColor_Refactor_Three('blue')
  81. .map(c => c.slice(1))
  82. .fold(e => 'no color',
  83. c => c.toUpperCase())
  84. console.log(result_four);
  85.  
  86. // const Right = x =>
  87. // ({
  88. // map: f => Right(f(x)),
  89. // inspect: () => `Right(${x})`
  90. // })
  91.  
  92. // const Left = x =>
  93. // ({
  94. // map: f => Left((x)),
  95. // inspect: () => `Left(${x})`
  96. // })
  97.  
  98. // const result = Left(3).map(x => x + 1).map(x => x /2)
  99. // console.log(result);
Add Comment
Please, Sign In to add comment