diff --git a/README.md b/README.md index 55a1aff..58da129 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/dash.el b/dash.el index 9781960..392019d 100644 --- a/dash.el +++ b/dash.el @@ -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." diff --git a/examples.el b/examples.el index 6f11b60..fb03e5c 100644 --- a/examples.el +++ b/examples.el @@ -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)