Skip to content

Instantly share code, notes, and snippets.

@alfredbaudisch
Last active March 15, 2021 12:45
Show Gist options
  • Save alfredbaudisch/908febf6e63826155f4ddf900e5f1a8f to your computer and use it in GitHub Desktop.
Save alfredbaudisch/908febf6e63826155f4ddf900e5f1a8f to your computer and use it in GitHub Desktop.
Elixir: returning the value of a map or struct field using an anonymous fun with a pipe (syntax sugar)
defmodule Company do
schema "companies" do
field(:name, :string)
field(:email, :string)
end
end
from(c in Company, where: c.name == "foo")
|> Repo.one()
|> (& &1.email).()
# This one doesn't make much sense, but it also works:
map = %{foo: "a"}
# %{foo: "a"}
map |> Map.get(:foo)
# "a"
map |> (& &1.foo).()
# "a"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment