Hacker News new | ask | show | jobs
by divs1210 1798 days ago
The problem here is that its inconvinient to define a function at the Elixir REPL because it demands that it be put inside a module.

Clojure REPLS allow switching the namespace, and anything you define goes intoo the current namespace. So it is more ergonomic.

1 comments

IEx will let you define a module at the REPL. Combine that with a macro (exercise for reader) in your .iex.exs which expands a function into a module definition and imports it and you're in business.

    iex(1)> defmodule Foo, do: def bar(x), do: x + 1
    {:module, Foo,
     <<70, 79, 82, 49, 0, 0, 4, 192, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 135,
       0, 0, 0, 15, 10, 69, 108, 105, 120, 105, 114, 46, 70, 111, 111, 8, 95, 95,
       105, 110, 102, 111, 95, 95, 10, 97, 116, ...>>, {:bar, 1}}
    iex(2)> Foo.bar(1)
    2
    
    iex(3)> import Foo
    Foo
    iex(4)> bar(1)
    2
Yup, like I said, not as ergonomic as Clojure.

In clojure, it is:

    user> (defn bar [x] (+ x 1))
If you want it inside a namespace named foo, then it is:

    user> (in-ns 'foo)
    foo> (defn bar [x] (+ x 1))
I agree Clojure has a better REPL experience than Elixir. But in Elixir REPL for a quick and dirty function you'd probably just do

    >bar = fn x -> x + 1 end
The most unfortunate thing though is then you have to have a different calling convention. Its the greatest flaw in Elixir by far but they didn't have a reasonable alternative given the constraints of Erlang.

Still for defining a named function in the REPL its the same in Haskell and most other languages I believe that you can't define a new named function or add a function to a module in the REPL, though at least in Haskell the calling convention for a variable bound to a lambda is the same as for a named top-level function. LISPs have always had a different notion of how the REPL integrates into the development experience of a running program, and I don't think its really been replicated elsewhere.

Or maybe go with the shorter anonymous function syntax:

bar = &(&1 + 1)

If we're playing code golf, then in Clojure it is:

    (def bar #(+ % 1))
or even

    (def bar inc)
but the difference is that in Clojure, all of these are `IFn`s, and have the same calling syntax, unlike Elixir.
You may be "playing code golf", but I generally just use the capture syntax where the compactness aids readability. For example, a function that takes two arguments and returns their product could be written as

fn x, y -> x * y end

or

&(&1 * &2)

When used inside a map or reduce or when the function is a direct mathematical operation on its arguments, it can be a bit quicker to parse the capture syntax than the fn ... end syntax.