|
|||
|
How would you name
(defun unnamed (operator &rest functions) "Return a closure that applies OPERATOR on the value returned by calling functions on its single argument." (lambda (x) (apply operator (mapcar (lambda (f) (funcall f x)) functions)))) ? Examples: (unnamed #'+ #'f #'g) ; would be f+g in math (funcall (unnamed #'+ #'identity (lambda (x) (* 2 x))) 1) ; => 3 More general version: (defun unnamed2 (operator &rest functions-or-values) (lambda (x) (apply operator (mapcar (lambda (f) (if (functionp f) (funcall f x) f)) functions-or-values)))) (funcall (unnamed2 #'* 2 #'identity) 3) ; => 6 If this already exists in a library, especially with compiler macros etc, then even better :-) Best, Tamas |
|
|
||||
|
||||
|
|
|
|||
|
On Friday, April 13, 2012 3:54:10 PM UTC+2, Tamas wrote:
> How would you name > > (defun unnamed (operator &rest functions) > "Return a closure that applies OPERATOR on the value returned by calling > functions on its single argument." > (lambda (x) > (apply operator (mapcar (lambda (f) (funcall f x)) functions)))) > > ? > > Examples: > > (unnamed #'+ #'f #'g) ; would be f+g in math > > (funcall (unnamed #'+ #'identity (lambda (x) (* 2 x))) 1) ; => 3 > > More general version: > > (defun unnamed2 (operator &rest functions-or-values) > (lambda (x) > (apply operator (mapcar (lambda (f) > (if (functionp f) > (funcall f x) > f)) > functions-or-values)))) > > (funcall (unnamed2 #'* 2 #'identity) 3) ; => 6 > > If this already exists in a library, especially with compiler macros > etc, then even better :-) > > Best, > > Tamas Of course the WJ will point out that such a function already exists and has a name in Newlisp, Picolisp, Clojure, Minilisp and Intercalisp. I am less travelled and I would point out that one piece of your code is sometimes called MULTIPLEX. (defun multiplex (fns value) (mapcar (lambda (f) (funcall f value)) fns)) So that your UNNAMED becomes (defun unnamed (op &rest fns) (lambda (v) (apply op (multiplex fns v))) In any case no. I don't have a nice name for your UNNAMED. Maybe CONFS? As in "Convoluting Functions"? I am just making it up as I go ![]() Cheers -- MA |
|
|||
|
Tamas Papp <tkpapp@gmail.com> writes:
> How would you name > > (defun unnamed (operator &rest functions) > "Return a closure that applies OPERATOR on the value returned by calling > functions on its single argument." > (lambda (x) > (apply operator (mapcar (lambda (f) (funcall f x)) functions)))) > > ? > > Examples: > > (unnamed #'+ #'f #'g) ; would be f+g in math > > (funcall (unnamed #'+ #'identity (lambda (x) (* 2 x))) 1) ; => 3 (fapply #'+ #'f #'g) for functionnal-apply. > More general version: > > (defun unnamed2 (operator &rest functions-or-values) > (lambda (x) > (apply operator (mapcar (lambda (f) > (if (functionp f) > (funcall f x) > f)) > functions-or-values)))) > > (funcall (unnamed2 #'* 2 #'identity) 3) ; => 6 > > If this already exists in a library, especially with compiler macros > etc, then even better :-) You can also call it fapply; mathematically, you can consider numbers as constant functions. (fapply '+ 2 3) == (fapply '+ (constantly 2) (constantly 3)) -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}. |
|
|||
|
Marco Antoniotti <marcoxa@gmail.com> writes:
> I am less travelled and I would point out that one piece of your code > is sometimes called MULTIPLEX. > > (defun multiplex (fns value) > (mapcar (lambda (f) (funcall f value)) fns)) I hadn't seen it called "multiplex" before. What background is that from? Using the (newish) language, Factor, this operation is called "cleave": http://docs.factorcode.org/content/w...mbinators.html Not sure where that name came from either though... (maybe Forth?) The other part: > (defun unnamed (op &rest fns) > (lambda (v) (apply op (multiplex fns v))) is less commonly seen, I think, partly because of the usual 'arity problem. I'd sort of expect to see a reduce in here somewhere. Rupert -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) iJwEAQECAAYFAk+IRikACgkQRtd/pJbYVoYyZgP+Mmp5ivn1XWIErLmZDlzWgukX azNA7xkGj6ULEdxOFlHVhWKf11n1f9SDxG3RwN1wbn0IBHmssA MDcEnH7KGs36WQ iq7qSE8QLvDQVHCZDl/cyQdUSTrR3aQ/VtvjxammSqd69ao5QA/iLvowiJtINfAp 14FFkgkZ9EcrfKbd+Bc= =9enq -----END PGP SIGNATURE----- |
|
|||
|
On Apr 13, 4:37*pm, "Pascal J. Bourguignon" <p...@informatimago.com>
wrote: > You can also call it fapply; mathematically, you can consider numbers as > constant functions. > > (fapply '+ 2 3) == (fapply *'+ (constantly 2) (constantly 3)) What you do is "reduce" applied over "map" so I would call it reducemap or redumap or the like. |
|
|||
|
On 2012-04-13, Tamas Papp <tkpapp@gmail.com> wrote:
> How would you name > > (defun unnamed (operator &rest functions) > "Return a closure that applies OPERATOR on the value returned by calling > functions on its single argument." > (lambda (x) > (apply operator (mapcar (lambda (f) (funcall f x)) functions)))) > > ? This combinator implements a form of map-reduce. You might call it mapply, since it maps and then applies. |
|
|||
|
In article
<4595025.70.1334327546784.JavaMail.geo-discussion-forums@yno29>, Marco Antoniotti <marcoxa@gmail.com> wrote: > On Friday, April 13, 2012 3:54:10 PM UTC+2, Tamas wrote: > > How would you name > > > > (defun unnamed (operator &rest functions) > > "Return a closure that applies OPERATOR on the value returned by calling > > functions on its single argument." > > (lambda (x) > > (apply operator (mapcar (lambda (f) (funcall f x)) functions)))) > > > > ? > > > > Examples: > > > > (unnamed #'+ #'f #'g) ; would be f+g in math > > > > (funcall (unnamed #'+ #'identity (lambda (x) (* 2 x))) 1) ; => 3 > > > > More general version: > > > > (defun unnamed2 (operator &rest functions-or-values) > > (lambda (x) > > (apply operator (mapcar (lambda (f) > > (if (functionp f) > > (funcall f x) > > f)) > > functions-or-values)))) > > > > (funcall (unnamed2 #'* 2 #'identity) 3) ; => 6 > > > > If this already exists in a library, especially with compiler macros > > etc, then even better :-) > > > > Best, > > > > Tamas > > Of course the WJ will point out that such a function already exists and has a > name in Newlisp, Picolisp, Clojure, Minilisp and Intercalisp. > > I am less travelled and I would point out that one piece of your code is > sometimes called MULTIPLEX. > > (defun multiplex (fns value) > (mapcar (lambda (f) (funcall f value)) fns)) > > So that your UNNAMED becomes > > (defun unnamed (op &rest fns) > (lambda (v) (apply op (multiplex fns v))) > > In any case no. I don't have a nice name for your UNNAMED. Maybe CONFS? As > in "Convoluting Functions"? I am just making it up as I go ![]() How about APPLY-MULTIPLEX? -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** |
|
|||
|
On 2012-04-13, Kaz Kylheku <kaz@kylheku.com> wrote:
> On 2012-04-13, Tamas Papp <tkpapp@gmail.com> wrote: >> How would you name >> >> (defun unnamed (operator &rest functions) >> "Return a closure that applies OPERATOR on the value returned by calling >> functions on its single argument." >> (lambda (x) >> (apply operator (mapcar (lambda (f) (funcall f x)) functions)))) >> >> ? > > This combinator implements a form of map-reduce. You might call it mapply, > since it maps and then applies. I would tend toward leaving this in pieces. The construct does too much. (compose (mapf #'f #'g) (op #'apply #'+)) mapf gives us a function which distributes its argument over a bunch of functions, and collects the results into a list. op gives us partial evaluation: a function that invokes funcall #'apply #'+ and some additional arguments. chain composes them together. map to get the list, then pass it into the curried apply. Another thought: multiple values could be exploited here. The mapping could just return values. I.e. mapf could be a macro which expands to: (values (funcall f1) (funcall f2) ...) Then this is passed to + using multiple-value call. |
|
|||
|
In article
<11b16725-5fd9-40a1-be75-7c97b78e4315@h5g2000vbx.googlegroups.com>, kodifik <kodifik@eurogaran.com> wrote: > On Apr 13, 4:37*pm, "Pascal J. Bourguignon" <p...@informatimago.com> > wrote: > > You can also call it fapply; mathematically, you can consider numbers as > > constant functions. > > > > (fapply '+ 2 3) == (fapply *'+ (constantly 2) (constantly 3)) > > What you do is "reduce" applied over "map" > so I would call it reducemap or redumap or the like. MAP usually refers to running one function over a list of arguments. He's doing the inverse: running multiple functions over a single argument. -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** |
|
|||
|
On 4/13/2012 1:37 PM, Barry Margolin wrote:
> In article > <11b16725-5fd9-40a1-be75-7c97b78e4315@h5g2000vbx.googlegroups.com>, > kodifik<kodifik@eurogaran.com> wrote: > >> On Apr 13, 4:37 pm, "Pascal J. Bourguignon"<p...@informatimago.com> >> wrote: >>> You can also call it fapply; mathematically, you can consider numbers as >>> constant functions. >>> >>> (fapply '+ 2 3) == (fapply '+ (constantly 2) (constantly 3)) >> >> What you do is "reduce" applied over "map" >> so I would call it reducemap or redumap or the like. > > MAP usually refers to running one function over a list of arguments. > He's doing the inverse: running multiple functions over a single > argument. > ah, so PAM it should be |
|
|||
|
On Friday, April 13, 2012 9:06:51 PM UTC+2, Chris Riesbeck wrote:
> On 4/13/2012 1:37 PM, Barry Margolin wrote: > > In article > > <11b16725-5fd9-40a1-be75-7c97b78e4315@h5g2000vbx.googlegroups.com>, > > kodifik<kodifik@eurogaran.com> wrote: > > > >> On Apr 13, 4:37 pm, "Pascal J. Bourguignon" > > >> wrote: > >>> You can also call it fapply; mathematically, you can consider numbers as > >>> constant functions. > >>> > >>> (fapply '+ 2 3) == (fapply '+ (constantly 2) (constantly 3)) > >> > >> What you do is "reduce" applied over "map" > >> so I would call it reducemap or redumap or the like. > > > > MAP usually refers to running one function over a list of arguments. > > He's doing the inverse: running multiple functions over a single > > argument. > > > > ah, so PAM it should be 1+ MA |
|
|||
|
Tamas Papp wrote:
> How would you name > > (defun unnamed (operator &rest functions) > "Return a closure that applies OPERATOR on the value returned by calling > functions on its single argument." > (lambda (x) > (apply operator (mapcar (lambda (f) (funcall f x)) functions)))) > > ? > > Examples: > > (unnamed #'+ #'f #'g) ; would be f+g in math > > (funcall (unnamed #'+ #'identity (lambda (x) (* 2 x))) 1) ; => 3 > > More general version: > > (defun unnamed2 (operator &rest functions-or-values) > (lambda (x) > (apply operator (mapcar (lambda (f) > (if (functionp f) > (funcall f x) > f)) > functions-or-values)))) > > (funcall (unnamed2 #'* 2 #'identity) 3) ; => 6 > fork |
|
|||
|
Tamas Papp writes:
> How would you name > > (defun unnamed (operator &rest functions) > "Return a closure that applies OPERATOR on the value returned by calling > functions on its single argument." > (lambda (x) > (apply operator (mapcar (lambda (f) (funcall f x)) functions)))) > > ? > > Examples: > > (unnamed #'+ #'f #'g) ; would be f+g in math That is composition of functions. The outer function has arbitrary arity, so there can be many inner functions that accept the same arguments. The name compose itself is somewhat taken to mean (+ (f (g arg))) instead of (+ (f arg) (g arg)) but some variant name could be used. (composen #'p #'f #'g) to suggest that there are n inner functions? (compose* ...) to suggest repetition, as in Kleene start? (compose... #'p #'f #'g)? (compose-with-many-inner-functions #'p #'f #'g) Or (compose1 #'p #'f #'g) to suggest only one _composition_ so that the inner functions need to be combined in some _other_ way? |
|
|||
|
On Apr 13, 8:37*pm, Barry Margolin <bar...@alum.mit.edu> wrote:
> MAP usually refers to running one function over a list of arguments. > He's doing the inverse: running multiple functions over a single > argument. Yes, it USUALLY means that, but nothing forbids: (let ((x some-value)) (mapcar (lambda (fun) (funcall fun x)) list-of-funs)) |
|
|||
|
On Apr 16, 10:53*am, kodifik <kodi...@eurogaran.com> wrote:
> Yes, it USUALLY means that, but nothing forbids: > (let ((x some-value)) > * (mapcar (lambda (fun) (funcall fun x)) list-of-funs)) Perhaps we should be discussing what to call something that maps a value to a list of functions. My point is it should be called map: Nothing essentially different. |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|