Permalink
Browse files

Move a bunch of now-unused code to old-code

  • Loading branch information...
1 parent c3ea109 commit 01feafb5db762b530bc15b18e570e50151d2420b @marick committed Oct 15, 2012
File renamed without changes.
File renamed without changes.
File renamed without changes.
View
@@ -0,0 +1,37 @@
+;;; Exercise 1
+
+(def recurser-core
+ (fn [stop-when ending-value combiner reducer]
+ (letfn [(recursive-function [something]
+ (if (stop-when something)
+ (ending-value something)
+ (combiner something
+ (recursive-function (reducer something)))))]
+ recursive-function)))
+
+;;; Exercise 2
+
+(def recurser-core
+ (fn [stop-when ending-value combiner reducer]
+ (Y (fn [recursive-function]
+ (fn [something]
+ (if (stop-when something)
+ (ending-value something)
+ (combiner something
+ (recursive-function (reducer something)))))))))
+
+;;; Exercise 3
+
+(def recurser
+ (fn [& args]
+ (let [control-map (apply hash-map args)
+ ending-value (:ending-value control-map)
+ ending-value (if (fn? ending-value)
+ ending-value
+ (constantly ending-value))]
+ (recurser-core (:stop-when control-map)
+ ending-value
+ (:combiner control-map)
+ (:reducer control-map)))))
+
+
View
@@ -1,12 +1,9 @@
cat pieces/add-and-make* > add-and-make.clj
cat pieces/class-?.clj > class.clj
-cat pieces/superclass-*.clj > superclass.clj
cat pieces/recursion-*.clj > recursion.clj
cat pieces/without-klass-?.clj > without-klass.clj
cat pieces/klass-?.clj > klass.clj
-cat pieces/building-data-*.clj > building-data.clj
cat pieces/higher-order-functions-*.clj > higher-order-functions.clj
-cat pieces/Y-?.clj > Y.clj
cat pieces/function-monads*.clj > function-monads.clj
cat pieces/scheduling-?.clj > scheduling.clj
cat pieces/zipper-*.clj > zipper.clj
@@ -0,0 +1,79 @@
+;;; Exercise 1
+
+(def fmerge
+ (fn [fap map]
+ (reduce (fn [so-far key]
+ (fassoc so-far key (key map)))
+ fap
+ (keys map))))
+
+;;; Exercise 2
+
+;; In this solution, I start with an `fassoc-base` that
+;; knows how to add on only one key/value pair. `fmerge`
+;; uses that, along with `reduce`. The multi-pair `fassoc`
+;; creates a map from its arguments and merges it.
+
+(def fassoc-base
+ (fn [fap new-key value]
+ (fn [lookup-key]
+ (if (= lookup-key new-key)
+ value
+ (fap lookup-key)))))
+
+(def fmerge
+ (fn [fap map]
+ (reduce (fn [so-far key]
+ (fassoc-base so-far key (key map)))
+ fap
+ (keys map))))
+
+(def fassoc
+ (fn [fap & pairs]
+ (fmerge fap (apply hash-map pairs))))
+
+;;; Exercise 2 (another version)
+
+;; Rather than have `fassoc-base` be `def`ed itself, I'll define it in
+;; a let. There's no need for it to be globally visible. It's also
+;; interesting to note that the `let` can surround a `def`. (In Ruby,
+;; a method definition can't see bindings established outside it, but
+;; the lesser-used `define-method` can.)
+
+
+(let [fassoc-base
+ (fn [fap new-key value]
+ (fn [lookup-key]
+ (if (= lookup-key new-key)
+ value
+ (fap lookup-key))))]
+ (def fassoc
+ (fn [fap & key-values]
+ (let [pairs (partition 2 key-values)]
+ (reduce (fn [so-far pair]
+ (apply fassoc-base so-far pair))
+ fap
+ pairs)))))
+
+(def fmerge
+ (fn [fap to-add]
+ (let [map-to-arguments (partial mapcat identity)]
+ (apply fassoc fap (map-to-arguments to-add)))))
+
+
+;;; Exercise 3
+
+(def lector
+ (fn []
+ (fn [index]
+ (throw (new IndexOutOfBoundsException)))))
+
+;;; Exercise 4
+
+(def lons
+ (fn [value lector]
+ (fn [index]
+ (if (zero? index)
+ value
+ (lector (dec index))))))
+

0 comments on commit 01feafb

Please sign in to comment.