Arc Forumnew | comments | leaders | submitlogin
Curried Functions
3 points by fallintothis 4477 days ago | discuss
I can't imagine this is the first time the following has ever been done, but I'm still surprised at how easy it is.

  (mac curry (parms . body)
    (unless (all atom parms)
      (err "Can't curry optional/destructuring parameters:" parms))
    (w/uniq missing
      (let ex (afn (parms)
                (if parms
                    `(if (is ,(car parms) ',missing)
                         (fn ,parms ,@body)
                         ,(self (cdr parms)))
                    `(do ,@body)))
        `(fn (,@(map (fn (parm) `(o ,parm ',missing)) parms))
           ,(ex parms)))))

  (mac defcurry (name parms . body)
    `(safeset ,name (curry ,parms ,@body)))
Thus, we can explicitly make functions work when partially-applied:

  (defcurry same (prop a b)
    ((compare is prop) a b))

  arc> ((same) len "you" "me")
  nil
  arc> ((same len) "you" "me")
  nil
  arc> ((same len "you") "me")
  nil
  arc> (same len "you" "me")
  nil
  arc> ((same) len "him" "her")
  t
  arc> ((same len) "him" "her")
  t
  arc> ((same len "him") "her")
  t
  arc> (same len "him" "her")
  t
I may not have the time for bigger projects these days, but at least I can put up some random one-off hacks. :)