Contents    Page-10    Prev    Next    Page+10    Index   

Mapcat and Filter

The Clojure function mapcat ( mapcan in Lisp) works much like map ( mapcar in Lisp), but with a different way of gathering results:

A function related to mapcat is filter, Which returns a list of only those items that satisfy a predicate.


(defn filtr [predicate lst]
  (mapcat (fn [item]
              (if (predicate item)
                  (list item) ))
          lst) )

>(filter number? '(a 2 or 3 and 7))
      ;           ()(2)()(3)() (7)
(2 3 7)

>(filter symbol? '(a 2  or 3  and 7))
      ;           (a)()(or)()(and)()
(A OR AND)