Last active
February 10, 2023 20:49
-
-
Save rodrigues/ccd8115c20944d0adb42a07aa017d972 to your computer and use it in GitHub Desktop.
This file contains 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 TTTMap do | |
def win?(%{s1: x, s2: x, s3: x}) when not is_nil(x), do: true | |
def win?(%{s4: x, s5: x, s6: x}) when not is_nil(x), do: true | |
def win?(%{s7: x, s8: x, s9: x}) when not is_nil(x), do: true | |
def win?(%{s1: x, s4: x, s7: x}) when not is_nil(x), do: true | |
def win?(%{s2: x, s5: x, s8: x}) when not is_nil(x), do: true | |
def win?(%{s3: x, s6: x, s9: x}) when not is_nil(x), do: true | |
def win?(%{s1: x, s5: x, s9: x}) when not is_nil(x), do: true | |
def win?(%{s3: x, s5: x, s7: x}) when not is_nil(x), do: true | |
def win?(_), do: false | |
end | |
defmodule TTTTuple do | |
def win?({x, x, x, _, _, _, _, _, _}) when not is_nil(x), do: true | |
def win?({_, _, _, x, x, x, _, _, _}) when not is_nil(x), do: true | |
def win?({_, _, _, _, _, _, x, x, x}) when not is_nil(x), do: true | |
def win?({x, _, _, x, _, _, x, _, _}) when not is_nil(x), do: true | |
def win?({_, x, _, _, x, _, _, x, _}) when not is_nil(x), do: true | |
def win?({_, _, x, _, _, x, _, _, x}) when not is_nil(x), do: true | |
def win?({x, _, _, _, x, _, _, _, x}) when not is_nil(x), do: true | |
def win?({_, _, x, _, x, _, x, _, _}) when not is_nil(x), do: true | |
def win?(_), do: false | |
end | |
defmodule TTTList do | |
def win?([x, x, x, _, _, _, _, _, _]) when not is_nil(x), do: true | |
def win?([_, _, _, x, x, x, _, _, _]) when not is_nil(x), do: true | |
def win?([_, _, _, _, _, _, x, x, x]) when not is_nil(x), do: true | |
def win?([x, _, _, x, _, _, x, _, _]) when not is_nil(x), do: true | |
def win?([_, x, _, _, x, _, _, x, _]) when not is_nil(x), do: true | |
def win?([_, _, x, _, _, x, _, _, x]) when not is_nil(x), do: true | |
def win?([x, _, _, _, x, _, _, _, x]) when not is_nil(x), do: true | |
def win?([_, _, x, _, x, _, x, _, _]) when not is_nil(x), do: true | |
def win?(_), do: false | |
end | |
opts = [nil, :o, :x] | |
tuple_items = | |
for s1 <- opts, | |
s2 <- opts, | |
s3 <- opts, | |
s4 <- opts, | |
s5 <- opts, | |
s6 <- opts, | |
s7 <- opts, | |
s8 <- opts, | |
s9 <- opts do | |
{s1, s2, s3, s4, s5, s6, s7, s8, s9} | |
end | |
list_items = Enum.map(tuple_items, &Tuple.to_list/1) | |
map_items = | |
for {s1, s2, s3, s4, s5, s6, s7, s8, s9} <- tuple_items do | |
%{s1: s1, s2: s2, s3: s3, s4: s4, s5: s5, s6: s6, s7: s7, s8: s8, s9: s9} | |
end | |
Benchee.run( | |
%{ | |
"list" => fn -> Enum.each(list_items, &TTTList.win?/1) end, | |
"tuple" => fn -> Enum.each(tuple_items, &TTTTuple.win?/1) end, | |
"map" => fn -> Enum.each(map_items, &TTTMap.win?/1) end | |
}, | |
warmup: 5, | |
time: 10 | |
) |
Author
rodrigues
commented
Aug 21, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment