| For those who don't know, point free style is a technique common in languages like Haskell and can greatly increase code brevity, if combined with other higher-order function manipulations. I propose def should be modified to support point-free programming.
In point-free style, instead of declaring all the parameters to your function, you can declare fewer and instead have your function return another function that accepts the remaining parameters.
Here is a new version (I'm calling "defx" for now) that supports this: (mac defx (name args . rest)
(let wild [is _ '_]
(let gens ()
(repeat (count wild args) (push (uniq) gens))
(if gens
(let g (copy gens)
(let args_nu (map [if (wild _)
(pop g)
_]
args)
`(def ,name ,args_nu
((do ,@rest) ,@gens))))
`(def ,name ,args
,@rest)))))
To write a function in point-free style using my defx, simply replace any arguments you wish to leave undeclared by "_"- Note that they can be parameters located anywhere in the parameter list. If no "_" exist in the arguments, defx works exactly like the regular def.Here is the splitn function in arc.arc: (def splitn (n xs)
(let acc nil
((afn (n xs)
(if (or (no xs) (<= n 0))
(list (rev acc) xs)
(do (push (car xs) acc)
(self (- n 1) (cdr xs)))))
n xs)))
Here is the shorter version of the function modified to take advantage of a point-free style: (defx splitn (_ _)
(let acc nil
(afn (n xs)
(if (or (no xs) (<= n 0))
(list (rev acc) xs)
(do (push (car xs) acc)
(self (- n 1) (cdr xs)))))))
(Note that there are bigger payoffs for pfs if combined with other techniques that I'm not getting into here)-Conrad Barski |