Add -flatten

master
Magnar Sveen 14 years ago
parent f1c845338c
commit 0dab44c011
  1. 10
      README.md
  2. 6
      dash.el
  3. 4
      examples.el

@ -18,6 +18,7 @@ Or you can just dump `dash.el` in your load path somewhere.
* [-filter](#filter-fn-list) `(fn list)`
* [-remove](#remove-fn-list) `(fn list)`
* [-keep](#keep-fn-list) `(fn list)`
* [-flatten](#flatten-l) `(l)`
* [-concat](#concat-rest-lists) `(&rest lists)`
* [-mapcat](#mapcat-fn-list) `(fn list)`
* [-take](#take-n-list) `(n list)`
@ -146,6 +147,15 @@ Returns a new list of the non-nil results of applying `fn` to the items in `list
(--keep (when (> it 3) (* 10 it)) '(1 2 3 4 5 6)) ;; => '(40 50 60)
```
### -flatten `(l)`
Takes a nested list `l` and returns its contents as a single, flat list.
```cl
(-flatten '((1))) ;; => '(1)
(-flatten '((1 (2 3) (((4 (5))))))) ;; => '(1 2 3 4 5)
```
### -concat `(&rest lists)`
Returns a new list with the concatenation of the elements in

@ -130,6 +130,12 @@ Alias: `-reject'"
"Returns a new list of the non-nil results of applying FN to the items in LIST."
(--keep (funcall fn it) list))
(defun -flatten (l)
"Takes a nested list L and returns its contents as a single, flat list."
(if (listp l)
(-mapcat '-flatten l)
(list l)))
(defun -concat (&rest lists)
"Returns a new list with the concatenation of the elements in
the supplied LISTS."

@ -47,6 +47,10 @@
(-keep (lambda (num) (when (> num 3) (* 10 num))) '(1 2 3 4 5 6)) => '(40 50 60)
(--keep (when (> it 3) (* 10 it)) '(1 2 3 4 5 6)) => '(40 50 60))
(defexamples -flatten
(-flatten '((1))) => '(1)
(-flatten '((1 (2 3) (((4 (5))))))) => '(1 2 3 4 5))
(defexamples -concat
(-concat '(1)) => '(1)
(-concat '(1) '(2)) => '(1 2)

Loading…
Cancel
Save