Created
November 21, 2016 15:48
-
-
Save paulohp/d07e04b59c15c791ac0a8019034c68ef to your computer and use it in GitHub Desktop.
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 Identicon do | |
def main(input) do | |
input | |
|> hash_input | |
|> pick_color | |
|> build_grid | |
end | |
def build_grid(%Identicon.Image{hex: hex} = image) do | |
grid = | |
hex | |
|> Enum.chunk(3) | |
|> Enum.map(&mirror_row/1) | |
|> List.flatten | |
|> Enum.with_index | |
%Identicon.Image{image | grid: grid} | |
end | |
def mirror_row(row) do | |
[first, second | _tail] = row | |
row ++ [second, first] | |
end | |
def pick_color(%Identicon.Image{hex: [r, g, b | _tail]} = image) do | |
%Identicon.Image{image | color: {r, g, b}} | |
end | |
def hash_input(input) do | |
hex = :crypto.hash(:md5, input) | |
|> :binary.bin_to_list | |
%Identicon.Image{hex: hex} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment