Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

name for higher order function

64 views
Skip to first unread message

Tamas Papp

unread,
Apr 13, 2012, 9:54:10 AM4/13/12
to
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

Marco Antoniotti

unread,
Apr 13, 2012, 10:32:26 AM4/13/12
to
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

Pascal J. Bourguignon

unread,
Apr 13, 2012, 10:37:04 AM4/13/12
to
Tamas Papp <tkp...@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 {}.

Rupert Swarbrick

unread,
Apr 13, 2012, 11:28:40 AM4/13/12
to
Marco Antoniotti <mar...@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/word-cleave,combinators.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

kodifik

unread,
Apr 13, 2012, 11:54:59 AM4/13/12
to
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.

Kaz Kylheku

unread,
Apr 13, 2012, 12:22:47 PM4/13/12
to
On 2012-04-13, Tamas Papp <tkp...@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.

Barry Margolin

unread,
Apr 13, 2012, 12:24:28 PM4/13/12
to
In article
<4595025.70.1334327546784.JavaMail.geo-discussion-forums@yno29>,
How about APPLY-MULTIPLEX?

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Kaz Kylheku

unread,
Apr 13, 2012, 12:40:05 PM4/13/12
to
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.

Barry Margolin

unread,
Apr 13, 2012, 2:37:27 PM4/13/12
to
In article
<11b16725-5fd9-40a1...@h5g2000vbx.googlegroups.com>,
MAP usually refers to running one function over a list of arguments.
He's doing the inverse: running multiple functions over a single
argument.

Chris Riesbeck

unread,
Apr 13, 2012, 3:06:51 PM4/13/12
to
On 4/13/2012 1:37 PM, Barry Margolin wrote:
> In article
> <11b16725-5fd9-40a1...@h5g2000vbx.googlegroups.com>,
> kodifik<kod...@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

Marco Antoniotti

unread,
Apr 13, 2012, 3:31:21 PM4/13/12
to
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...@h5g2000vbx.googlegroups.com>,
> > kodifik<kod...@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

WJ

unread,
Apr 13, 2012, 7:25:15 PM4/13/12
to
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

namekuseijin

unread,
Apr 14, 2012, 12:58:49 AM4/14/12
to
Em sexta-feira, 13 de abril de 2012 10h54min10s UTC-3, Tamas escreveu:
> 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:
>
> (funcall (unnamed #'+ #'identity (lambda (x) (* 2 x))) 1) ; => 3
> (funcall (unnamed2 #'* 2 #'identity) 3) ; => 6

I'd call it rube-goldberg, because it seems to me a unnecessarily complicated way to simply write:

(funcall (lambda (x) (+ x (* 2 x))) 1) ; => 3
(funcall (lambda (x) (* 2 x)) 3) ; => 6

you may save a few (f x) function calls, but it seems likely you be using lots of #'identity and (lambda ) definitions on-the-fly and in the end simply calling many (f x) in the above single body is simply shorter and more straightforward.

Jussi Piitulainen

unread,
Apr 14, 2012, 2:07:14 AM4/14/12
to
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?

kodifik

unread,
Apr 16, 2012, 4:53:53 AM4/16/12
to
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))

kodifik

unread,
Apr 16, 2012, 5:09:33 AM4/16/12
to
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.
0 new messages