1
0
mirror of https://github.com/janet-lang/janet synced 2025-01-12 16:40:27 +00:00

Add more clojure functions in the examples

This commit is contained in:
Heefoo 2018-03-21 00:00:09 +02:00
parent c977c339a2
commit c7de277f55

View File

@ -16,10 +16,12 @@
(++ n))
reversed)
(defn reverse [t]
'(arrays are more efficient so reverse-tuple will not be used)
(defn reverse
"Reverses order of give array or tuple"
[t]
(def the-type (type t))
(cond (= the-type :tuple) (reverse-tuple t)
(cond (= the-type :tuple) (->> t iter2array reverse-array (apply tuple) )
(= the-type :array) (reverse-array t)))
@ -30,6 +32,30 @@
exp-1
exp-2))
(defmacro when-not
"Sorthand for (if (not ... "
[condition exp-1]
(tuple 'when (tuple 'not condition) exp-1))
(defmacro if-let
[bindings then else]
(def head (ast-unwrap1 bindings))
(tuple 'let head
(tuple 'if (get head 1)
then
else)))
(defmacro when-let
[bindings & then]
(def head (ast-unwrap1 bindings))
(tuple 'let head
(tuple
'when
(get head 1) (apply tuple (array-concat ['do] (ast-unwrap1 then))) )))
(defn comp0-
"Compose two functions. Second function must accept only one argument)"
[f g] (fn [x] (f (g x))))
@ -51,6 +77,6 @@
(do
(def f (get functions 0))
(def g (get functions 1))
(apply comp (comp0 f g) (array-slice functions 2 -1))) )))
(apply comp (comp0- f g) (array-slice functions 2 -1))) )))