8.0 KiB
8.0 KiB
In [1]:
(defn moyenner [lst]
(/ (apply + lst) (count lst)))Out [1]:
#'user/moyenner
In [2]:
(moyenner [ 10 11 12])Out [2]:
11
In [1]:
(defn double-number[n]
(* 2 n))
""
(defn double-all [ lst]
(map #(* 2 %) lst))
Out [1]:
#'user/double-all
In [2]:
(double-all [45 66 12 45])Out [2]:
(90 132 24 90)
In [7]:
;; 3 < 4 < 10 < 100\
;; (< 3 4 10 100)
(defn larger-than [n]
(< 3 n))
(defn new-sum [lst]
"Add all the numbers in lst that are larger than 3."
(apply + lst)
(filter #(< 3 %) lst)
(apply + (filter #(< 3 %) lst)))
(new-sum [1 5 2 4 4 0])Out [7]:
13
In [8]:
(#(- %1 %2) 5 6)Out [8]:
-1
In [9]:
(let [x [ 1 2 34]
y "hello"
z :toto]
[x y z])Out [9]:
[[1 2 34] "hello" :toto]
In [10]:
(defn new-sum [lst]
"Add all the numbers in lst that are larger than 3."
(let [sub-lst (filter #(< 3 %) lst)]
(apply + sub-lst)))Out [10]:
#'user/new-sum
In [11]:
;; (reduce f initial-value lst)
(defn f [acc new-el]
;; update acc
acc)
Out [11]:
#'user/f
In [12]:
; var initial-value = [];
;for(var e : lst){
; initial-value.push(f(e));
;}In [13]:
(reductions + [2 3 4])Out [13]:
(2 5 9)
In [14]:
(conj (map #(* 2 %)[ 1 2 3])3)
Out [14]:
(3 2 4 6)
In [15]:
(reduce + [2 3 4])Out [15]:
9
In [20]:
(defn foo [g lst e]
(conj lst (g e)))
(defn my-map [f lst]
(let [poartialled-foo (partial foo f)]
(reduce poartialled-foo [] lst)))
(my-map #(* 2 %) [6 7 8]) ;; 12 14 16
;;#(conj %1 (f %2))Out [20]:
[12 14 16]
In [21]:
(defn toto [ x y]
(+ x y))Out [21]:
#'user/toto
In [22]:
((partial toto 90) 100)Out [22]:
190
In [23]:
;; 3 * x + 7
;; 17 * 3 <- 17 <- 10
(let [f1 (map #(* 3 %))
f2 (map #(+ % 7))]
(sequence (comp f2 f1) [10 1 0 101 45]))Out [23]:
(51 24 21 324 156)
In [24]:
(-> 16
(* 3)
double-number
(* 89)
str)Out [24]:
"8544"
In [25]:
(-> :foo name clojure.string/upper-case )Out [25]:
"FOO"
In [ ]: