Commit 49f59c49 authored by Alf Kristian Støyle's avatar Alf Kristian Støyle

Initial commit, with the first tests.

parents
/target
/classes
/checkouts
pom.xml
pom.xml.asc
*.jar
*.class
/.lein-*
/.nrepl-port
.idea/
*.iml
This diff is collapsed.
# clojure-workshop-flatmap
A Clojure library designed to ... well, that part is up to you.
## Usage
FIXME
## License
Copyright © 2014 FIXME
Distributed under the Eclipse Public License either version 1.0 or (at
your option) any later version.
# Introduction to clojure-workshop-flatmap
TODO: write [great documentation](http://jacobian.org/writing/great-documentation/what-to-write/)
(defproject clojure-workshop-flatmap "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.6.0"]
[midje "1.6.3"]]
:profiles {:dev {:plugins [[lein-midje "3.1.1"]]}})
(ns clojure-workshop-flatmap.core)
(defn foo
"I don't do a whole lot."
[x]
(println x "Hello, World!"))
(ns clojure-workshop-flatmap.ex-1
(:require [clojure.test :refer :all]))
; Placeholders do not touch.
(defn __ [& args] false)
(def ___ '())
(deftest very-basic-types
; Which number is the same as 1? :)
(is (= 1 1))
; A string number is not = to the same number
(is (not= "1" 1))
; A more lenient number comparison with ==
(is (== 1 1.0))
; Only one thing is really true...
(is (true? true))
; Now what is the concatenation of those Characters
(is (= (str \a \b \c) "abc"))
; Use vector literal
(is (= '(1 2 3) [1 2 3]))
; Use list literal
(is (= [1 2 3] '(1 2 3)))
; Use list function
(is (= [1 2 3] (list 1 2 3)))
; Use vector function
(is (= [1 2 3] (vector 1 2 3))))
(deftest use-conjoin
; conjoin is a special function that works with most datastructures.
; It nows where the right place is to append an element.
; Use (doc conj) for info
; Oh, and by the way. 'are' creates a template (using a macro),
; for alle the following test form, which need to be in pairs in this case.
(are [x y] (= x y)
(conj [1] 2) [1 2]
(conj [1 2] 3 4) [1 2 3 4]
(conj '(2 1) 3 4) '(4 3 2 1)
(conj #{2 1} 3 4) #{1 2 3 4}))
(deftest how-to-count-stuff
(are [x y] (= x y)
5 (count '(1 2 3 4 5))
4 (count [1 2 3 4])
10 (count (range 10))
4 (count {:a 1, :b 2, :c 3, :d 4})
8 (count "En banan")))
(deftest how-to-find-length-of-something
; First class functions are so cool. Put in the right function in let
; and all assertions should be correct.
(let [f count]
(are [x y] (= x y)
(f '(1 2 3 3 1)) 5
(f "Hello World") 11
(f [[1 2] [3 4] [5 6]]) 3
(f '(13)) 1
(f '(:a :b :c)) 3)))
(deftest using-if
; Lots of things are truthy, i.e. is handled as true in e.g. if expressions.
(are [x y] (= x y)
(if (> 1 0)
true
false) true
(if (> 3 2 1)
true
false) true
(if (> 3 2 3 1)
true
false) false
(if nil
"nil is truthy"
"nil is falsey") "nil is falsey"
(if true
"true is truthy"
"true is falsey") "true is truthy"
(if false
"false is truthy"
"false is falsey") "false is falsey"
(if '(1)
"list is truthy"
"list is falsey") "list is truthy"
(if '()
"empty list is truthy"
"empty list is falsey") "empty list is truthy"
(if (Object.)
"Object is truthy"
"Object is falsey") "Object is truthy"))
(deftest dealing-with-lists
(are [x y] (= x y)
; Should be able to use one simple function call to get result
(first '(1 2 3 4)) 1
(last '(1 2 3 4 5)) 5
(take 2 '(1 2 3 4)) '(1 2)))
(deftest doing-match
(are [x y] (= x y)
; Here you should try to use the correct mathematical function to get the expected result
(+ 1 2 3 4) 10
(- 4 3 2 1) -2
(* 1 2 3 4) 24))
(deftest math-on-lists
(are [x y] (= x y)
; Here you should try to apply the correct mathematical function to get the expected result
(apply + [1 2 3 4]) 10
(apply - [4 3 2 1]) -2
(apply * [1 2 3 4]) 24))
(deftest how-to-filter-out-the-stuff-you-want
(are [x y] (= x y)
; Filter is cool. Can you write or use a cool function to get correct result?
(filter odd? '(1 2 3 4 5)) '(1 3 5)
(filter even? '(1 2 3 4 5)) '(2 4)))
(deftest use-map-to-double-all-numbers-in-a-sequence
; Write a function in the let form that doubles its input
(let [double (partial * 2)]
(are [x y] (= x y)
(map double '(1 2 3)) '(2 4 6)
(map double '(5 10 15)) '(10 20 30))))
(deftest define-a-function-that-checks-string-longer-than
; Write a function in the let form that that checks that the input String is longer than number input.
(let [longer-than? #(> (count %1) %2)]
(are [x y] (= x y)
(longer-than? "long string" 5) true
(longer-than? "short" 5) false
(longer-than? nil 2) false)))
(ns clojure-workshop-flatmap.ex-2
(:use [midje.sweet])
(:require [clojure.string :as str]))
(def _ 0)
(defn __ [& args] false)
(def ___ '())
(fact "A couple of thing about midje"
1 => 1
(throw (Exception.)) => (throws Exception))
;; ffirst second
(fact "working with seqs is pretty simple"
; how to get the head element of a seq
(first [1 2 3]) => 1
; how to get the tail element of a seq
(rest [1 2 3]) => [2 3]
; What does rest of empty list do?
(rest ()) => ()
; What does next of empty list do?
(next ()) => nil
; What does drop produce?
(drop 2 [1 2 3]) => [3]
; Drop on a too small list?
(drop 2 [1]) => []
; Get the next of the first element
(nfirst [[1 2][3 4]]) => [2]
; Get the first of the next element?
(fnext [[1 2] 3 4]) => 3
(fnext [[1 2] [3 4]]) => [3 4]
(fnext []) => nil)
(fact "Not all functions are meant to work with sequences"
(apply map inc [[1 2 3]]) => [2 3 4]
(apply str "1 2 3 " [4 " " 5 " " 6]) => "1 2 3 4 5 6")
(fact "After a let (fn, defn), you can do side-effect"
(let [a (atom 0)]
(swap! a inc)
(deref a) => 1))
(fact "dotimes stuff for side-effects"
(let [a (atom 0)]
; Let is incorrect you should do times x with swap! function
(dotimes [x 10]
(swap! a inc))
@a => 10))
(fact "doseq stuff for side-effects"
(let [a (atom 2)]
; Let is incorrect you should do times x with swap! function
(doseq [f [inc (partial * 2)]]
(swap! a f))
@a => 6))
(fact "doseq stuff for side-effects"
(let [a (atom 0)]
; Let is incorrect you should do times x with swap! function
(while (> 6 @a)
(swap! a inc))
@a => 6))
; map filter reduce
(fact "Filter removes values from a sequence"
(filter odd? [0 1 2 3 4 5 6 7 8 9]) => [1 3 5 7 9]
(filter even? (take 10 (range))) => [0 2 4 6 8]
(filter identity [1 2 3 nil 5]) => [1 2 3 5])
(fact "Use map to transform each element a list"
(map #(str (inc %)) [1 2 3]) => ["2" "3" "4"]
(map (memfn toUpperCase) ["a" "simple" "sentence"]) => ["A" "SIMPLE" "SENTENCE"])
(fact "With reduce you can do more"
(reduce
#((memfn toUpperCase) (str %1 " " %2)) ["a" "simple" "sentence"])
=> "A SIMPLE SENTENCE"
(reduce
#(conj %1 ((memfn toUpperCase) %2)) [] ["a" "simple" "sentence"])
=> ["A" "SIMPLE" "SENTENCE"])
(fact "Some functions blow up if you dont use them correctly"
(nth [1 2 3] 1) => 2
(nth [1 2 3] 4) => (throws Exception)
(nth [1 2 3] 4 nil) => nil)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment