a-conjecture-of-mine

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

commit d58bd163e61411a420f36a77d42ff0f24f44dc4a
parent 6b73ae0b1eb3a5d3144291d9b4df55912e0ef1ef
Author: Pablo Emílio Escobar Gaviria <pablo-escobar@riseup.net>
Date:   Thu,  6 Aug 2020 09:37:59 -0300

Cleaned up the Rust implementation

Diffstat:
MMakefile | 18+++++++++---------
Mrust/main.rs | 47++++++++++++++++++++++++-----------------------
2 files changed, 33 insertions(+), 32 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,30 +1,30 @@
-c:
+c-bin:
 	gcc -pthread -o bin/c ./c/main.c
 
-elixir:
+elixir-bin:
 	elixirc -o bin/elixir ./elixir/main.ex
 
-go:
+go-bin:
 	cd ./go/ ; go build ; mv ./go ../bin/go ; cd ..
 
-haskell:
+haskell-bin:
 	ghc --make -O -o ./bin/haskell ./haskell/Main.hs \
 	    && rm ./haskell/*.o ./haskell/*.hi
 
-java:
+java-bin:
 	javac ./java/Main.java
 	jar cfe ./bin/java.jar Main ./java/Main.class && rm ./java/*.class
 
-kotlin:
+kotlin-bin:
 	kotlinc ./kotlin/main.kt -d ./bin/kotlin.jar
 
-ocaml:
+ocaml-bin:
 	ocamlfind ocamlopt -thread -package threads -o ./bin/ocaml -linkpkg ./ocaml/main.ml \
 	    && rm ./ocaml/*.cmi ./ocaml/*.cmx ./ocaml/*.o
 
-rust:
+rust-bin:
 	rustc -v -o ./bin/rust ./rust/main.rs
 
-wasm:
+wasm-bin:
 	wat2wasm ./wasm/main.wat -o ./bin/wasm.wasm
 
diff --git a/rust/main.rs b/rust/main.rs
@@ -7,34 +7,24 @@ use std::{
     env,
     process,
     thread,
-    sync::{Arc, mpsc::{self, Sender, Receiver}}
+    sync::{Arc, mpsc::{Sender, Receiver, channel}}
 };
 
 const SUCCESS: i32 = 0;
 const FAIL: i32 = 1;
 const INVALID_INPUT: i32 = 2;
-const DEFAULT_N_THREADS: usize = 1;
 
 fn main() {
     let mut args = env::args();
     args.next();
 
-    if let Some(max_str) = args.next() {
-        let n_threads = args.next()
-            .and_then(|s| s.trim().parse::<usize>().ok())
-            .unwrap_or(DEFAULT_N_THREADS);
+    let max = parse_arg(args.next());
+    let n_threads = parse_arg(args.next());
 
-        if let Ok(max) =  max_str.trim().parse::<usize>() {
-            if counterexempl(max, n_threads) {
-                process::exit(FAIL);
-            } else {
-                process::exit(SUCCESS);
-            }
-        } else {
-            process::exit(INVALID_INPUT);
-        }
+    if counterexempl(max, n_threads) {
+        process::exit(FAIL);
     } else {
-        process::exit(INVALID_INPUT);
+        process::exit(SUCCESS);
     }
 }
 
@@ -43,21 +33,23 @@ fn counterexempl(max: usize, n_threads: usize) -> bool {
     let sums_cache = Arc::new(get_sums(max));
 
     if n_threads > 1 {
-        let (coutexpl_sender, coutexpl_reciever): (Sender<bool>, Receiver<bool>) = mpsc::channel();
+        let (sender, reciever): (Sender<bool>, Receiver<bool>) = channel();
         let mut child_threads = Vec::with_capacity(n_threads);
 
-        for i in 0..n_threads {
-            let thread_countr_sd = coutexpl_sender.clone();
+        for _ in 0..n_threads {
+            let thread_countr_sd = sender.clone();
             let thread_sums = sums_cache.clone();
 
             let child = thread::spawn(
-                move || thread_countr_sd.send(counterexempl_range(thread_sums, max, n_threads)
-            ).expect(
-                &format!("Thread n°{} was unable to sent a message trought the channel", i))
+                move || thread_countr_sd.send(
+                    counterexempl_range(thread_sums, max, n_threads)
+                ).expect(
+                    "Thread was unable to sent a message trought the channel"
+                )
             );
             
             child_threads.push(child);
-            if let Ok(true) = coutexpl_reciever.recv() {
+            if let Ok(true) = reciever.recv() {
                 return true;
             }
         }
@@ -115,3 +107,12 @@ fn get_sums(max: usize) -> Vec<isize> {
     output
 }
 
+#[inline]
+/// Parse an argument from argv.
+fn parse_arg(arg: Option<String>) -> usize {
+    match arg.and_then(|s| s.trim().parse::<usize>().ok()) {
+        Some(n) => n,
+        None    => process::exit(INVALID_INPUT)
+    }
+}
+