Last active
March 15, 2021 12:45
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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