NoMethodError (undefined method for nil)input
|> (&(&1 + 1)).()from p in Post,
left_join: c in Comment,
select: %{p | comments_count: count(c.id)},
group_by: p.id
order: [desc: p.published_at](1..1_000).reduce({}) do |acc, x|
acc.merge(x => 1)
end
(1..1_000).reduce({}) do |acc, x|
acc[x] = 1
acc
endArchitecture the Lost Years by Robert Martin - 2011
def foo(arg) do
{:ok, val} = bar.call(baz, arg)
...
enddefmodule MyApp.Foo do
@enforce_keys [:bar]
defstruct [:foo, :bar]
@spec call(%__MODULE__, number) :: %__MODULE__
def call(%__MODULE__, val) do
...
end
end
%MyApp.Foo{bar: bar} = baz()It's probably not too important, in the grand scheme of things, whether you are using a language like Erlang or not. While I do feel it's under-used and under-rated, the biggest benefit of it is not in running a system that uses it. The biggest benefit comes from learning about the fundamentals of solid system design and internalizing its lessons in a practical context.
class FooAbstract
def foo
raise "Not Implemented Yet"
end
enddefmodule Foo do
@callback foo(String.t) :: {:ok, term}
end
defmodule Bar do
@behaviour Foo
@impl Foo
def foo(str) do
{:ok, :foo}
end
endmodule Countable
def size
if respond_to?(:count)
count
else
amount
end
end
enddefprotocol Countable do
def count(input)
end
defimpl Countable, for: Foo do
def count(foo), do: Foo.size(foo)
end
defimpl Countable, for: Bar do
def count(bar), do: Bar.amount(bar)
end