aboutsummaryrefslogtreecommitdiff
path: root/challenge-073
diff options
context:
space:
mode:
authorJonas Berlin <xkr47@outerspace.dyndns.org>2020-08-16 13:18:07 +0300
committerJonas Berlin <xkr47@outerspace.dyndns.org>2020-08-16 13:19:01 +0300
commit13b2fc61b68bc705a308a35be05eb2b1810863a1 (patch)
tree869d9309d3307a4cf2e17149ba84cdc8debcb1ae /challenge-073
parentee4d4d5f5a89d4585af1bb4b292bc10f3b2fa27f (diff)
downloadperlweeklychallenge-club-13b2fc61b68bc705a308a35be05eb2b1810863a1.tar.gz
perlweeklychallenge-club-13b2fc61b68bc705a308a35be05eb2b1810863a1.tar.bz2
perlweeklychallenge-club-13b2fc61b68bc705a308a35be05eb2b1810863a1.zip
Challenge 73 Task 2 only, 2 alternative implementations
Diffstat (limited to 'challenge-073')
-rw-r--r--challenge-073/xkr47/rust/Cargo.toml16
-rw-r--r--challenge-073/xkr47/rust/ch-2-2.rs47
-rw-r--r--challenge-073/xkr47/rust/ch-2.rs18
3 files changed, 81 insertions, 0 deletions
diff --git a/challenge-073/xkr47/rust/Cargo.toml b/challenge-073/xkr47/rust/Cargo.toml
new file mode 100644
index 0000000000..59facb0ade
--- /dev/null
+++ b/challenge-073/xkr47/rust/Cargo.toml
@@ -0,0 +1,16 @@
+[package]
+name = "pwd-073"
+version = "0.1.0"
+authors = ["Jonas Berlin <xkr47@outerspace.dyndns.org>"]
+edition = "2018"
+
+[dependencies]
+num-traits = "0.2"
+
+[[bin]]
+name = "ch-2"
+path = "ch-2.rs"
+
+[[bin]]
+name = "ch-2-2"
+path = "ch-2-2.rs"
diff --git a/challenge-073/xkr47/rust/ch-2-2.rs b/challenge-073/xkr47/rust/ch-2-2.rs
new file mode 100644
index 0000000000..87aefc5875
--- /dev/null
+++ b/challenge-073/xkr47/rust/ch-2-2.rs
@@ -0,0 +1,47 @@
+use std::ops::Deref;
+use num_traits::Bounded;
+
+// Alternate impl as an iterator
+
+fn main() {
+ let a = vec![7, 8, 3, 12, 10];
+ let o: Vec<i32> = doit(&a);
+ assert_eq!(o, vec![0, 7, 0, 3, 3]);
+
+ let a = vec![4, 6, 5];
+ let o: Vec<i32> = doit(&a);
+ assert_eq!(o, vec![0, 4, 4]);
+}
+
+fn doit(a: &Vec<i32>) -> Vec<i32> {
+ MinLeft::new(a.iter()).collect()
+}
+
+struct MinLeft<I, T: Bounded + Copy + Ord + Default> {
+ a: I,
+ min: T,
+}
+
+impl<I, T: Bounded + Copy + Ord + Default> MinLeft<I, T> {
+ fn new(a: I) -> MinLeft<I, T> {
+ MinLeft {
+ a,
+ min: T::max_value(),
+ }
+ }
+}
+
+impl<'a, T: Bounded + Copy + Ord + Default, P: Deref<Target=T>, I: Iterator<Item=P>> Iterator for MinLeft<I, T> {
+ type Item = T;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ match self.a.next() {
+ Some(next) => {
+ let result = if self.min < *next { self.min } else { Default::default() };
+ self.min = self.min.min(*next);
+ Some(result)
+ },
+ None => None
+ }
+ }
+}
diff --git a/challenge-073/xkr47/rust/ch-2.rs b/challenge-073/xkr47/rust/ch-2.rs
new file mode 100644
index 0000000000..3cec5f12f5
--- /dev/null
+++ b/challenge-073/xkr47/rust/ch-2.rs
@@ -0,0 +1,18 @@
+fn main() {
+ let a = vec![7, 8, 3, 12, 10];
+ let o: Vec<i32> = doit(&a);
+ assert_eq!(o, vec![0, 7, 0, 3, 3]);
+
+ let a = vec![4, 6, 5];
+ let o: Vec<i32> = doit(&a);
+ assert_eq!(o, vec![0, 4, 4]);
+}
+
+fn doit(a: &Vec<i32>) -> Vec<i32> {
+ let mut min = i32::max_value();
+ a.iter().map(|next| {
+ let result = if min < *next { min } else { 0 };
+ min = min.min(*next);
+ result
+ }).collect()
+}