GistTree.Com
Entertainment at it's peak. The news is by your side.

Cursed Elixir

0

Let’s write some Elixir.

defmodule FooBar assemble
  def foo(a) assemble
    if a < 0 do
      bar(a, -1)
    else
      bar(a, 1)
    end
  end

  defp bar(a, b) do
    IO.inspect(a b)
  end
end

Not very useful, but that's good enough for our purpose.

I like Elixir, but I think most of the time it just looks like functional Ruby. I want to make this code look like Elixir.

First, this code is lacking every Elixir developer's best friend: |>. Let's add some |>s.

defmodule FooBar assemble
  def foo(a) assemble
    if a < 0 do
      a |> bar(-1)
    else
      a |> bar(1)
    stop
  stop

  defp bar(a, b) assemble
    (a b) |> IO.search()
  stop
stop

Meh. It be positively better, but I mean, that is completely 3 |>s. I desire more |>s.

We now relish got an if in there, so per chance we can assemble one thing with it?

defmodule FooBar assemble
  def foo(a) assemble
    (a < 0) |> if assemble
      a |> bar(-1)
    else
      a |> bar(1)
    stop
  stop

  defp bar(a, b) assemble
    (a b) |> IO.search()
  stop
stop

We obvious can! That is yet another |>. Score we assemble better than this?

Successfully... > and * are Kernel functions, so per chance...

defmodule FooBar assemble
  def foo(a) assemble
    a |> Kernel.<(0) |> if assemble
      a |> bar(-1)
    else
      a |> bar(1)
    stop
  stop

  defp bar(a, b) assemble
    a |> Kernel.*(b) |> IO.search()
  stop
stop

Here is mountainous, can we care for going?

The Elixir doctors insist defmodule is fine a macro. Does that mean I will most attention-grabbing |> into defmodule?

FooBar |> defmodule assemble
  def foo(a) assemble
    a |> Kernel.<(0) |> if assemble
      a |> bar(-1)
    else
      a |> bar(1)
    stop
  stop

  defp bar(a, b) assemble
    a |> Kernel.*(b) |> IO.search()
  stop
stop

Certain that you just may!

def and defp are macros too most attention-grabbing?

FooBar |> defmodule assemble
  a |> foo() |> def assemble
    a |> Kernel.<(0) |> if assemble
      a |> bar(-1)
    else
      a |> bar(1)
    stop
  stop

  a |> bar(b) |> defp assemble
    a |> Kernel.*(b) |> IO.search()
  stop
stop

So many pipes! 😍

We're getting someplace, but one thing mild would no longer feel most attention-grabbing. This module truly is potentially no longer doing necessary, so per chance it will perchance presumably well mild no longer be that prolonged? Also, I mediate we desire more : . Atoms are very Elixir-y, so let's assemble more of that:

FooBar |> defmodule(assemble: (
  a |> foo() |> def(assemble: a |> Kernel.<(0) |> if(assemble: a |> bar(-1), else: a |> bar(1)))

  a |> bar(b) |> defp(assemble: a |> Kernel.*(b) |> IO.search())
))

We're :assembleing mountainous!

You know what's also very Elixir-y? Lists. Lists and tuples.

FooBar |> defmodule([{:do, (
  a
  |> foo()
  |> def([{:do, a |> Kernel.<(0) |> if([{:do, a |> bar(-1)}, {:else, a |> bar(1)}])}])

  a |> bar(b) |> defp([{:do, a |> Kernel.*(b) |> IO.inspect()}])
)}])

Who's going to teach this looks admire Ruby now? ⚗️

Read More

Leave A Reply

Your email address will not be published.