a-conjecture-of-mine

An exercise on polyglossy: the same problem solved on multiple languages

Commit
293551db3313a6afac9588b1e56343ec0ebce3a4
Parent
d1ec6e64e95a15e0bccb7374ddd759f070d4619f
Author
Pablo Escobar Gaviria <gark.garcia@protonmail.com>
Date

Removed unnecessary files from the Elixir implementation.

Diffstat

7 files changed, 78 insertions, 138 deletions

Status File Name N° Changes Insertions Deletions
Deleted Elixir/conjecture/.formatter.exs 4 0 4
Deleted Elixir/conjecture/.gitignore 24 0 24
Deleted Elixir/conjecture/lib/conjecture.ex 73 0 73
Deleted Elixir/conjecture/mix.exs 28 0 28
Deleted Elixir/conjecture/test/conjecture_test.exs 8 0 8
Deleted Elixir/conjecture/test/test_helper.exs 1 0 1
Added Elixir/digital_sum.ex 78 78 0
diff --git a/Elixir/conjecture/.formatter.exs b/Elixir/conjecture/.formatter.exs
@@ -1,4 +0,0 @@
-# Used by "mix format"
-[
-  inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
-]
diff --git a/Elixir/conjecture/.gitignore b/Elixir/conjecture/.gitignore
@@ -1,24 +0,0 @@
-# The directory Mix will write compiled artifacts to.
-/_build/
-
-# If you run "mix test --cover", coverage assets end up here.
-/cover/
-
-# The directory Mix downloads your dependencies sources to.
-/deps/
-
-# Where third-party dependencies like ExDoc output generated docs.
-/doc/
-
-# Ignore .fetch files in case you like to edit your project deps locally.
-/.fetch
-
-# If the VM crashes, it generates a dump, let's ignore it too.
-erl_crash.dump
-
-# Also ignore archive artifacts (built via "mix archive.build").
-*.ez
-
-# Ignore package tarball (built via "mix hex.build").
-conjecture-*.tar
-
diff --git a/Elixir/conjecture/lib/conjecture.ex b/Elixir/conjecture/lib/conjecture.ex
@@ -1,73 +0,0 @@
-defmodule Conjecture do
-  INVALID_INPUT = 2
-
-  def main do 
-    case System.argv() |> Enum.map(fn s -> Integer.parse s end) do
-      [{max, ""}, {n_processes, ""}] -> run max, n_processes
-      _ -> System.halt INVALID_INPUT
-    end
-  end
-
-  def run max, n_processes do
-    if max < 0 or n_processes <= 0 do
-      System.halt INVALID_INPUT
-    else
-      # Spawn a new process for each starting index from 0 to ``max`
-      f = fn i -> spawn fn -> counterexpl i, max, n_processes end end
-      pids = Enum.map 0..max, f
-
-      # Send the current PID to each process
-      Enum.map pids, fn pid -> send pid, self() end
-      listen
-    end
-  end
-
-  # Listen for messages on all processes
-  def listen do
-    receive do
-      :found -> System.halt 1
-      :ok -> listen
-    end
-  end
-
-  def counterexpl start, max, n_processes do
-    receive do
-      parent_pid ->
-        if counterexpl_loop start, max, n_processes do
-          send parent_pid, :found
-        else
-          send parent_pid, :ok
-        end
-    end
-  end
-
-  def counterexpl_loop a, max, n_processes do
-    cond do
-      iter a, 0 -> true
-      a + n_processes <= max ->
-        counterexpl_loop (a + n_processes), max, n_processes
-      true -> false
-    end
-  end
-
-  def iter a, b do
-    cond do
-      test a, b -> true
-      b + 1 < a -> iter a, b + 1
-      true -> false
-    end
-  end
-
-  def test a, b do
-    rem (sum (a + b) - sum a - sum b), 9 == 0
-  end
-
-  def sum 0 do 0 end
-
-  def sum n do
-    d = rem n, 10
-    r = div n, 10
-
-    d + sum r
-  end
-end
diff --git a/Elixir/conjecture/mix.exs b/Elixir/conjecture/mix.exs
@@ -1,28 +0,0 @@
-defmodule Conjecture.MixProject do
-  use Mix.Project
-
-  def project do
-    [
-      app: :conjecture,
-      version: "0.1.0",
-      elixir: "~> 1.9",
-      start_permanent: Mix.env() == :prod,
-      deps: deps()
-    ]
-  end
-
-  # Run "mix help compile.app" to learn about applications.
-  def application do
-    [
-      extra_applications: [:logger]
-    ]
-  end
-
-  # Run "mix help deps" to learn about dependencies.
-  defp deps do
-    [
-      # {:dep_from_hexpm, "~> 0.3.0"},
-      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
-    ]
-  end
-end
diff --git a/Elixir/conjecture/test/conjecture_test.exs b/Elixir/conjecture/test/conjecture_test.exs
@@ -1,8 +0,0 @@
-defmodule ConjectureTest do
-  use ExUnit.Case
-  doctest Conjecture
-
-  test "greets the world" do
-    assert Conjecture.hello() == :world
-  end
-end
diff --git a/Elixir/conjecture/test/test_helper.exs b/Elixir/conjecture/test/test_helper.exs
@@ -1 +0,0 @@
-ExUnit.start()
diff --git a/Elixir/digital_sum.ex b/Elixir/digital_sum.ex
@@ -0,0 +1,78 @@
+defmodule Conjecture do
+  def main do 
+    case System.argv() |> Enum.map(fn s -> Integer.parse s end) do
+      [{max, ""}, {n_processes, ""}] -> run max, n_processes
+      [{max, ""}] -> run max, 1
+      _ -> invalid_input()
+    end
+  end
+
+  def run max, n_processes do
+    if max < 0 or n_processes <= 0 do
+      invalid_input()
+    else
+      # Spawn a new process for each starting index from 0 to ``max`
+      f = fn i -> spawn fn -> counterexpl i, max, n_processes end end
+      pids = Enum.map 0..max, f
+
+      # Send the current PID to each process
+      Enum.map pids, fn pid -> send pid, self() end
+      listen n_processes
+    end
+  end
+
+  # Listen for messages on all processes
+  def listen 0 do :ok end
+
+  def listen n do
+    receive do
+      :found -> System.halt 1
+      :ok -> listen (n - 1)
+    end
+  end
+
+  def counterexpl start, max, n_processes do
+    receive do
+      parent_pid ->
+        if counterexpl_loop start, max, n_processes do
+          send parent_pid, :found
+        else
+          send parent_pid, :ok
+        end
+    end
+  end
+
+  def counterexpl_loop a, max, n_processes do
+    cond do
+      iter a, 0 -> true
+      a + n_processes <= max ->
+        counterexpl_loop (a + n_processes), max, n_processes
+      true -> false
+    end
+  end
+
+  def iter a, b do
+    cond do
+      test a, b -> true
+      b + 1 < a -> iter a, b + 1
+      true -> false
+    end
+  end
+
+  def test a, b do
+    rem (sum (a + b) - sum a - sum b), 9 == 0
+  end
+
+  def sum 0 do 0 end
+
+  def sum n do
+    d = rem n, 10
+    r = div n, 10
+
+    d + sum r
+  end
+
+  def invalid_input do
+    System.halt 2
+  end
+end