Skip to content

Commit

Permalink
small performance patch
Browse files Browse the repository at this point in the history
  • Loading branch information
cdetroye committed Mar 26, 2018
1 parent f960cad commit 6b4b882
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 17 deletions.
2 changes: 1 addition & 1 deletion data/benvolios/benvolios.dat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#{<<"cdetroye">> => <<"niks">>}.
#{<<"cdetroye">> => <<"foo">>}.
19 changes: 3 additions & 16 deletions lib/bot/benvolioss.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ defmodule Plugin.Benvolios do
# Compute all permutations of the first word in the string.
# if it matches "order", turn it into "order".
words = String.split(text)

perms =
words |> hd |> String.to_charlist() |> shuffle() |> Enum.map(&String.Chars.to_string/1)

match? = Enum.any?(perms, &String.equivalent?("order", &1))
command = hd(words)
chars = String.to_charlist(command)
match? = ("order" |> String.to_charlist() |> Enum.sort) == (words |> hd |> String.to_charlist() |> Enum.sort())

text =
if match? do
Expand Down Expand Up @@ -141,15 +139,4 @@ defmodule Plugin.Benvolios do
defp handle_order(orderer, order) do
Brain.Benvolios.save_order(orderer, order)
end

# https://stackoverflow.com/questions/33756396/how-can-i-get-permutations-of-a-list
def shuffle(list), do: shuffle(list, length(list))
def shuffle([], _), do: [[]]
def shuffle(_, 0), do: [[]]

def shuffle(list, i) do
for x <- list,
y <- shuffle(list, i - 1),
do: [x | y]
end
end

0 comments on commit 6b4b882

Please sign in to comment.