Skip to content

Commit 0b6c42e

Browse files
author
Ilmari Vacklin
committed
Initial commit
0 parents  commit 0b6c42e

File tree

5 files changed

+197
-0
lines changed

5 files changed

+197
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/target
2+
/lib
3+
/classes
4+
/checkouts
5+
pom.xml
6+
*.jar
7+
*.class
8+
.lein-deps-sum
9+
.lein-failures
10+
.lein-plugins

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# recursion
2+
3+
I'm an app. Or maybe I'm a library? I haven't decided yet.
4+
5+
The choice is up to you!
6+
7+
## Usage
8+
9+
FIXME
10+
11+
## License
12+
13+
Copyright © 2012 FIXME
14+
15+
Distributed under the Eclipse Public License, the same as Clojure.

project.clj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(defproject structured-data "0.1.0-SNAPSHOT"
2+
:dependencies [[org.clojure/clojure "1.4.0"]]
3+
:profiles {:dev {:dependencies [[midje "1.4.0"]]
4+
:plugins [[lein-midje "2.0.0-SNAPSHOT"]]}})

src/recursion.clj

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
(ns recursion)
2+
3+
(defn product [coll]
4+
:-)
5+
6+
(defn last-element [coll]
7+
:-)
8+
9+
(defn sequence-contains? [elem a-seq]
10+
:-)
11+
12+
(defn seq= [a-seq b-seq]
13+
:-)
14+
15+
(defn power [a b]
16+
:-)
17+
18+
(defn fib [n]
19+
:-)
20+
21+
(defn my-range [up-to]
22+
:-)
23+
24+
(defn map-1 [f a-seq]
25+
:-)
26+
27+
(defn tails [a-seq]
28+
:-)
29+
30+
(defn inits [a-seq]
31+
:-)
32+
33+
(defn split-into-monotonics [a-seq]
34+
:-)
35+
36+
(defn rotations [a-seq]
37+
:-)
38+
39+
(defn my-frequencies [a-seq]
40+
:-)
41+
42+
(defn un-frequencies [a-map]
43+
:-)
44+
45+
(defn seq-merge [a-seq b-seq]
46+
:-)
47+
48+
(defn mergesort [a-seq]
49+
:-)
50+
51+
(defn permutations [a-seq]
52+
:-)
53+
54+
(defn powerset [a-seq]
55+
:-)
56+
57+
; %_____%

test/recursion_test.clj

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
(ns recursion-test
2+
(:use recursion
3+
midje.sweet))
4+
5+
(facts "product"
6+
(product []) => 1
7+
(product [1 2 3]) => 6
8+
(product [1 2 3 4]) => 24
9+
(product [0 1 2]) => 0
10+
(product #{2 3 4}) => 24)
11+
12+
(facts "last-element"
13+
(last-element []) => nil
14+
(last-element [1 2 3]) => 3
15+
(last-element [2 5]) => 5)
16+
17+
(facts "sequence-contains?"
18+
(sequence-contains? 3 [1 2 3]) => true
19+
(sequence-contains? 3 [4 7 9]) => false
20+
(sequence-contains? :pony []) => false)
21+
22+
(facts "seq="
23+
(seq= [1 2 4] '(1 2 4)) => true
24+
(seq= [1 2 3] [1 2 3 4]) => false
25+
(seq= [1 3 5] []) => false)
26+
27+
(facts "power"
28+
(power 2 2) => 4
29+
(power 5 3) => 125
30+
(power 7 0) => 1
31+
(power 0 10) => 0)
32+
33+
(facts "fib"
34+
(fib 0) => 0
35+
(fib 1) => 1
36+
(fib 2) => 1
37+
(fib 3) => 2
38+
(fib 4) => 3
39+
(fib 5) => 5
40+
(fib 6) => 8
41+
(fib 10) => 55)
42+
43+
(facts "my-range"
44+
(my-range 0) => nil
45+
(my-range 1) => [0]
46+
(my-range 2) => [1 0]
47+
(my-range 3) => [2 1 0])
48+
49+
(facts "map-1"
50+
(map-1 identity []) => empty?
51+
(map-1 identity [1 2 3]) => [1 2 3]
52+
(map-1 count ["aaa" "bb" "cccc"]) => [3 2 4]
53+
(map-1 first [[1 2] [4] [7 12 28]]) => [1 4 7]
54+
(map-1 zero? [0 2 0 13 4 0])
55+
=> [true false true false false true])
56+
57+
(facts "tails"
58+
(tails [1 2 3 4]) => (just [[1 2 3 4] [2 3 4] [3 4] [4] empty?] :in-any-order)
59+
(tails []) => (just [empty?] :in-any-order)
60+
(tails [1]) => (just [[1] empty?] :in-any-order))
61+
62+
(facts "inits"
63+
(inits [1 2 3 4]) => (just [empty? [1] [1 2] [1 2 3] [1 2 3 4]] :in-any-order)
64+
(inits []) => (just [empty?] :in-any-order)
65+
(inits [1]) => (just [empty? [1]] :in-any-order))
66+
67+
(facts "split-into-monotonics"
68+
(split-into-monotonics [0 1 2 1 0]) => '((0 1 2) (1 0))
69+
(split-into-monotonics [0 5 4 7 1 3]) => '((0 5) (4 7) (1 3)))
70+
71+
(facts "rotations"
72+
(rotations []) => empty?
73+
(rotations [1 2 3]) => (just [[1 2 3] [2 3 1] [3 1 2]] :in-any-order)
74+
(rotations [:a :b]) => (just [[:a :b] [:b :a]] :in-any-order)
75+
(rotations [1 5 9 2]) => (just '(1 5 9 2) '(2 1 5 9)
76+
'(9 2 1 5) '(5 9 2 1)
77+
:in-any-order)
78+
(count (rotations [6 5 8 9 2])) => 5)
79+
80+
(facts "my-frequencies"
81+
(my-frequencies []) => {}
82+
(my-frequencies [1 1 2 2 :D :D :D]) => {1 2, 2 2, :D 3}
83+
(my-frequencies [:a "moi" :a "moi" "moi" :a 1])
84+
=> {:a 3, "moi" 3, 1 1})
85+
86+
(facts "un-frequencies"
87+
(un-frequencies {:a 3 :b 2 "^_^" 1})
88+
=> (just [:a :a :a "^_^" :b :b] :in-any-order)
89+
(un-frequencies (my-frequencies [:a :b :c :a]))
90+
=> (just [:a :a :b :c] :in-any-order)
91+
(my-frequencies (un-frequencies {:a 100 :b 10}))
92+
=> {:a 100 :b 10})
93+
94+
(facts "seq-merge"
95+
(seq-merge [4] [1 2 6 7]) => '(1 2 4 6 7)
96+
(seq-merge [1 5 7 9] [2 2 8 10]) => '(1 2 2 5 7 8 9 10))
97+
98+
(facts "mergesort"
99+
(mergesort []) => empty?
100+
(mergesort [1 2 3]) => '(1 2 3)
101+
(mergesort [5 3 4 17 2 100 1]) => '(1 2 3 4 5 17 100))
102+
103+
(facts "permutations"
104+
(permutations []) => empty?
105+
(permutations [1 5 3])
106+
=> (just [[1 5 3] [5 1 3] [5 3 1] [1 3 5] [3 1 5] [3 5 1]]
107+
:in-any-order))
108+
109+
(facts "powerset"
110+
(powerset []) => '(())
111+
(powerset [1 2 4]) => (just [empty? [4] [2] [2 4] [1] [1 4] [1 2] [1 2 4]] :in-any-order))

0 commit comments

Comments
 (0)