Learning Elixir, frst impressions ( plz don't kill me ! )

Now, I would have to disagree here.

My point was that the JavaScript runtime isn’t designed to cope with unbridled recursion regardless of the available tricks. Any for loop avoidance is largely implemented via higher order functions which hide the implementing iterative loop away from you.

using a couple of libraries you can do pure FP

  • those libraries can cheat under the hood (not that it’s necessarily a problem) to align with the sensibilities of the JavaScript runtime
  • and they can’t ensure that your code is pure compared to a compiler

A previous user just said Elixir doesn’t really do function composition.

Not out-of-the-box.

This adheres to our general principle that we do not provide built-in mechanisms; we provide primitives with which mechanisms can be built.

defmodule Demo do

  def f(x) do
    x + 2
  end

  def g(x) do
    x * 3
  end

  # dynamic composition with a closure
  def compose(g,f) do
    fn (x) ->
      x
      |> f.()
      |> g.()
    end
  end

  # "static composition"
  def g_comp_f(x) do
    x
    |> f()
    |> g()
  end

end

x = 5
fun = Demo.compose(&Demo.g/1,&Demo.f/1)

IO.inspect(fun.(x))
IO.inspect(Demo.g_comp_f(x))
$ elixir demo.exs
21
21
$

Here is an example of 2 functions called reverse.

No it is not. Those are two function clauses belonging to the same function. They are functionally equivalent to:

def reverse(list, reversed) do
  case list do
    [] ->
      reversed

    [head|tail] ->
      reverse(tail, [head|reversed])
  end
end

See

I do believe Haskell has some things Elixir could really use.

Why don’t you use Haskell then? There are plenty of forum users here who know Haskell and use it whenever possible but still find Elixir/Erlang useful under many circumstances. I’d personally prefer if there was static typing with a real type system but it’s absence isn’t a showstopper (and I blame JavaScript for getting me used to that fact).

But I do believe that assessing syntax as irrelevant is irresponsible at best.

It’s meant as a “stop whining and get on with the business at hand” notion - and we are talking about Elixir/Erlang here - not JS syntax hampered by backward compatibility considerations.

4 Likes