def boundary do:crypto.rand_bytes(8)|> Base.encode16|> &("--------FormDataBoundary" <> &1)end
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Such "sugar" on top of pipes can lead to wrong understanding of what pipe is. Right now it works perfectly like this:"hello" |> (fn txt -> IO.puts(txt) end).() # full syntax"hello" |> (&(IO.puts(&1))).() # all parentheses"hello" |> (&IO.puts&1).() # shorthand
On Tue, Jul 8, 2014 at 2:08 PM, Kirill Romanov <o...@aleph0.cc> wrote:
Such "sugar" on top of pipes can lead to wrong understanding of what pipe is. Right now it works perfectly like this:"hello" |> (fn txt -> IO.puts(txt) end).() # full syntax"hello" |> (&(IO.puts(&1))).() # all parentheses"hello" |> (&IO.puts&1).() # shorthandIs there any possible meaning for:"hello" |> &IO.puts(&1)?
This comes up every once in a while... and a lot of people starting with the language seem to expect it to work.So let's give it another try: should the following work?def boundary do:crypto.rand_bytes(8)|> Base.encode16|> &("--------FormDataBoundary" <> &1)endAdvantages:1. Easy piping elsewhere2. Apparently it is obvious to many people (please reply if you disagree)Disadvantages:1. If the function is defined outside of the pipe, we need to explicitly invoked it with .()Thoughts?
José ValimSkype: jv.ptecFounder and Lead Developer
at the risk of being painfully obvious, this works:
at the risk of being painfully obvious, this works::crypto.rand_bytes(8)|> Base.encode16|> (&("--------FormDataBoundary" <> &1)).()
On Jul 8, 2014, at 3:24 PM, José Valim <jose....@plataformatec.com.br> wrote:This comes up every once in a while... and a lot of people starting with the language seem to expect it to work.So let's give it another try: should the following work?def boundary do:crypto.rand_bytes(8)|> Base.encode16|> &("--------FormDataBoundary" <> &1)endAdvantages:1. Easy piping elsewhere2. Apparently it is obvious to many people (please reply if you disagree)Disadvantages:1. If the function is defined outside of the pipe, we need to explicitly invoked it with .()Thoughts?at the risk of being painfully obvious, this works::crypto.rand_bytes(8)|> Base.encode16 |>|> (&("--------FormDataBoundary" <> &1)).()Clojure has a similar problem with its threading macros and hasn’t done anything to deal with it (in Clojure it is a weird ((+ 1)) kind of thing you write).
Your timing of this thread is excellent :-)
# intermediary value
def encrypt_password(user, password) do
encrypted = password
|> Salt.add
|> BCrypt.hash
put_in(user[:hashed_password], encrypted)
|> EctoOrSomething.UpdateIGuess.ImStillNewHere
end
# anonymous function wrap
def encrypt_password(user, password) do
password
|> Salt.add
|> BCrypt.hash
|> (fn(encrypted) -> put_in(user[:hashed_password], encrypted) end).()
# or |> (&(put_in(user[:hashed_password], &1)).()
# but considerably messy either way |> ...
end
good_status = "200"
{^good_status, response} = fetch("www.example.com")
# expands to {"200", response} = fetch("www.example.com")
def encrypt_password(user, password) do
password
|> Salt.add
|> BCrypt.hash
|^ put_in(user[:hashed_password], ^)
|> ...
# expands to ...(put_in(user[:hashed_password], BCrypt.hash(Salt.add(password)))
end
# more contrived examples!!
> map = %{little_bunny: %{}}
> :foo
|^ put_in(map[:little_bunny][^], ^)
%{little_bunny: %{foo: :foo}}
> "Ton" |^ (^<>"y! " <> ^<>"i! " <> ^<>"é!")
"Tony! Toni! Toné!"
thing |> a_function
a_function(thing)
def encrypt_password(user, password) do
encrypted = password
|> Salt.add
|> BCrypt.hash
put_in(user[:hashed_password], encrypted)
|> EctoLike.update
end
def encrypt_password(user, password) do
put_in(user[:hashed_password], encrypt(password))
|> EctoLike.update
end
defp encrypt(password) do
password
|> Salt.add
|> BCrypt.hash
end
defmodule Words do def count("" <> string), do: count(parse(string), %{})
defp count([], map), do: map defp count(["" | words], map), do: count(words, map) defp count([word | words], map) do word = word |> String.replace("_", "-") count(words, put_in(map[word], (map[word] || 0) + 1)) end
# strip underscores, retain hyphens defp parse(string), do: string |> String.downcase |> String.replace("-", "&hyph;") |> String.replace("_", "-") |> String.replace("&hyph;", "_") |> String.split(~r/\W/u)end
defp count([], map), do: map defp count(["" | words], map), do: count(words, map) defp count([word | words], map), do: count(words, increment(map, word))
defp increment(map, word)
word = word |> String.replace("_", "-")
put_in(map[word], (map[word] || 0) + 1)
end
defmodule Words do def count("" <> string), do: parse(string) |> Enum.reduce(%{}, &count/2)
defp count("", map), do: map
defp count("" <> word, map) do
word |> String.replace("_", "-")
|> increment(map) end
defp increment(word, map), do: put_in(map[word], (map[word] || 0) + 1)
defp parse(string), do: string |> String.downcase |> String.replace("-", "&hyph;") |> String.replace("_", "-") |> String.replace("&hyph;", "_") |> String.split(~r/\W/u)end
--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/dwbNOh_yNd4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/59c6d90d-ded2-4794-8a73-920ab9350007%40googlegroups.com.
def boundary do
sha = :crypto.rand_bytes(8)
|> Base.encode(16)
"---------FormDataBoundary" <> sha
end
def boundary do
"--------FormDataBoundary" <> (:crypto.rand_bytes(8)
|> Base.encode(16))
end # sha, you know what? uh-uh.
def boundary, do: :crypto.rand_bytes(8) |> Base.encode(16) |> boundary
defp boundary(sha), do: "--------FormDataBoundary" <> sha
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/59c6d90d-ded2-4794-8a73-920ab9350007%40googlegroups.com.
Sorry to dredge up an old thread but I wanted to push back against this again. The point of the pipe operator is to be able to clearly express the flow of data through a transformation. Claiming the pipe operator shouldn't be able to handle captured or anonymous functions because it confuses how it works is confusing an implementation detail of how the pipe operator macro works now to how it is semantically expected to behave. Having to wrap them in parenthesis and an invocation introduces a great deal of noise to what could be an extremely simple syntax. As Wojtek Mach showed, it's fairly easy to get the pipe operator to handle these cases, especially since you're pattern matching against them anyway to give error messages. Why pattern match only to give an error when it's easy and straightforward to just make it work, what else could a user intend other than to call the function?. There's a pull request here I made than extends his work so that captured functions and anonymous functions both work. https://github.com/wojtekmach/pipe_capture/pull/1--"Hello"
|> fn x -> IO.puts x end
|> &IO.puts/1
|> &IO.puts(:stderr, &1)
vs
"Hello"
|> (fn x -> IO.puts x; x end).()
|> (&IO.puts/1).()
|> (&IO.puts(:stderr, &1)).()
It seems weird to me that the pipe macro works exclusively on already invoked functions and doesn't know what to do with a function reference.
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/b2182e21-939e-4762-b603-9cd492e1650b%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CABu8xFDB7oMcaXNfrYbkh%3DrLz7YcZMRzcuODjHWaJKp5DkNxZw%40mail.gmail.com.