Skip to content

Instantly share code, notes, and snippets.

@mijoharas
Created November 16, 2015 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mijoharas/99cd06827ab9ef9fe04d to your computer and use it in GitHub Desktop.
Save mijoharas/99cd06827ab9ef9fe04d to your computer and use it in GitHub Desktop.
defmodule PuzzledPintScriptElixir do
HTTPotion.start
defp get_file_strings do
File.read! "/usr/share/dict/cracklib-small"
end
def list_of_urls do
base_string = "http://www.puzzledpint.com/puzzles/august-2015/semaphore/"
list_of_words = get_file_strings
list_of_lines = String.split(list_of_words, "\n", trim: true)
filtered_words = Enum.filter(list_of_lines, fn x -> String.length(x) == 7 end)
Enum.map(filtered_words, fn x -> base_string <> x end)
end
def pmap_printer_func(collection, fun, condition_func) do
me = self
collection
|> Enum.map(fn (elem) ->
:timer.sleep(200)
spawn_link fn -> (send me, {self, [ fun.(elem), elem ] }) end
end)
|> Enum.map(fn (pid) ->
receive do {^pid, result} ->
if condition_func.(result[0]) do
IO.puts(result[1])
end
result end
end)
end
def getter_func(url) do
IO.puts url
HTTPotion.get url
end
def condition_function(response) do
response.status_code == 404
end
def main do
pmap_printer_func(list_of_urls, &getter_func/1, &condition_function/1)
end
end
@mijoharas
Copy link
Author

Timeouts in code:

=ERROR REPORT==== 16-Nov-2015::22:43:23 ===
Error in process <0.244.0> with exit value:
{#{'__exception__' => true,
   '__struct__' => 'Elixir.HTTPotion.HTTPError',
   message => <<"req_timedout">>},
 [{'Elixir.HTTPotion',handle_response,1,
      [{file,"lib/httpotion.ex"},{line,209}]},
  {'Elixir.PuzzledPintScriptElixir','-pmap_printer_func/3-fun-0-',3,
      [{file,"lib/puzzled_pint_script_elixir.ex"},{line,20}]}]}
** (EXIT from #PID<0.213.0>) an exception was raised:
    ** (HTTPotion.HTTPError) req_timedout
        (httpotion) lib/httpotion.ex:209: HTTPotion.handle_response/1
        (puzzled_pint_script_elixir) lib/puzzled_pint_script_elixir.ex:20: anonymous fn/3 in PuzzledPintScriptElixir.pmap_printer_func/3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment