How do i access the left hand argument of |> (Like in groovy the default arg of closure is "it")

iex1> IO.gets(“what is your name?”) |> String.trim |> IO.puts “Hello #{???}”

Hello and welcome,

You might do like this

iex> koko = & IO.puts("Hello #{&1}")
#Function<7.91303403/1 in :erl_eval.expr/5>
iex> IO.gets("what is your name?") |> String.trim |> koko.()
what is your name?koko
Hello koko
:ok

This is a shortcut for

fn string -> IO.puts("Hello #{string}") end

Thanks a lot. This works
iex(1)> “my name” |> (fn string -> IO.puts(“Hello #{string}”) end).()
Hello my name
:ok

and this
iex(2)> “hello” |> (& IO.puts(“Hey and #{&1}”)).()
Hey and hello
:ok

Thanks a lot :+1:

I find that viewing the pipe as just another form of Fluent interface is too limiting.

Interactive Elixir (1.9.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> input = fn x -> {x, IO.gets("#{x}, what is your name? ")} end 
#Function<7.91303403/1 in :erl_eval.expr/5>
iex(2)> output = fn x -> IO.puts(x) end
#Function<7.91303403/1 in :erl_eval.expr/5>
iex(3)> process = fn {x, y} -> "#{x} #{String.trim(y)}" end
#Function<7.91303403/1 in :erl_eval.expr/5>
iex(4)> compose = fn f, g -> (fn x -> f.(x) |> g.() end) end
#Function<13.91303403/2 in :erl_eval.expr/5>
iex(5)> b = compose.(process, output)
#Function<7.91303403/1 in :erl_eval.expr/5>
iex(6)> a = compose.(input, b)
#Function<7.91303403/1 in :erl_eval.expr/5>
iex(7)> a.("Hello")
Hello, what is your name? Dave
Hello Dave
:ok
iex(8)>
1 Like

You all probably find this discussion https://groups.google.com/forum/#!topic/elixir-lang-core/JVFGZm58Qo0 interesting.

Interactive Elixir (1.9.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> defmodule InEffect do
...(1)>   def acquire_name(salutation),
...(1)>     do: {salutation, IO.gets("#{salutation}, what is your name? ")}
...(1)> end
{:module, InEffect,
 <<70, 79, 82, 49, 0, 0, 5, 64, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 180,
   0, 0, 0, 17, 15, 69, 108, 105, 120, 105, 114, 46, 73, 110, 69, 102, 102, 101,
   99, 116, 8, 95, 95, 105, 110, 102, 111, ...>>, {:acquire_name, 1}}
iex(2)> 
nil
iex(3)> defmodule OutEffect do
...(3)>   def greet(greeting),
...(3)>     do: IO.puts(greeting)
...(3)> end
{:module, OutEffect,
 <<70, 79, 82, 49, 0, 0, 4, 80, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 144,
   0, 0, 0, 15, 16, 69, 108, 105, 120, 105, 114, 46, 79, 117, 116, 69, 102, 102,
   101, 99, 116, 8, 95, 95, 105, 110, 102, ...>>, {:greet, 1}}
iex(4)> 
nil
iex(5)> defmodule Transformation do
...(5)>   def make_greeting({salutation, name}),
...(5)>     do: "#{salutation} #{String.trim(name)}"
...(5)> end
{:module, Transformation,
 <<70, 79, 82, 49, 0, 0, 5, 144, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 204,
   0, 0, 0, 19, 21, 69, 108, 105, 120, 105, 114, 46, 84, 114, 97, 110, 115, 102,
   111, 114, 109, 97, 116, 105, 111, 110, 8, ...>>, {:make_greeting, 1}}
iex(6)> 
nil
iex(7)> defmodule Composition do
...(7)>   def greet_user_with(salutation) do
...(7)>     salutation
...(7)>     |> InEffect.acquire_name()
...(7)>     |> Transformation.make_greeting()
...(7)>     |> OutEffect.greet()
...(7)>   end
...(7)> end
{:module, Composition,
 <<70, 79, 82, 49, 0, 0, 5, 48, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 229,
   0, 0, 0, 19, 18, 69, 108, 105, 120, 105, 114, 46, 67, 111, 109, 112, 111,
   115, 105, 116, 105, 111, 110, 8, 95, 95, 105, ...>>, {:greet_user_with, 1}}
iex(8)> 
nil
iex(9)> Composition.greet_user_with("Hello")
Hello, what is your name? Dave
Hello Dave
:ok
iex(10)> 

In my opinion anonymous functions are often over/abused.

I think Function.pipe_to/2 would encourage the over/abuse of anonymous functions even more.