Skip to content

Instantly share code, notes, and snippets.

@agumonkey
Created August 10, 2014 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agumonkey/c2c33c88d5372e25312b to your computer and use it in GitHub Desktop.
Save agumonkey/c2c33c88d5372e25312b to your computer and use it in GitHub Desktop.
Everything is a function ? even structs and types ?
(setq lexical-binding t)
(defun make-struct (fields)
(lambda (values)
(let ((map (-zip fields values)))
(lambda (field)
(assoc field map)))))
;;; generic
(setq s (make-struct '(:a :b :c)))
(setq u (funcall s '(1 2 3)))
(funcall u :a)
(funcall u :b)
(funcall u :c)
(-map (lambda (f) (funcall u f)) '(:a :b :c)) ; u -> u
;;; specific
(setq Person (make-struct '(:name :age :city)))
(setq john (funcall Person '(John 23 London)))
(funcall john :age)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment