ruricolist / serapeum

Utilities beyond Alexandria
MIT License
420 stars 41 forks source link

Proposal: local function aliases #124

Closed mmontone closed 2 years ago

mmontone commented 2 years ago

Hello,

I'm thinking that perhaps having local function aliases would be a good utility to have.

It may be just me perhaps, but I often like to bind large function names to shorter, locally, like this:

(flet-aliases ((-> json:encode-object-member))
    (with-output-to-string (json:*json-output*)
      (json:with-object ()
    (-> :id (vs::video-id video))
    (-> :title (vs::title video))
    (-> :status (vs::status video)))))

or this:

(flet-aliases ((-> access:accesses))
    (list :id (-> video :id) 
           :title (-> video :title)
           :description (-> video :description)))

This is my flet-aliases definition:

(defmacro flet-aliases (bindings &body body)
  "Bind function aliases."
  `(flet ,(loop for binding in bindings
           collect `(,(first binding) (&rest args)
              (apply #',(second binding) args)))
     ,@body))

Do you think this is useful? Is there some other idiom for this? I have no idea if others are used to program like this.

Feel free to discard this :)

ruricolist commented 2 years ago

This can be done with fbind.

mmontone commented 2 years ago

Oh great. Thanks.