msimcoe

msimcoe

The code below works find but there are some things about it that I don’t understand. The only other language I know is Ruby and the order of execution is top to bottom so I’m confused by the ordering on lines 5 and 6. Also, what purpose does ‘names’ serve on the fourth line.

defmodule Greeter do
	def hello(names, country \\ "en") 
  	def hello(names, country) when is_list(names) do
	    names
	    |> Enum.join(", ")
	    |> hello(country)
  	end

  	def hello(name, country) when is_binary(name) do
  		phrase(country) <> name
  	end

  	defp phrase("en"), do: "Hello, "
 	defp phrase("es"), do: "Hola, "
end

Showing Posts 1 to 10

Insanitariume

Insanitariume

That names is your argument that gets passed on as a first argument to the next operation.
Elixir is all about transformation and we use those pipelines |> for better readability and it’s an elixir idiom.

So those lines (4 to 6) could be rewritten as:
hello(country, (Enum.join(names, ", ")))

msimcoe

msimcoe OP

Thank you. That makes sense to me. I have only been studying Elixir for less than 2 weeks so I have a ways to go. Again, thanks.

themarlzy

themarlzy

6 months into Elixir and I find it very hard to go back to Ruby, without its pipeline operator.

I found Dave Thomas’ book particularly stressed the pjpeline and how it forms your thinking very well. That is, the syntax isn’t mere sugar, it changes the way you think about transformations of data.

M

mdrozdziel

mdrozdziel

I wonder how hard would it be to port the pipeline operator to Ruby. :wink:

pablo

pablo

There are at least least two approaches:

This one uses the pipe | operator.
And Chainable Methods which creates a proxy object that emulates the pipe.

To be quite honest, both approaches feel too hackish, and I wouldn’t recommend anyone to use them on anything more important than a PoC. I do see a lot of value in adding the pipe operator to a future version of the language, though.

Qqwy

Qqwy

TypeCheck Core Team

:wink:

NobbZ

NobbZ

Since a Prof of mine explained the difference between OOP and imperative programming briefly as “the difference is the position of the first parameter”.

Having this in mind, one can think of the . as an alternative of elixirs pipe.

Also paradigms in OOP aren’t about transforming immutable data, but transforming mutable objects, so piping as in elixir might (semantically) not even make sense.

Last but not least, the methods you are calling needs to support chaining into another, but thats the case in elixir anyway. If a method only transforms self and returns Nil you can’t chain it. If a function in elixir just sends a message to modify thats process state and returns a simple :ok you can’t pipe that any further in a meaningful way.

jwarlander

jwarlander

@msimcoe - the pipe operator certainly allows for a really nice, straightforward way to model simple chains of function calls, but it’s good to be aware that there are better options if your needs are more complex :slight_smile:

Strictly speaking, using pipes, you could of course handle things like a simple :ok results etc (that @NobbZ mentioned above) by crafting your functions to handle the outputs from earlier in the pipeline:

defmodule Monitor do
  def poll() do
    # Let's simulate getting some metric from a device..
    x = :rand.uniform(15)
    if x <= 12 do
      {:ok, x}
    else
      {:error, :device_failure}
    end
  end

  def check({:ok, x}) when is_integer(x) and x < 10, do: {:ok, x}
  def check({:ok, x}) when is_integer(x) and x >= 10, do: {:warning, x}
  def check({:error, _reason} = error), do: error

  def output({:ok, x}), do: IO.puts("All is ok: #{x}")
  def output({:warning, x}), do: IO.puts("WARNING: Check your x, it's getting a bit high: #{x}")
  def output({:error, reason}), do: IO.puts("ERROR: #{reason}")

  def report() do
    poll() |> check() |> output()
  end
end

iex(3)> Monitor.report()
WARNING: Check your x, it's getting a bit high: 11
:ok
iex(4)> Monitor.report()
All is ok: 9
:ok
iex(5)> Monitor.report()
ERROR: device_failure
:ok

The above functions, however, are highly coupled to each other, and most likely not useful outside their context, even if they happen to do something that you could otherwise have use for. You might be better served by using with in a more complex chain like this:

defmodule Monitor do
  def poll() do
    # Let's simulate getting some metric from a device..
    x = :rand.uniform(15)
    if x <= 12 do
      {:ok, x}
    else
      {:error, :device_failure}
    end
  end

  def check(x) when is_integer(x) and x < 10, do: {:ok, x}
  def check(x) when is_integer(x) and x >= 10, do: {:warning, x}

  def report() do
    with {:ok, x} <- poll(),
         {:ok, x} <- check(x) do
      IO.puts("All is ok: #{x}")
    else
      {:warning, x} -> IO.puts("WARNING: Check your x, it's getting a bit high: #{x}")
      {:error, reason} -> IO.puts("ERROR: #{reason}")
    end
  end
end

Now each function has only its own purpose to worry about, and report ties it all together, handling the different responses that are possible, while still making the flow clear.

NobbZ

NobbZ

Yeah, you can handle that :ok which you get in any case from my described function, but you can not handle the stuff happened at the other process state-data. Thats what I meant.

Remember: this :ok only tells you that sending a message to another process has been successful, NOT that the effectful operation on the other threads data has been successfull. It doesn’t even tell you which process you have send a message to…

It is just an atom, which by consensus means that some arbitrary operation has been successful, but you don’t have enough context to decide which one in this special case.

jwarlander

jwarlander

Fair enough :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews