mirror of
https://github.com/teo3300/rust-mal.git
synced 2026-01-12 01:05:32 +01:00
Adding collect function
- Collect function as fn:(collector, x) -> new_collector for collections - Moving some definitions to "libs" folder - Map and filter defined in mal
This commit is contained in:
22
libs/lists.mal
Normal file
22
libs/lists.mal
Normal file
@ -0,0 +1,22 @@
|
||||
(def! concat (fn* [x y] (def! concat-r (fn* [x y t]
|
||||
"Concatenate arguments, keeping their order"
|
||||
(if (car x)
|
||||
(concat-r (cdr x) y (cons (car x) t))
|
||||
(if (car y)
|
||||
(concat-r '() (cdr y) (cons (car y) t))
|
||||
t))))
|
||||
(concat-r (reverse y) (reverse x) '())))
|
||||
|
||||
(def! distribute (fn* [x] (def! distribute-r (fn* [p n t]
|
||||
(if (empty? n)
|
||||
t
|
||||
(let* [c (car n) n (cdr n)]
|
||||
(distribute-r (cons c p) n (cons (cons c (concat p n)) t))))))
|
||||
(distribute-r '() x '())))
|
||||
|
||||
(def! len (fn* [l] (def! len-r (fn* [l c]
|
||||
(if (empty? l)
|
||||
c
|
||||
(len-r (cdr l) (+ c 1)))))
|
||||
(len-r l 0)))
|
||||
|
||||
23
libs/math.mal
Normal file
23
libs/math.mal
Normal file
@ -0,0 +1,23 @@
|
||||
(def! abs (fn* [a]
|
||||
(if (> a 0)
|
||||
a
|
||||
(- 0 a))))
|
||||
|
||||
(def! mod (fn* [a b]
|
||||
(- a (* (/ a b) b))))
|
||||
|
||||
(def! max (fn* [a b]
|
||||
(if (> a b)
|
||||
a
|
||||
b)))
|
||||
|
||||
(def! min (fn* [a b]
|
||||
(if (< a b)
|
||||
a
|
||||
b)))
|
||||
|
||||
(def! fact (fn* [a]
|
||||
(def! fact-r (fn* [a b]
|
||||
(if (not (> a 1)) b
|
||||
(fact-r (- a 1) (* a b)))))
|
||||
(fact-r a 1)))
|
||||
Reference in New Issue
Block a user