Hacker News new | ask | show | jobs
by notpushkin 60 days ago
I’ve had to click around a bit and I think this is a better summary: https://effect.website/docs/v4/getting-started/why-effect#th...

  import { Effect } from "effect"
  
  const divide = (a: number, b: number): Effect.Effect<number, Error, never> =>
    b === 0
      ? Effect.fail(new Error("Cannot divide by zero"))
      : Effect.succeed(a / b)
  
  Effect.runSync(divide(4, 2)) // => 2
This seems to be similar in spirit to railway oriented programming (see e.g. https://returns.readthedocs.io/en/latest/pages/railway.html), but I’m not 100% confident here.
1 comments

Monad Maybe?
Kinda like that, but with the error branch having a value too. I think Result is the common name for this monad:

  type Result e v = Ok v | Err e
https://en.wikipedia.org/wiki/Result_type