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
1 | |
|> Stream.iterate(fn n -> n + 1 end) | |
|> Stream.filter(fn n -> s = Integer.to_string(n); s == String.reverse(s) end) | |
|> Stream.filter(fn n -> rem(n, 3) == 0 end) | |
|> Enum.at(99) |
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 Controller do | |
@four_megabytes 4*1024*1024 | |
def find_all(params) do | |
query(params) |> ElasticSearch.stream(params.index, &consumer(params, &1)) | |
end | |
def query(params) do | |
%{term: %{params.query_term => params.query_value}} | |
end |
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
1 | |
|> Stream.iterate(increment) | |
|> Stream.filter(palindrome?) | |
|> Stream.filter(divisible_by_3?) | |
|> Enum.at(99) |
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
@type delta() :: {:update, any(), any()} | {:delete, any()} | {:add, any()} | |
@type path() :: list() | |
@type delta_spec() :: [{path(), delta()}] |
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
@spec delta(any(), any()) :: delta_spec() | |
def delta(a, b) do | |
[] | |
|> delta(a, b) | |
|> List.flatten() | |
|> Enum.map(fn {path, element} -> {Enum.reverse(path), element} end) | |
|> Enum.sort_by(fn {path, _} -> {length(path), path} end) | |
end |
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
@spec delta(path(), any(), any()) :: delta_spec() | |
defp delta(path, a, b) when is_struct(a) and is_struct(b), do: delta_struct(path, a, b) | |
defp delta(path, a, b) when is_map(a) and is_map(b), do: delta_map(path, a, b) | |
defp delta(path, a, b) when is_list(a) and is_list(b), do: delta_list(path, a, b) | |
defp delta(path, a, b) when is_tuple(a) and is_tuple(b), do: delta_tuple(path, a, b) | |
defp delta(path, a, b), do: delta_simple(path, a, b) |
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
@spec delta_struct(path(), struct(), struct()) :: delta_spec() | |
def delta_struct(path, %a_s{} = a, %b_s{} = b) when a_s != b_s, do: delta_simple(path, a, b) | |
def delta_struct(path, a, b), do: delta_map(path, Map.from_struct(a), Map.from_struct(b)) |
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
@spec delta_map(path(), map(), map()) :: [delta()] | |
def delta_map(path, a, b) do | |
a_keys = MapSet.new(Map.keys(a)) | |
b_keys = MapSet.new(Map.keys(b)) | |
common = MapSet.intersection(a_keys, b_keys) | |
a_only = a_keys |> MapSet.difference(common) | |
b_only = b_keys |> MapSet.difference(common) | |
Enum.flat_map(common, fn key -> delta([key | path], a[key], b[key]) end) ++ | |
Enum.flat_map(a_only, fn key -> [{[key | path], {:delete, a[key]}}] end) ++ |
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
@spec delta_list(path(), list(), list()) :: [delta()] | |
defp delta_list(path, as, bs) when length(as) != length(bs), do: delta_simple(path, as, bs) | |
defp delta_list(path, as, bs) do | |
as | |
|> indexed_zip(bs) | |
|> Enum.flat_map(fn {i, {a, b}} -> delta([i | path], a, b) end) | |
end | |
def indexed_zip(as, bs, i \\ 0) |
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
@spec delta_tuple(path(), tuple(), tuple()) :: [delta()] | |
defp delta_tuple(path, a, b) when tuple_size(a) != tuple_size(b), do: delta_simple(path, a, b) | |
defp delta_tuple(path, a, b), do: delta_list(path, Tuple.to_list(a), Tuple.to_list(b)) |