aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-095/tyler-wardhaugh/clojure/deps.edn3
-rw-r--r--challenge-095/tyler-wardhaugh/clojure/src/tw/weekly/c95/core.clj12
-rw-r--r--challenge-095/tyler-wardhaugh/clojure/src/tw/weekly/c95/stack.clj25
-rw-r--r--challenge-095/tyler-wardhaugh/clojure/src/tw/weekly/c95/t2.clj70
4 files changed, 109 insertions, 1 deletions
diff --git a/challenge-095/tyler-wardhaugh/clojure/deps.edn b/challenge-095/tyler-wardhaugh/clojure/deps.edn
index 7c98aa5deb..12e0c0d36b 100644
--- a/challenge-095/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-095/tyler-wardhaugh/clojure/deps.edn
@@ -1,5 +1,6 @@
{:paths ["src" "resources"]
- :deps {org.clojure/clojure {:mvn/version "1.10.1"}}
+ :deps {org.clojure/clojure {:mvn/version "1.10.1"}
+ org.clojure/data.finger-tree {:mvn/version "0.0.3"}}
:aliases
{:test {:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}}
diff --git a/challenge-095/tyler-wardhaugh/clojure/src/tw/weekly/c95/core.clj b/challenge-095/tyler-wardhaugh/clojure/src/tw/weekly/c95/core.clj
new file mode 100644
index 0000000000..4a0f36d178
--- /dev/null
+++ b/challenge-095/tyler-wardhaugh/clojure/src/tw/weekly/c95/core.clj
@@ -0,0 +1,12 @@
+(ns tw.weekly.c95.core
+ (:require [tw.weekly.c95.t1 :as t1])
+ (:require [tw.weekly.c95.t2 :as t2])
+ (:gen-class))
+
+(defn -main
+ "Run all tasks"
+ [& _]
+ (println "Task #1:")
+ (t1/-main)
+ (println "\nTask #2:")
+ (t2/-main))
diff --git a/challenge-095/tyler-wardhaugh/clojure/src/tw/weekly/c95/stack.clj b/challenge-095/tyler-wardhaugh/clojure/src/tw/weekly/c95/stack.clj
new file mode 100644
index 0000000000..292ccf4e41
--- /dev/null
+++ b/challenge-095/tyler-wardhaugh/clojure/src/tw/weekly/c95/stack.clj
@@ -0,0 +1,25 @@
+(ns tw.weekly.c95.stack
+ (:refer-clojure :exclude [pop min])
+ (:require [clojure.data.finger-tree :as ft]))
+
+(defprotocol PStack
+ "A stack protocol"
+ (push [this v] "Push a value onto the stack")
+ (pop [this] "Pop a value from the top of the stack")
+ (top [this] "See the top of the stack (a.k.a. peek)")
+ (min [this] "Return the minumum value of the stack"))
+
+(defrecord FTStack [coll]
+ PStack
+ (push [_ v]
+ (FTStack. (conj coll v)))
+ (pop [_]
+ (FTStack. (.pop coll)))
+ (top [_]
+ (.peek coll))
+ (min [_]
+ (reduce clojure.core/min coll)))
+
+(defn make-stack
+ ([] (->FTStack (ft/double-list)))
+ ([coll] (->FTStack (into (ft/double-list) coll))))
diff --git a/challenge-095/tyler-wardhaugh/clojure/src/tw/weekly/c95/t2.clj b/challenge-095/tyler-wardhaugh/clojure/src/tw/weekly/c95/t2.clj
new file mode 100644
index 0000000000..7fabc650ca
--- /dev/null
+++ b/challenge-095/tyler-wardhaugh/clojure/src/tw/weekly/c95/t2.clj
@@ -0,0 +1,70 @@
+(ns tw.weekly.c95.t2
+ (:require [clojure.edn :as edn]
+ [tw.weekly.c95.stack :as stk])
+ (:import java.util.Stack))
+
+;;;
+; Task description for TASK #2 › Demo Stack
+;;;
+
+;;; Example from Task:
+; my $stack = Stack->new;
+; $stack->push(2);
+; $stack->push(-1);
+; $stack->push(0);
+; $stack->pop; # removes 0
+; print $stack->top; # prints -1
+; $stack->push(0);
+; print $stack->min; # prints -1
+;;;
+
+(defn demo-stack-java
+ "Demonstrate using Java's Stack"
+ []
+ (->>
+ (doto (Stack.)
+ (.push 2)
+ (.push -1)
+ (.push 0)
+ (.pop)
+ (#(println (.peek %)))
+ (.push 0))
+ (reduce min)
+ println))
+
+(defn demo-stack-clj-list
+ "Demonstrate using Clojure's list datatype as a stack"
+ []
+ (->>
+ (-> (list)
+ (conj 2)
+ (conj -1)
+ (conj 0)
+ (pop)
+ ((fn [stack] (println (peek stack)) stack))
+ (conj 0))
+ (reduce min)
+ println))
+
+(defn demo-stack-finger-tree
+ "Demonstrate using protocol & a finger tree-backed record"
+ []
+ (-> (stk/make-stack)
+ (.push 2)
+ (.push -1)
+ (.push 0)
+ (.pop)
+ ((fn [stack] (println (.top stack)) stack))
+ (.push 0)
+ (.min)
+ println))
+
+(defn -main
+ "Run Task 2, demoing stack operations in three different ways."
+ [& args]
+ (println "java.util.Stack demo:")
+ (demo-stack-java)
+ (println "Clojure list demo:")
+ (demo-stack-clj-list)
+ (println "Finger tree-backed record demo")
+ (demo-stack-finger-tree))