-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathexploring.clj
More file actions
128 lines (109 loc) · 2.84 KB
/
exploring.clj
File metadata and controls
128 lines (109 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
; START:ns
(ns examples.exploring
(:require [clojure.string :as str])
(:import (java.io File)))
; END:ns
; START:date
(defn date [person-1 person-2 & chaperones]
(println person-1 "and" person-2
"went out with" (count chaperones) "chaperones."))
; END:date
; START:if
(defn is-small? [number]
(if (< number 100) "yes"))
; END:if
(def is-small-with-if? is-small?)
; START:if-else
(defn is-small? [number]
(if (< number 100) "yes" "no"))
; END:if-else
(def is-small-with-else? is-small?)
; START:do
(defn is-small? [number]
(if (< number 100)
"yes"
(do
(println "Saw a big number" number)
"no")))
; END:do
(def is-small-with-do? is-small?)
(defn demo-loop []
; START: loop
(loop [result [] x 5]
(if (zero? x)
result
(recur (conj result x) (dec x))))
; END: loop
)
; START:countdown
(defn countdown [result x]
(if (zero? x)
result
(recur (conj result x) (dec x))))
; END:countdown
; START: indexed
(defn indexed [coll] (map-indexed vector coll))
; END: indexed
; START: index-filter
(defn index-filter [pred coll]
(when pred
(for [[idx elt] (indexed coll) :when (pred elt)] idx)))
; END: index-filter
; START:index-of-any
(defn index-of-any [pred coll]
(first (index-filter pred coll)))
; END: index-of-any
; START:greeting
(defn greeting
"Returns a greeting of the form 'Hello, username.'"
[username]
(str "Hello, " username))
; END:greeting
(def simple-greeting greeting)
; START:greeting-with-default
(defn greeting
"Returns a greeting of the form 'Hello, username.'
Default username is 'world'."
([] (greeting "world"))
([username] (str "Hello, " username)))
; END:greeting-with-default
(def greeting-with-default greeting)
; START:indexable-word
(defn indexable-word? [word]
(> (count word) 2))
; END:indexable-word
; START:indexable-words
(defn indexable-words [text]
(let [indexable-word? (fn [w] (> (count w) 2))]
(filter indexable-word? (str/split text #"\W+"))))
; END:indexable-words
; START:make-greeter
(defn make-greeter [greeting-prefix]
(fn [username] (str greeting-prefix ", " username)))
; END:make-greeter
; START:square-corners
(defn square-corners [bottom left size]
(let [top (+ bottom size)
right (+ left size)]
[[bottom left] [top left] [top right] [bottom right]]))
; END:square-corners
; START:busted
(defn ^{:test (fn []
(assert (nil? (busted))))}
busted [] "busted")
; END:busted
(def vinge {:first-name "Vernor" :last-name "Vinge"})
; START:greet-author-1
(defn greet-author-1 [author]
(println "Hello," (:first-name author)))
; END:greet-author-1
; START:greet-author-2
(defn greet-author-2 [{fname :first-name}]
(println "Hello," fname))
; END:greet-author-2
; START:ellipsize
(require '[clojure.string :as str])
(defn ellipsize [words]
(let [[w1 w2 w3] (str/split words #"\s+")]
(str/join " " [w1 w2 w3 "..."])))
; END:ellipsize