Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Common Patterns and Idioms

Building Options Incrementally

(defun add-auth-headers (opts token)
  (cons `(header ,(++ "Authorization: Bearer " token))
        opts))

(defun add-timeout (opts ms)
  (cons `(timeout ,ms) opts))

;; Usage:
(-> '((method post))
    (add-timeout 5000)
    (add-auth-headers "abc123"))
;; ((header "Authorization: Bearer abc123") (timeout 5000) (method post))

Extracting Multiple Values

(defun get-credentials (opts)
  (let ((username (proplists:get_value 'username opts))
        (password (proplists:get_value 'password opts)))
    (tuple username password)))

Validating Required Options

(defun validate-opts (opts required-keys)
  (lists:all 
    (lambda (key) (proplists:is_defined key opts))
    required-keys))

;; Usage:
(validate-opts '((host "localhost") (port 8080)) 
               '(host port))
;; true

(validate-opts '((host "localhost")) 
               '(host port))
;; false

Converting Between Formats

;; Proplist to keyword list (for external APIs)
(defun to-keyword-list (proplist)
  (lists:flatmap
    (lambda (entry)
      (case entry
        ((tuple k v) (list (tuple ':key (atom_to_binary k 'utf8))
                           (tuple ':value v)))
        (atom (list (tuple ':key (atom_to_binary atom 'utf8))
                    (tuple ':value 'true)))))
    proplist))