### Install dash.el Package Source: https://github.com/magnars/dash.el/blob/master/README.md Installs the dash.el Emacs package using the standard package-install command. This is the recommended method for users who have package archives configured. ```emacs-lisp M-x package-install RET dash RET ``` -------------------------------- ### partition-before-item: Partition list before specific item Source: https://github.com/magnars/dash.el/blob/master/README.md Partitions a list directly before each occurrence of a specified `item`. The `item` starts the new sublist. ```el (-partition-before-item 3 ()) ;; => () (-partition-before-item 3 '(1)) ;; => ((1)) (-partition-before-item 3 '(3)) ;; => ((3)) ``` -------------------------------- ### Generate Sequence (iota) (Emacs Lisp) Source: https://github.com/magnars/dash.el/blob/master/README.md Returns a list containing `count` numbers, starting from `start` (default 0) and incrementing by `step` (default 1). ```el (-iota 6) ;; => (0 1 2 3 4 5) (-iota 4 2.5 -2) ;; => (2.5 0.5 -1.5 -3.5) (-iota -1) ;; Wrong type argument: natnump, -1 ``` -------------------------------- ### partition-before-pred: Partition list before elements satisfying a predicate Source: https://github.com/magnars/dash.el/blob/master/README.md Partitions a list directly before each element for which a predicate `pred` returns non-`nil`. The element satisfying the predicate starts the new sublist. ```el (-partition-before-pred #'booleanp ()) ;; => () (-partition-before-pred #'booleanp '(0 t)) ;; => ((0) (t)) (-partition-before-pred #'booleanp '(0 0 t 0 t t)) ;; => ((0 0) (t 0) (t) (t)) ``` -------------------------------- ### Elisp -iterate: Generate List by Iterated Application Source: https://github.com/magnars/dash.el/blob/master/README.md Returns a list of `n` iterated applications of a function `fun` starting from an initial value `init`. The sequence generated is `init`, `(fun init)`, `(fun (fun init))`, and so on. ```elisp (-iterate #'1+ 1 10) ;; => (1 2 3 4 5 6 7 8 9 10) (-iterate (lambda (x) (+ x x)) 2 5) ;; => (2 4 8 16 32) (--iterate (* it it) 2 5) ;; => (2 4 16 256 65536) ``` -------------------------------- ### Update Require Statements Source: https://github.com/magnars/dash.el/wiki/Obsoletion-of-dash-functional.el Illustrates the necessary changes to Emacs Lisp require statements when migrating from dash-functional to the integrated dash package. ```elisp ;; Replace with: ;; (require 'dash) ``` ```elisp ;; (require 'dash-functional) ``` -------------------------------- ### Emacs Lisp: -tree-reduce-from for Initialized Recursive Reduction Source: https://github.com/magnars/dash.el/blob/master/README.md The `-tree-reduce-from` function performs a recursive reduction on a tree, starting with an initial value. It applies the reduction function (`fn`) to the accumulator and each element, handling nested lists recursively. The initial value is used as the starting point for the accumulation process. ```emacs-lisp (-tree-reduce-from '+ 1 '(1 (1 1) ((1)))) ;; => 8 (--tree-reduce-from (-concat acc (list it)) nil '(1 (2 3 (4 5)) (6 7))) ;; => ((7 6) ((5 4) 3 2) 1) ``` -------------------------------- ### -first-item: Get the first item of a list Source: https://github.com/magnars/dash.el/blob/master/README.md Returns the first item of a list. Returns nil if the list is empty. This function can also be used to set the first item of a list. ```el (-first-item ()) ;; => () (-first-item '(1 2 3 4 5)) ;; => 1 (let ((list (list 1 2 3))) (setf (-first-item list) 5) list) ;; => (5 2 3) ``` -------------------------------- ### dash.el Map vs Anaphoric Map Example Source: https://github.com/magnars/dash.el/blob/master/README.md Demonstrates the usage of the standard -map function and its anaphoric counterpart --map. The standard version takes an explicit lambda function, while the anaphoric version uses the 'it' variable to refer to the current element. ```emacs-lisp (-map (lambda (n) (* n n)) '(1 2 3 4)) ; Normal version. (--map (* it it) '(1 2 3 4)) ; Anaphoric version. ``` -------------------------------- ### elisp: -reduce-from - Left-associative reduction with initial value Source: https://github.com/magnars/dash.el/blob/master/README.md Reduces a function across a list from left to right, starting with an initial value. It applies the function to the accumulator and each element sequentially. If the list is empty, it returns the initial value. ```el (-reduce-from #'- 10 '(1 2 3)) ;; => 4 (-reduce-from #'list 10 '(1 2 3)) ;; => (((10 1) 2) 3) (--reduce-from (concat acc " " it) "START" '("a" "b" "c")) ;; => "START a b c" ``` -------------------------------- ### elisp: -reductions-from - Intermediate values of left-associative reduction Source: https://github.com/magnars/dash.el/blob/master/README.md Returns a list of intermediate accumulator values generated by a left-associative reduction (like -reduce-from). It captures the state of the accumulator after each application of the reduction function, starting with the initial value. ```el (-reductions-from #'max 0 '(2 1 4 3)) ;; => (0 2 2 4 4) (-reductions-from #'* 1 '(1 2 3 4)) ;; => (1 1 2 6 24) (--reductions-from (format "(FN %s %d)" acc it) "INIT" '(1 2 3)) ;; => ("INIT" "(FN INIT 1)" "(FN (FN INIT 1) 2)" "(FN (FN (FN INIT 1) 2) 3)") ``` -------------------------------- ### Emacs Lisp: -take-while Predicate Source: https://github.com/magnars/dash.el/blob/master/README.md Takes successive items from a list for which a predicate function returns non-nil. The predicate is a function of one argument. Returns a new list of the successive elements from the start of the list that satisfy the predicate. ```elisp (-take-while #'even? '(1 2 3 4)) ;; => () (-take-while #'even? '(2 4 5 6)) ;; => (2 4) (--take-while (< it 4) '(1 2 3 4 3 2 1)) ;; => (1 2 3) ``` -------------------------------- ### Emacs Lisp: -slice List Slicing Source: https://github.com/magnars/dash.el/blob/master/README.md Returns a copy of a list, starting from an optional 'from' index to an optional 'to' index. Indices can be negative, interpreted modulo the list length. An optional 'step' argument allows returning only every Nth item. ```elisp (-slice '(1 2 3 4 5) 1) ;; => (2 3 4 5) (-slice '(1 2 3 4 5) 0 3) ;; => (1 2 3) (-slice '(1 2 3 4 5 6 7 8 9) 1 -1 2) ;; => (2 4 6 8) ``` -------------------------------- ### Elisp: Running product of list elements -running-product Source: https://github.com/magnars/dash.el/blob/master/README.md Returns a list containing the running products of items in the input list. The input list must be non-empty. ```elisp (-running-product '(1 2 3 4)) ;; => (1 2 6 24) (-running-product '(1)) ;; => (1) (-running-product ()) ;; Wrong type argument: consp, nil ``` -------------------------------- ### Elisp: Product of list elements -product Source: https://github.com/magnars/dash.el/blob/master/README.md Returns the product of all elements in a list. Handles empty lists by returning 1. ```elisp (-product ()) ;; => 1 (-product '(1)) ;; => 1 (-product '(1 2 3 4)) ;; => 24 ``` -------------------------------- ### Elisp: All prefixes of a list -inits Source: https://github.com/magnars/dash.el/blob/master/README.md Returns a list containing all prefixes of the input list, including the empty list and the list itself. ```elisp (-inits '(1 2 3 4)) ;; => (nil (1) (1 2) (1 2 3) (1 2 3 4)) (-inits nil) ;; => (nil) (-inits '(1)) ;; => (nil (1)) ``` -------------------------------- ### Update Package-Requires Header Source: https://github.com/magnars/dash.el/wiki/Obsoletion-of-dash-functional.el Demonstrates how to update the Package-Requires header in Emacs Lisp packages to reflect the new dependency on dash version 2.18.0 and remove the dependency on dash-functional. ```elisp ;; Package-Requires: ((dash "2.0.0") (dash-functional "1.2.0")) ``` ```elisp ;; Package-Requires: ((dash "2.18.0")) ``` -------------------------------- ### partition-all-in-steps: Partition list into n-sized sublists with step stride, allowing partial trailing groups Source: https://github.com/magnars/dash.el/blob/master/README.md Partitions a list into sublists of length `n` that are `step` items apart. Adjacent groups may overlap if `n` exceeds the `step` stride. Trailing groups may contain fewer than `n` items. ```el (-partition-all-in-steps 2 1 '(1 2 3 4)) ;; => ((1 2) (2 3) (3 4) (4)) (-partition-all-in-steps 3 2 '(1 2 3 4)) ;; => ((1 2 3) (3 4)) (-partition-all-in-steps 3 2 '(1 2 3 4 5)) ;; => ((1 2 3) (3 4 5) (5)) ``` -------------------------------- ### -last-item: Get the last item of a list Source: https://github.com/magnars/dash.el/blob/master/README.md Returns the last item of a list. Returns nil if the list is empty. This function can also be used to set the last item of a list. ```el (-last-item ()) ;; => () (-last-item '(1 2 3 4 5)) ;; => 5 (let ((list (list 1 2 3))) (setf (-last-item list) 5) list) ;; => (1 2 5) ``` -------------------------------- ### -fifth-item: Get the fifth item of a list Source: https://github.com/magnars/dash.el/blob/master/README.md Returns the fifth item of a list. Returns nil if the list is too short. This function can also be used to set the fifth item of a list. ```el (-fifth-item ()) ;; => () (-fifth-item '(1 2 3 4)) ;; => () (-fifth-item '(1 2 3 4 5)) ;; => 5 ``` -------------------------------- ### Binding Macros Source: https://github.com/magnars/dash.el/blob/master/README.md Macros that enhance `let` and `let*` bindings with destructuring capabilities and conditional flow control, simplifying variable assignment and conditional execution. ```APIDOC Binding Macros: -when-let (var val) &rest body Executes `body` if `val` is not nil, binding `var` to `val`. -when-let* (vars-vals &rest body) Executes `body` if all bindings in `vars-vals` are successful, binding variables. -if-let ((var val) then &rest else) If `val` is not nil, executes `then` binding `var` to `val`; otherwise, executes `else`. -if-let* (vars-vals then &rest else) If all bindings in `vars-vals` are successful, executes `then`; otherwise, executes `else`. -let (varlist &rest body) A macro for creating local bindings, similar to `let`. -let* (varlist &rest body) A macro for creating sequential local bindings, similar to `let*`. -lambda (match-form &rest body) Defines an anonymous function with pattern matching for arguments. -setq (match-form val) ... Assigns values to variables or data structures using pattern matching. ``` -------------------------------- ### -fourth-item: Get the fourth item of a list Source: https://github.com/magnars/dash.el/blob/master/README.md Returns the fourth item of a list. Returns nil if the list is too short. This function can also be used to set the fourth item of a list. ```el (-fourth-item ()) ;; => () (-fourth-item '(1 2 3)) ;; => () (-fourth-item '(1 2 3 4 5)) ;; => 4 ``` -------------------------------- ### -third-item: Get the third item of a list Source: https://github.com/magnars/dash.el/blob/master/README.md Returns the third item of a list. Returns nil if the list is too short. This function can also be used to set the third item of a list. ```el (-third-item ()) ;; => () (-third-item '(1 2)) ;; => () (-third-item '(1 2 3 4 5)) ;; => 3 ``` -------------------------------- ### Partitioning Functions Source: https://github.com/magnars/dash.el/blob/master/README.md Functions that partition the input list into a list of lists based on various criteria. ```APIDOC -split-at (n list) Splits a list into two parts at index `n`. Parameters: n: The index at which to split. list: The input list. Returns: A list containing two lists: the first `n` elements and the rest. ``` ```APIDOC -split-with (pred list) Splits a list into two parts: elements satisfying `pred` and those that don't. Parameters: pred: A function that takes an element and returns a boolean. list: The input list. Returns: A list containing two lists: elements satisfying `pred` and elements not satisfying `pred`. ``` ```APIDOC -split-on (item list) Splits a list into sublists based on occurrences of `item`. Parameters: item: The delimiter item. list: The input list. Returns: A list of sublists, split by `item`. ``` ```APIDOC -split-when (fn list) Splits a list into sublists based on when `fn` returns true for an element. Parameters: fn: A function that takes an element and returns a boolean. list: The input list. Returns: A list of sublists, split when `fn` is true. ``` ```APIDOC -separate (pred list) Similar to `split-with`, separates elements into two lists based on `pred`. Parameters: pred: A function that takes an element and returns a boolean. list: The input list. Returns: A list containing two lists: elements satisfying `pred` and elements not satisfying `pred`. ``` ```APIDOC -partition (n list) Partitions a list into sublists of size `n`. Parameters: n: The size of each partition. list: The input list. Returns: A list of sublists, each of size `n` (the last may be smaller). ``` ```APIDOC -partition-all (n list) Partitions a list into sublists of size `n`, including an empty last sublist if needed. Parameters: n: The size of each partition. list: The input list. Returns: A list of sublists, each of size `n`. ``` ```APIDOC -partition-in-steps (n step list) Partitions a list into sublists of size `n`, advancing by `step` each time. Parameters: n: The size of each partition. step: The number of elements to advance between partitions. list: The input list. Returns: A list of sublists. ``` ```APIDOC -partition-all-in-steps (n step list) Partitions a list into sublists of size `n`, advancing by `step` each time, including partial last sublists. Parameters: n: The size of each partition. step: The number of elements to advance between partitions. list: The input list. Returns: A list of sublists. ``` ```APIDOC -partition-by (fn list) Partitions a list into sublists based on the result of applying `fn` to each element. Parameters: fn: A function that returns a key for partitioning. list: The input list. Returns: A list of sublists, where each sublist contains elements that produced the same key. ``` ```APIDOC -partition-by-header (fn list) Partitions a list into sublists, using `fn` to determine headers and group subsequent elements. Parameters: fn: A function that identifies header elements. list: The input list. Returns: A list of sublists, where each sublist starts with a header. ``` ```APIDOC -partition-after-pred (pred list) Partitions a list into sublists, starting a new sublist after an element satisfying `pred`. Parameters: pred: A function that takes an element and returns a boolean. list: The input list. Returns: A list of sublists. ``` ```APIDOC -partition-before-pred (pred list) Partitions a list into sublists, starting a new sublist before an element satisfying `pred`. Parameters: pred: A function that takes an element and returns ``` -------------------------------- ### -second-item: Get the second item of a list Source: https://github.com/magnars/dash.el/blob/master/README.md Returns the second item of a list. Returns nil if the list is too short. This function can also be used to set the second item of a list. ```el (-second-item ()) ;; => () (-second-item '(1 2 3 4 5)) ;; => 2 (let ((list (list 1 2))) (setf (-second-item list) 5) list) ;; => (1 5) ``` -------------------------------- ### partition-in-steps: Partition list into n-sized sublists with step stride Source: https://github.com/magnars/dash.el/blob/master/README.md Partitions a list into sublists of length `n` that are `step` items apart. If the last group does not have enough items to reach size `n`, those items are discarded. ```el (-partition-in-steps 2 1 '(1 2 3 4)) ;; => ((1 2) (2 3) (3 4)) (-partition-in-steps 3 2 '(1 2 3 4)) ;; => ((1 2 3)) (-partition-in-steps 3 2 '(1 2 3 4 5)) ;; => ((1 2 3) (3 4 5)) ``` -------------------------------- ### partition-all: Group list items into n-sized sublists Source: https://github.com/magnars/dash.el/blob/master/README.md Groups items in a list into sublists of size `n`. The last group may contain fewer than `n` items. ```el (-partition-all 2 '(1 2 3 4 5 6)) ;; => ((1 2) (3 4) (5 6)) (-partition-all 2 '(1 2 3 4 5 6 7)) ;; => ((1 2) (3 4) (5 6) (7)) (-partition-all 3 '(1 2 3 4 5 6 7)) ;; => ((1 2 3) (4 5 6) (7)) ``` -------------------------------- ### -fix: Compute least fixpoint Source: https://github.com/magnars/dash.el/blob/master/README.md Computes the least fixpoint of a function `fn` starting with an initial input `list`. The function `fn` is called at least once, and results are compared using `equal`. ```el (-fix (lambda (l) (-non-nil (--mapcat (-split-at (/ (length it) 2) it) l))) '((1 2 3))) ;; => ((1) (2) (3)) (let ((l '((starwars scifi) (jedi starwars warrior)))) (--fix (-uniq (--mapcat (cons it (cdr (assq it l))) it)) '(jedi book))) ;; => (jedi starwars warrior scifi book) ``` -------------------------------- ### Emacs Lisp: -take List Prefix Source: https://github.com/magnars/dash.el/blob/master/README.md Returns a copy of the first 'n' items in a list. If the list contains 'n' items or fewer, the entire list is returned. Returns nil if 'n' is zero or less. ```elisp (-take 3 '(1 2 3 4 5)) ;; => (1 2 3) (-take 17 '(1 2 3 4 5)) ;; => (1 2 3 4 5) (-take 0 '(1 2 3 4 5)) ;; => () ``` -------------------------------- ### Elisp -repeat: Create List with Repeated Element Source: https://github.com/magnars/dash.el/blob/master/README.md Returns a new list of length `n` where each element is `x`. Returns `nil` if `n` is less than 1. ```elisp (-repeat 3 :a) ;; => (:a :a :a) (-repeat 1 :a) ;; => (:a) (-repeat 0 :a) ;; => () ``` -------------------------------- ### dash.el Map with Custom Function Example Source: https://github.com/magnars/dash.el/blob/master/README.md Shows how to use the -map function with a separately defined Emacs Lisp function. This approach is useful for more complex transformations or when reusing existing functions. ```emacs-lisp (defun my-square (n) "Return N multiplied by itself." (* n n)) (-map #'my-square '(1 2 3 4)) ``` -------------------------------- ### -let &as Alias Notation Source: https://github.com/magnars/dash.el/blob/master/README.md Introduces the '&as' syntax for naming the entire source structure while also destructuring its components. This works with lists, vectors, and maps. ```Emacs Lisp ;; List example (list &as a b c) (list 1 2 3) ;; Improper list example (bounds &as beg . end) (cons 1 2) ``` -------------------------------- ### -butlast: Get list excluding the last item Source: https://github.com/magnars/dash.el/blob/master/README.md Returns a new list containing all items from the original list except for the last one. Returns nil if the list has zero or one element. ```el (-butlast '(1 2 3)) ;; => (1 2) (-butlast '(1 2)) ;; => (1) (-butlast '(1)) ;; => nil ``` -------------------------------- ### -let Key/Value Store Destructuring Source: https://github.com/magnars/dash.el/blob/master/README.md Shows how -let can destructure key/value pairs from plists, alists, and hash tables. It binds values associated with specified keys to corresponding variables. ```Emacs Lisp ;; Plist destructuring (&plist key0 a0 ... keyN aN) ;; Alist destructuring (&alist key0 a0 ... keyN aN) ;; Hash table destructuring (&hash key0 a0 ... keyN aN) ``` -------------------------------- ### Check if list contains element (-contains?) Source: https://github.com/magnars/dash.el/blob/master/README.md Checks if a list contains a specific element using `equal` or a custom comparison function. Returns the tail of the list starting from the element if found, otherwise returns `nil`. ```elisp (-contains? '(1 2 3) 1) ;; => (1 2 3) (-contains? '(1 2 3) 2) ;; => (2 3) (-contains? '(1 2 3) 4) ;; => () ``` -------------------------------- ### Elisp -unfold: Build List from Seed Source: https://github.com/magnars/dash.el/blob/master/README.md Builds a list from a `seed` value using a function `fun`. The `fun` should return `nil` to stop, or a cons cell `(a . b)` where `a` is prepended and `b` is the new seed. ```elisp (-unfold (lambda (x) (unless (= x 0) (cons x (1- x)))) 10) ;; => (10 9 8 7 6 5 4 3 2 1) (--unfold (when it (cons it (cdr it))) '(1 2 3 4)) ;; => ((1 2 3 4) (2 3 4) (3 4) (4)) (--unfold (when it (cons it (butlast it))) '(1 2 3 4)) ;; => ((1 2 3 4) (1 2 3) (1 2) (1)) ``` -------------------------------- ### partition-after-item: Partition list after specific item Source: https://github.com/magnars/dash.el/blob/master/README.md Partitions a list directly after each occurrence of a specified `item`. The `item` is included in the preceding sublist. ```el (-partition-after-item 3 ()) ;; => () (-partition-after-item 3 '(1)) ;; => ((1)) (-partition-after-item 3 '(3)) ;; => ((3)) ``` -------------------------------- ### -let Macro Overview Source: https://github.com/magnars/dash.el/blob/master/README.md The -let macro binds variables according to a varlist and then evaluates the body. The varlist is a list of ('pattern' 'source') pairs, where each source is evaluated once before any symbols are bound. Sources are evaluated in parallel. ```Emacs Lisp (varlist &rest body) ``` -------------------------------- ### Emacs Lisp: -drop-while Predicate Source: https://github.com/magnars/dash.el/blob/master/README.md Drops successive items from a list for which a predicate function returns non-nil. The predicate is a function of one argument. Returns the tail of the list starting from its first element for which the predicate returns nil. ```elisp (-drop-while #'even? '(1 2 3 4)) ;; => (1 2 3 4) (-drop-while #'even? '(2 4 5 6)) ;; => (5 6) (--drop-while (< it 4) '(1 2 3 4 3 2 1)) ;; => (4 3 2 1) ``` -------------------------------- ### Threading Macros Source: https://github.com/magnars/dash.el/blob/master/README.md Macros that facilitate sequential data processing by threading expressions through function calls, improving readability and reducing boilerplate. ```APIDOC Threading Macros: -> (x &optional form &rest more) Threads the first argument `x` through a sequence of forms. ->> (x &optional form &rest more) Threads the first argument `x` through a sequence of forms, applying them as the last argument. --> (x &rest forms) Threads the first argument `x` through a sequence of forms, applying them as the first argument. -as-> (value variable &rest forms) Threads a `value` through a sequence of forms, binding it to `variable` in each form. -some-> (x &optional form &rest more) Threads `x` through forms, stopping if any intermediate result is nil. -some->> (x &optional form &rest more) Threads `x` through forms, stopping if any intermediate result is nil, applying as last argument. -some--> (expr &rest forms) Threads `expr` through forms, stopping if any intermediate result is nil, applying as first argument. -doto (init &rest forms) Threads `init` through forms, returning the final result of the last form. ``` -------------------------------- ### -let Vector Shorthand Source: https://github.com/magnars/dash.el/blob/master/README.md A syntactic sugar is provided for the common case where varlist contains only one ('pattern' 'source') element. The outer parentheses can be omitted by using a vector instead. ```Emacs Lisp (-let ((`pattern` `source`)) ...) becomes (-let [`pattern` `source`] ...) ``` -------------------------------- ### Anaphoric Macro Usage Example Source: https://github.com/magnars/dash.el/blob/master/readme-template.md Demonstrates the usage of dash.el's anaphoric macros (prefixed with '--') compared to standard functions (prefixed with '-'). The anaphoric version uses the 'it' variable to refer to the current element. ```Emacs Lisp (-map (lambda (n) (* n n)) '(1 2 3 4)) ; Normal version. (--map (* it it) '(1 2 3 4)) ; Anaphoric version. ``` -------------------------------- ### Elisp: Running sum of list elements -running-sum Source: https://github.com/magnars/dash.el/blob/master/README.md Returns a list containing the running sums of items in the input list. The input list must be non-empty. ```elisp (-running-sum '(1 2 3 4)) ;; => (1 3 6 10) (-running-sum '(1)) ;; => (1) (-running-sum ()) ;; Wrong type argument: consp, nil ``` -------------------------------- ### Test -pad with zero arguments (Elisp) Source: https://github.com/magnars/dash.el/blob/master/NEWS.md Confirms that the Dash Elisp function `-pad` can now be called with zero arguments, improving its flexibility. ```Elisp ; The function `-pad` can now be called with zero lists as arguments. ``` -------------------------------- ### List Powerset (Elisp) Source: https://github.com/magnars/dash.el/blob/master/README.md Returns the powerset of a given list, which is the set of all possible subsets, including the empty set and the list itself. The order of subsets is not guaranteed. ```elisp (-powerset ()) ;; => (nil) (-powerset '(x y)) ;; => ((x y) (x) (y) nil) (-powerset '(x y z)) ;; => ((x y z) (x y) (x z) (x) (y z) (y) (z) nil) ``` -------------------------------- ### elisp: -reduce-r-from - Right-associative reduction with initial value Source: https://github.com/magnars/dash.el/blob/master/README.md Reduces a function across a list from right to left, starting with an initial value. It applies the function to the current element and the accumulator sequentially, with arguments flipped compared to left-associative folds. If the list is empty, it returns the initial value. ```el (-reduce-r-from #'- 10 '(1 2 3)) ;; => -8 (-reduce-r-from #'list 10 '(1 2 3)) ;; => (1 (2 (3 10))) (--reduce-r-from (concat it " " acc) "END" '("a" "b" "c")) ;; => "a b c END" ``` -------------------------------- ### -let* Macro for Sequential Destructuring Bindings Source: https://github.com/magnars/dash.el/blob/master/README.md The -let* macro binds variables sequentially according to a varlist, where each source can refer to symbols bound by previous patterns in the same varlist. This is useful for naming intermediate structures during recursive destructuring. ```elisp (-let* (((a . b) (cons 1 2)) ((c . d) (cons 3 4))) (list a b c d)) ;; => (1 2 3 4) (-let* (((a . b) (cons 1 (cons 2 3))) ((c . d) b)) (list a b c d)) ;; => (1 (2 . 3) 2 3) (-let* (((&alist "foo" foo "bar" bar) (list (cons "foo" 1) (cons "bar" (list 'a 'b 'c)))) ((a b c) bar)) (list foo a b c bar)) ;; => (1 a b c (a b c)) ``` -------------------------------- ### partition-after-pred: Partition list after elements satisfying a predicate Source: https://github.com/magnars/dash.el/blob/master/README.md Partitions a list after each element for which a predicate `pred` returns non-`nil`. The element satisfying the predicate is included in the preceding sublist. ```el (-partition-after-pred #'booleanp ()) ;; => () (-partition-after-pred #'booleanp '(t t)) ;; => ((t) (t)) (-partition-after-pred #'booleanp '(0 0 t t 0 t)) ;; => ((0 0 t) (t) (0 t)) ``` -------------------------------- ### Reduction Functions Source: https://github.com/magnars/dash.el/blob/master/README.md Functions that reduce lists to a single value, which may also be a list (e.g., `inits`, `tails`). ```APIDOC -reduce-from (fn init list) Applies `fn` cumulatively to the elements of `list`, starting with `init`. Parameters: fn: A function that takes an accumulator and an element, returning the new accumulator. init: The initial value of the accumulator. list: The input list. Returns: The final accumulated value. ``` ```APIDOC -reduce-r-from (fn init list) Applies `fn` cumulatively to the elements of `list` from right to left, starting with `init`. Parameters: fn: A function that takes an element and an accumulator, returning the new accumulator. init: The initial value of the accumulator. list: The input list. Returns: The final accumulated value. ``` ```APIDOC -reduce (fn list) Applies `fn` cumulatively to the elements of `list`. The first element is used as the initial value. Parameters: fn: A function that takes two elements and returns their combination. list: The input list (must not be empty). Returns: The final accumulated value. ``` ```APIDOC -reduce-r (fn list) Applies `fn` cumulatively to the elements of `list` from right to left. The last element is used as the initial value. Parameters: fn: A function that takes two elements and returns their combination. list: The input list (must not be empty). Returns: The final accumulated value. ``` ```APIDOC -reductions-from (fn init list) Returns a list of successive reduced values from the left, starting with `init`. Parameters: fn: A function that takes an accumulator and an element, returning the new accumulator. init: The initial value of the accumulator. list: The input list. Returns: A list of intermediate and final accumulated values. ``` ```APIDOC -reductions-r-from (fn init list) Returns a list of successive reduced values from the right, starting with `init`. Parameters: fn: A function that takes an element and an accumulator, returning the new accumulator. init: The initial value of the accumulator. list: The input list. Returns: A list of intermediate and final accumulated values. ``` ```APIDOC -reductions (fn list) Returns a list of successive reduced values from the left, using the first element as the initial value. Parameters: fn: A function that takes two elements and returns their combination. list: The input list. Returns: A list of intermediate and final accumulated values. ``` ```APIDOC -reductions-r (fn list) Returns a list of successive reduced values from the right, using the last element as the initial value. Parameters: fn: A function that takes two elements and returns their combination. list: The input list. Returns: A list of intermediate and final accumulated values. ``` ```APIDOC -count (pred list) Counts the number of elements in `list` for which `pred` returns true. Parameters: pred: A function that takes an element and returns a boolean. list: The input list. Returns: The count of elements satisfying `pred`. ``` ```APIDOC -sum (list) Calculates the sum of elements in a list of numbers. Parameters: list: The input list of numbers. Returns: The sum of the elements. ``` ```APIDOC -running-sum (list) Returns a list of the cumulative sums of elements in a list of numbers. Parameters: list: The input list of numbers. Returns: A list of running sums. ``` ```APIDOC -product (list) Calculates the product of elements in a list of numbers. Parameters: list: The input list of numbers. Returns: The product of the elements. ``` ```APIDOC -running-product (list) Returns a list of the cumulative products of elements in a list of numbers. Parameters: list: The input list of numbers. Returns: A list of running products. ``` ```APIDOC -inits (list) Returns a list of all initial segments (prefixes) of a list. Parameters: list: The input list. Returns: A list of lists, where each inner list is a prefix of the input list. ``` ```APIDOC -tails (list) Returns a list of all final segments (suffixes) of a list. Parameters: list: The input list. Returns: A list of lists, where each inner list is a suffix of the input list. ``` ```APIDOC -common-prefix (&rest lists) Finds the longest common prefix among multiple lists. Parameters: &rest lists: Two or more lists. Returns: The common prefix list. ``` ```APIDOC -common-suffix (&rest lists) Finds the longest common suffix among multiple lists. Parameters: &rest lists: Two or more lists. Returns: The common suffix list. ``` ```APIDOC -min (list) Finds the minimum element in a list. Parameters: list: The input list. Returns: The minimum element. ``` ```APIDOC -min-by (comparator list) Finds the minimum element in a list using a custom comparator. Parameters: comparator: A function that takes two elements and returns -1, 0, or 1. list: The input list. Returns: The minimum element according to the comparator. ``` ```APIDOC -max (list) Finds the maximum element in a list. Parameters: list: The input list. Returns: The maximum element. ``` ```APIDOC -max-by (comparator list) Finds the maximum element in a list using a custom comparator. Parameters: comparator: A function that takes two elements and returns -1, 0, or 1. list: The input list. Returns: The maximum element according to the comparator. ``` ```APIDOC -frequencies (list) Calculates the frequency of each unique element in a list. Parameters: list: The input list. Returns: A list of pairs, where each pair is `(element count)`. ```