Add support for multiple input arguments to -lambda

master
Matus Goljer 12 years ago
parent f0831d3ab9
commit 8f9fc4113d
  1. 2
      README.md
  2. 8
      dash.el
  3. 2
      dev/examples.el

@ -1822,7 +1822,7 @@ See `-let` for the description of destructuring mechanism.
```cl
(-map (-lambda ((x y)) (+ x y)) '((1 2) (3 4) (5 6))) ;; => '(3 7 11)
(-map (-lambda ([x y]) (+ x y)) '([1 2] [3 4] [5 6])) ;; => '(3 7 11)
(-map (-lambda ((&plist :a a :b b)) (+ a b)) '((:a 1 :b 2) (:a 3 :b 4) (:a 5 :b 6))) ;; => '(3 7 11)
(funcall (-lambda ((_ . a) (_ . b)) (-concat a b)) '(1 2 3) '(4 5 6)) ;; => '(2 3 5 6)
```

@ -1426,8 +1426,12 @@ See `-let' for the description of destructuring mechanism."
(symbolp (car match-form)))
`(lambda ,match-form ,@body))
(t
`(lambda (x)
(-let* ((,@match-form x)) ,@body)))))
(let* ((inputs (--map-indexed (list it (make-symbol (format "input%d" it-index))) match-form)))
;; TODO: because inputs to the lambda are evaluated only once,
;; -let* need not to create the extra bindings to ensure that.
;; We should find a way to optimize that. Not critical however.
`(lambda ,(--map (cadr it) inputs)
(-let* ,inputs ,@body))))))
(defun -distinct (list)
"Return a new list with all duplicates removed.

@ -757,8 +757,10 @@ new list."
(defexamples -lambda
(-map (-lambda ((x y)) (+ x y)) '((1 2) (3 4) (5 6))) => '(3 7 11)
(-map (-lambda ([x y]) (+ x y)) '([1 2] [3 4] [5 6])) => '(3 7 11)
(funcall (-lambda ((_ . a) (_ . b)) (-concat a b)) '(1 2 3) '(4 5 6)) => '(2 3 5 6)
(-map (-lambda ((&plist :a a :b b)) (+ a b)) '((:a 1 :b 2) (:a 3 :b 4) (:a 5 :b 6))) => '(3 7 11)
(-map (-lambda (x) (let ((k (car x)) (v (cadr x))) (+ k v))) '((1 2) (3 4) (5 6))) => '(3 7 11)
(funcall (-lambda ((a) (b)) (+ a b)) '(1 2 3) '(4 5 6)) => 5
(condition-case nil (progn (-lambda a t) (error "previous form should error")) (error t)) => t))
(def-example-group "Side-effects"

Loading…
Cancel
Save