~tim/malcc

ba50487832e303054626ed8ef12fcc5a5ac8cc86 — Tim Morgan 5 years ago 7446afc
Add a filter function example
1 files changed, 12 insertions(+), 0 deletions(-)

A examples/filter.mal
A examples/filter.mal => examples/filter.mal +12 -0
@@ 0,0 1,12 @@
(def! filter
      (fn* (l f)
           (let* [filter* (fn* (l1 l2 f)
                               (if (empty? l1)
                                 (seq l2)
                                 (if (apply f (first l1))
                                   (filter* (rest l1) (concat l2 (list (first l1))) f)
                                   (filter* (rest l1) l2 f))))]
             (filter* l [] f))))

(prn (filter '(1 2 3 4 5 6 7 8 0 9) (fn* (i) (< i 5))))
(prn (filter [0 1 2 3 4 5 6 7 8 9] (fn* (i) (< i 5))))