branch: externals/dash commit a9f90d7834337dadee1a69f2089e39fd81b22ba7 Author: Matus Goljer <matus.gol...@gmail.com> Commit: Matus Goljer <matus.gol...@gmail.com>
Add -select-column(s) --- README.md | 35 +++++++++++++++++++++++++++++++++++ dash.el | 25 +++++++++++++++++++++++++ dev/examples.el | 10 +++++++++- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 453b5ab..04e14e5 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,8 @@ Functions returning a sublist of the original list. * [-take-while](#-take-while-pred-list) `(pred list)` * [-drop-while](#-drop-while-pred-list) `(pred list)` * [-select-by-indices](#-select-by-indices-indices-list) `(indices list)` +* [-select-columns](#-select-columns-columns-table) `(columns table)` +* [-select-column](#-select-column-column-table) `(column table)` ### List to list @@ -575,6 +577,39 @@ as `(nth i list)` for all i from `indices`. (-select-by-indices '(0 1 2 0 1 3 3 1) '("f" "a" "r" "l")) ;; => '("f" "a" "r" "f" "a" "l" "l" "a") ``` +#### -select-columns `(columns table)` + +Select `columns` from `table`. + +`table` is a list of lists where each element represents one row. +It is assumed each row has the same length. + +Each row is transformed such that only the specified `columns` are +selected. + +See also: [`-select-column`](#-select-column-column-table), [`-select-by-indices`](#-select-by-indices-indices-list) + +```el +(-select-columns '(0 2) '((1 2 3) (a b c) (:a :b :c))) ;; => '((1 3) (a c) (:a :c)) +(-select-columns '(1) '((1 2 3) (a b c) (:a :b :c))) ;; => '((2) (b) (:b)) +(-select-columns nil '((1 2 3) (a b c) (:a :b :c))) ;; => '(nil nil nil) +``` + +#### -select-column `(column table)` + +Select `column` from `table`. + +`table` is a list of lists where each element represents one row. +It is assumed each row has the same length. + +The single selected column is returned as a list. + +See also: [`-select-columns`](#-select-columns-columns-table), [`-select-by-indices`](#-select-by-indices-indices-list) + +```el +(-select-column 1 '((1 2 3) (a b c) (:a :b :c))) ;; => '(2 b :b) +``` + ## List to list diff --git a/dash.el b/dash.el index b799b44..3f20e5c 100644 --- a/dash.el +++ b/dash.el @@ -1189,6 +1189,29 @@ as `(nth i list)` for all i from INDICES." (!cons (nth it list) r)) (nreverse r))) +(defun -select-columns (columns table) + "Select COLUMNS from TABLE. + +TABLE is a list of lists where each element represents one row. +It is assumed each row has the same length. + +Each row is transformed such that only the specified COLUMNS are +selected. + +See also: `-select-column', `-select-by-indices'" + (--map (-select-by-indices columns it) table)) + +(defun -select-column (column table) + "Select COLUMN from TABLE. + +TABLE is a list of lists where each element represents one row. +It is assumed each row has the same length. + +The single selected column is returned as a list. + +See also: `-select-columns', `-select-by-indices'" + (--mapcat (-select-by-indices (list column) it) table)) + (defmacro -> (x &optional form &rest more) "Thread the expr through the forms. Insert X as the second item in the first form, making a list of it if it is not a list @@ -2341,6 +2364,8 @@ structure such as plist or alist." "-find-last-index" "--find-last-index" "-select-by-indices" + "-select-columns" + "-select-column" "-grade-up" "-grade-down" "->" diff --git a/dev/examples.el b/dev/examples.el index 22ea314..6377e69 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -179,7 +179,15 @@ new list." (defexamples -select-by-indices (-select-by-indices '(4 10 2 3 6) '("v" "e" "l" "o" "c" "i" "r" "a" "p" "t" "o" "r")) => '("c" "o" "l" "o" "r") (-select-by-indices '(2 1 0) '("a" "b" "c")) => '("c" "b" "a") - (-select-by-indices '(0 1 2 0 1 3 3 1) '("f" "a" "r" "l")) => '("f" "a" "r" "f" "a" "l" "l" "a"))) + (-select-by-indices '(0 1 2 0 1 3 3 1) '("f" "a" "r" "l")) => '("f" "a" "r" "f" "a" "l" "l" "a")) + + (defexamples -select-columns + (-select-columns '(0 2) '((1 2 3) (a b c) (:a :b :c))) => '((1 3) (a c) (:a :c)) + (-select-columns '(1) '((1 2 3) (a b c) (:a :b :c))) => '((2) (b) (:b)) + (-select-columns nil '((1 2 3) (a b c) (:a :b :c))) => '(nil nil nil)) + + (defexamples -select-column + (-select-column 1 '((1 2 3) (a b c) (:a :b :c))) => '(2 b :b))) (def-example-group "List to list" "Bag of various functions which modify input list."