Permalink
Browse files

Initial commit; Chapter 1, done!

  • Loading branch information...
0 parents commit c3acd1dea8ff2f45133d34e1a1bc1590ec485656 @stevenharman committed Nov 18, 2012
Showing with 92 additions and 0 deletions.
  1. +10 −0 .gitignore
  2. +12 −0 README.md
  3. +3 −0 doc/intro.md
  4. +5 −0 project.clj
  5. +6 −0 src/fp_oo_exercises/core.clj
  6. +49 −0 src/fp_oo_exercises/just-enough-clojure.clj
  7. +7 −0 test/fp_oo_exercises/core_test.clj
10 .gitignore
@@ -0,0 +1,10 @@
+/target
+/lib
+/classes
+/checkouts
+pom.xml
+*.jar
+*.class
+.lein-deps-sum
+.lein-failures
+.lein-plugins
12 README.md
@@ -0,0 +1,12 @@
+# fp-oo-exercises
+
+A collection of Clojure snippets from my trip through @marick's "Functional
+Programming for Object-Oriented Programmers" as part of the @bignerdranch book
+club.
+
+## License
+
+Copyright © 2012 Steven Harman
+
+Distributed under the WTFPL. That is to say, it's free; if it breaks, feel
+free to keep both parts.
3 doc/intro.md
@@ -0,0 +1,3 @@
+# Introduction to fp-oo-exercises
+
+TODO: write [great documentation](http://jacobian.org/writing/great-documentation/what-to-write/)
5 project.clj
@@ -0,0 +1,5 @@
+(defproject fp-oo-exercises "0.1.0-SNAPSHOT"
+ :description "Steven Harman's first trip into Clojure"
+ :license {:name "WTFPL"
+ :url "http://sam.zoy.org/wtfpl/"}
+ :dependencies [[org.clojure/clojure "1.4.0"]])
6 src/fp_oo_exercises/core.clj
@@ -0,0 +1,6 @@
+(ns fp-oo-exercises.core)
+
+(defn foo
+ "I don't do a whole lot."
+ [x]
+ (println x "Hello, World!"))
49 src/fp_oo_exercises/just-enough-clojure.clj
@@ -0,0 +1,49 @@
+;; Exercise 1
+(def second (fn [list] (first (rest list))))
+
+;; Exercise 2
+(def third (fn [list] (nth list 2)))
+
+(def third-2 (fn [list] (second (rest list))))
+
+;; Exercise 3
+(def add-squares
+ (fn [& args]
+ (apply + (map
+ (fn [n] (* n n))
+ args))))
+
+;; Exercise 4
+(def factorial
+ (fn [n]
+ (apply * (range 1 (inc n)))))
+
+;; Exercise 5
+;; Lots of playing around in repl...
+
+;; Exercise 6
+(def prefix-of?
+ (fn [candidate sequence]
+ (= (take (count candidate) sequence)
+ candidate)))
+
+;; Exercise 7
+(def tails
+ (fn [seq]
+ (map drop
+ (range 0 (inc (count seq)))
+ (repeat (inc (count seq)) seq))))
+
+;; Exercise 8
+(def puzzle
+ (fn [list] (list list)))
+
+;; user => (puzzle '(1 2 3))
+;; ClassCastException clojure.lang.PersistentList cannot be cast to clojure.lang.IFn user/puzzle (NO_SOURCE_FILE:1))
+;;
+;; When the argument is substituted into the expanded function we get
+;; ((1 2 3) (1 2 3))
+;;
+;; And the error is complaining about the first list NOT being something that
+;; could be evaluted to a function, as is expected.
+
7 test/fp_oo_exercises/core_test.clj
@@ -0,0 +1,7 @@
+(ns fp-oo-exercises.core-test
+ (:use clojure.test
+ fp-oo-exercises.core))
+
+(deftest a-test
+ (testing "FIXME, I fail."
+ (is (= 0 1))))

0 comments on commit c3acd1d

Please sign in to comment.