Skip to content

Instantly share code, notes, and snippets.

@octosteve
Last active December 15, 2015 04:26
Show Gist options
  • Save octosteve/29be748361e84e3b25bd to your computer and use it in GitHub Desktop.
Save octosteve/29be748361e84e3b25bd to your computer and use it in GitHub Desktop.
defmodule Paralindrome do
def are_palindromes?(list) do
parent_process = self
Enum.map(list, fn (word) ->
spawn(fn ->
send parent_process, {self, word, is_palindrome?(word) }
end)
end)
|> Enum.each fn (process_id) ->
receive do
{^process_id, word, status} ->
IO.puts "#{word} is a Palindrome: #{status}"
end
end
end
def is_palindrome?(word) do
word == String.reverse(word)
end
end
Paralindrome.are_palindromes?(["mom", "hat"])
defmodule Paralindrome do
def are_palindromes?(list) do
Enum.map(list, fn (word) ->
Task.async(fn -> {word, is_palindrome?(word) } end)
end)
|> Enum.each fn (task) ->
case Task.await(task) do
{word, true} ->
IO.puts "#{word} is a Palindrome"
_ -> nil
end
end
end
def is_palindrome?(word) do
word == String.reverse(word)
end
end
list = File.read!("/usr/share/dict/words") |> String.split("\n")
Paralindrome.are_palindromes?(list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment