Source

lisp-random / function_notation.lisp

;; Dispatch on the `{' character to LAMBDA-BRACE
(set-macro-character
 #\{
 (lambda (stream char)
   (declare (ignore char))
   (let* ((lst (read-delimited-list #\} stream t)))
     (lambda-brace lst))))

(defun lambda-brace (elements)
  ;; Collect all of the variables of the lambda
  (let ((args (loop :while (and (not (eq (car elements) '->))
                                elements)
                    :collect (prog1 (car elements)
                               (setf elements (cdr elements))))))
    ;; Construct the lambda
    (if (null elements)
        `(lambda () ,@args)
      `(lambda (,@args) ,@(cdr elements)))))

;; Let `]' be equivalent to `)'
(set-macro-character
 #\}
 (get-macro-character #\)))

;;; EXAMPLES
;; CL-USER> (mapcar {x -> (1+ x)} '(1 2 3 4))
;; (2 3 4 5)
;; CL-USER> (mapcar {x y -> (+ (expt x 2) (expt y 2))} '(1 2 3 4) '(5 6 7 8))
;; (26 40 58 80)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.