Hacker News new | past | comments | ask | show | jobs | submit login

Oof, I don't know. I love Lisps, generally, having gotten my start with Scheme back in the day. But that monadic thing just rubs me the wrong way. I don't program Clojure, though, so maybe it's just lack of experience. If they had gone with Rust's Ok/Error rather than Haskell's Left/Right, maybe it would be better to me. (Yes, yes, I know, the monad is more general than that, but if it's used 99% of the time for handling errors, maybe optimize for that use case.)

I program in Elixir now, which I'm totally loving. It's also a "no early return" and "totally immutable" language, so I wonder if any "cross pollination" between the languages is possible. This is how I would code the example given in Elixir:

    with {:ok, x} <- foo(),
         y when not is_nil(y) <- bar(x),
         {:ok, z} <- goo(x, y) do
      qux(x, y, z)
      IO.puts "it worked"
      true
    else
      {:error, :not_fooable} -> IO.puts("foo failed"); false
      {:error, :no_bar} -> IO.puts("bar failed"); false
      {:error, :no_goo} -> IO.puts("goo failed"); false
      nil -> IO.puts("something (bar?) returned nil"); false
      _ -> IO.puts("some other error"); false
    end
Elixir's cool "with" / "else" macro is used for chains of calls where the intermediate steps can fail. In fact, it was introduced not too long ago, just to deal with nesting code issues like those raised here!

It relies on elixir/erlang's pattern matching to specify the happy path, extracting out the success responses (e.g. {:ok, x} will match a response of {:ok, 5}, and x will hold the value 5). If any of the happy matches fail, it falls through to the `else` and you can catch any particular failures you want there.

It's a really great system. Someone should try to add it to Clojure! Not sure how well the "pattern matching" aspect of it can work, though.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: