0

I want to have a pipe that does some operations on a Maybe, and want to return its value at last. Currently I am doing:

const data = Maybe(5)
pipe(
  map(add(1)),
  ... other operations
  y => y.getOrElse([])
)(data)

Is there any cleaner way out?

jgr0
  • 697
  • 2
  • 6
  • 20
  • 3
    No, I think that's it. However, replacing `Maybe a` with `[a]` doesn't give you much. It is just another representation of computations that may not produce a result. I would stick with the `Maybe` abstraction. Btw, `Maybe` is an odd name for a value constructor. It should be `Just`. –  Jun 29 '17 at 09:16
  • @ftor, I would assume that "...other operations" return a meaningful array, not just an array wrapping that one value. In that case, `[]` might be the appropriate default, as it often is for arrays. – Scott Sauyet Jun 29 '17 at 19:39
  • I agree with @ftor that there's nothing substantially better, and that you might be better off sticking with the Maybe. – Scott Sauyet Jun 29 '17 at 19:40

1 Answers1

0

The only improvement would be to create a pointfree helper function

const getOrElse = (defaultValue) => (m) => m.getOrElse(defaultValue);
Alfred Young
  • 397
  • 1
  • 3
  • 15