CURRY-COMPOSE-READER-MACROS - concise function partial application and composition


 

Abstract

CURRY-COMPOSE-READER-MACROS exports a single function which may be called to enable reading of {} braces as partial function application and [] brackets as function composition.

The code is available under the GNU General Public License.

Source: http://github.com/eschulte/curry-compose-reader-macros.
 

Contents

  1. Source
  2. Introduction
  3. The CURRY-COMPOSE-READER-MACROS dictionary
    1. enable-curry-compose-reader-macros
  4. Acknowledgements

 

Source

CURRY-COMPOSE-READER-MACROS together with this documentation can be found at http://github.com/eschulte/curry-compose-reader-macros.
 

Introduction

Reader macros for concise expression of function partial application and composition.

These reader macros expand into the curry, rcurry and compose functions from the Alexandria library. The contents of curly brackets are curried and the contents of square brackets are composed. The _ symbol inside curly brackets changes the order of arguments with rcurry.

The following examples demonstrate usage.

;; partial application `curry`
(mapcar {+ 1} '(1 2 3 4)) ; => (2 3 4 5)

;; alternate order of arguments `rcurry`
(mapcar {- _ 1} '(1 2 3 4)) ; => (0 1 2 3)

;; function composition
(mapcar [#'list {* 2}] '(1 2 3 4)) ; => ((2) (4) (6) (8))

Call enable-curry-compose-reader-macros from within eval-when to ensure that reader macros are defined for both compilation and execution.

(eval-when (:compile-toplevel :load-toplevel :execute)
  (enable-curry-compose-reader-macros))

Emacs users may easily treat {}'s and []'s like parenthesis for paredit commands and SEXP movement with the following configuration.

;; Syntax table
(modify-syntax-entry ?\[ "(]" lisp-mode-syntax-table)
(modify-syntax-entry ?\] ")[" lisp-mode-syntax-table)
(modify-syntax-entry ?\{ "(}" lisp-mode-syntax-table)
(modify-syntax-entry ?\} "){" lisp-mode-syntax-table)

;; Paredit keys
(eval-after-load "paredit"
  '(progn
    (define-key paredit-mode-map "[" 'paredit-open-parenthesis)
    (define-key paredit-mode-map "]" 'paredit-close-parenthesis)
    (define-key paredit-mode-map "(" 'paredit-open-bracket)
    (define-key paredit-mode-map ")" 'paredit-close-bracket)
    (define-key paredit-mode-map "{" 'paredit-open-curly)
    (define-key paredit-mode-map "}" 'paredit-close-curly)))

 

The CURRY-COMPOSE-READER-MACROS dictionary


[Function]
enable-curry-compose-reader-macros => result


Enable concise syntax for Alexandria's curry, rcurry and compose. Calls to this function should be wrapped in eval-when as shown below to ensure evaluation during compilation.
    (eval-when (:compile-toplevel :load-toplevel :execute)
      (enable-curry-compose-reader-macros))

 

Acknowledgements

This documentation was prepared with a hacked up version of DOCUMENTATION-TEMPLATE.