Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.lisp

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 04-13-2012, 01:54 PM
Tamas Papp
Guest
 
Posts: n/a
Default name for higher order function

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
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 04-13-2012, 02:32 PM
Marco Antoniotti
Guest
 
Posts: n/a
Default Re: name for higher order function

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
Reply With Quote
  #3 (permalink)  
Old 04-13-2012, 02:37 PM
Pascal J. Bourguignon
Guest
 
Posts: n/a
Default Re: name for higher order function

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 {}.
Reply With Quote
  #4 (permalink)  
Old 04-13-2012, 03:28 PM
Rupert Swarbrick
Guest
 
Posts: n/a
Default Re: name for higher order function

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-----
Reply With Quote
  #5 (permalink)  
Old 04-13-2012, 03:54 PM
kodifik
Guest
 
Posts: n/a
Default Re: name for higher order function

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.
Reply With Quote
  #6 (permalink)  
Old 04-13-2012, 04:22 PM
Kaz Kylheku
Guest
 
Posts: n/a
Default Re: name for higher order function

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.
Reply With Quote
  #7 (permalink)  
Old 04-13-2012, 04:24 PM
Barry Margolin
Guest
 
Posts: n/a
Default Re: name for higher order function

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 ***
Reply With Quote
  #8 (permalink)  
Old 04-13-2012, 04:40 PM
Kaz Kylheku
Guest
 
Posts: n/a
Default Re: name for higher order function

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.
Reply With Quote
  #9 (permalink)  
Old 04-13-2012, 06:37 PM
Barry Margolin
Guest
 
Posts: n/a
Default Re: name for higher order function

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 ***
Reply With Quote
  #10 (permalink)  
Old 04-13-2012, 07:06 PM
Chris Riesbeck
Guest
 
Posts: n/a
Default Re: name for higher order function

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
Reply With Quote
  #11 (permalink)  
Old 04-13-2012, 07:31 PM
Marco Antoniotti
Guest
 
Posts: n/a
Default Re: name for higher order function

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
Reply With Quote
  #12 (permalink)  
Old 04-13-2012, 11:25 PM
WJ
Guest
 
Posts: n/a
Default Re: name for higher order function

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
Reply With Quote
  #13 (permalink)  
Old 04-14-2012, 06:07 AM
Jussi Piitulainen
Guest
 
Posts: n/a
Default Re: name for higher order function

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?
Reply With Quote
  #14 (permalink)  
Old 04-16-2012, 08:53 AM
kodifik
Guest
 
Posts: n/a
Default Re: name for higher order function

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))
Reply With Quote
  #15 (permalink)  
Old 04-16-2012, 09:09 AM
kodifik
Guest
 
Posts: n/a
Default Re: name for higher order function

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.
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 06:21 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.