Effect CommunityEC
Effect Community3y ago
17 replies
kvothe

Issues with applicative functors in effect

Hello! I'm learning effect and so far I really like it! 🚀 I'm having issues translating some applicative code to effect, for example, the following works perfectly fine in crocks:
const safeAdd = (x, y) => Maybe(add).ap(Maybe(x)).ap(Maybe(y))
expect(safeAdd(2, 3).equals(Maybe(5))).toBeTruthy()
expect(safeAdd(null, 3).equals(Maybe(3))).toBeTruthy()

But I tried translating that into Effect with Option.ap and it is throwing a very ugly error message (and not working):
// safeAdd :: Option<number> -> Option<number> -> Option<number>
const safeAdd = (x: number | null, y: number | null) =>
  pipe(
    Option.some(Number.sum),
    Option.ap(Option.fromNullable(x)),
    Option.ap(Option.fromNullable(y))
  )
expect(safeAdd(2, 3)).toEqual(Option.some(5))
expect(safeAdd(null, 3)).toEqual(Option.some(3))

Any help would be much appreciated!
Was this page helpful?