Skip to content

Instantly share code, notes, and snippets.

View rugyoga's full-sized avatar

Guy Argo rugyoga

View GitHub Profile
@rugyoga
rugyoga / palindrome.ex
Last active July 21, 2022 08:53
Find the 100th palindromic number that is divisible by 3
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)
@rugyoga
rugyoga / controller.ex
Last active July 21, 2022 20:01
Sample controller leveraging ElasticSearch.stream, chunker and S3.upload
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
@rugyoga
rugyoga / palindrome2.ex
Last active August 4, 2022 17:56
Simpler version for presentation
1
|> Stream.iterate(increment)
|> Stream.filter(palindrome?)
|> Stream.filter(divisible_by_3?)
|> Enum.at(99)
@rugyoga
rugyoga / delta_type.ex
Created August 17, 2022 00:15
Types for deltas
@type delta() :: {:update, any(), any()} | {:delete, any()} | {:add, any()}
@type path() :: list()
@type delta_spec() :: [{path(), delta()}]
@rugyoga
rugyoga / delta.ex
Created September 4, 2022 21:42
Main entry point for difference engine
@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
@rugyoga
rugyoga / delta3.ex
Created September 4, 2022 21:47
Three argument version of delta.
@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)
@rugyoga
rugyoga / delta_struct.ex
Created September 4, 2022 21:49
Take delta of two structs
@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))
@rugyoga
rugyoga / delta_map.ex
Created September 4, 2022 21:56
Take the delta of two maps
@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) ++
@rugyoga
rugyoga / delta_list.ex
Last active September 4, 2022 23:13
Take delta of two lists
@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)
@rugyoga
rugyoga / delta_tuple.ex
Created September 4, 2022 22:22
Take the delta of two tuples
@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))