zsh-users
 help / color / mirror / code / Atom feed
* Higher order functions in zsh (article link)
@ 2013-03-07  5:25 rahul
  2013-03-07 16:59 ` Christopher Browne
  2013-03-08  8:40 ` Stephen Blott
  0 siblings, 2 replies; 5+ messages in thread
From: rahul @ 2013-03-07  5:25 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 153 bytes --]

THis has been posted on reddit programming, thought I'd share with you.

http://yannesposito.com/Scratch/en/blog/Higher-order-function-in-zsh/index.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Higher order functions in zsh (article link)
  2013-03-07  5:25 Higher order functions in zsh (article link) rahul
@ 2013-03-07 16:59 ` Christopher Browne
  2013-03-07 19:08   ` Phil Pennock
  2013-03-08  8:40 ` Stephen Blott
  1 sibling, 1 reply; 5+ messages in thread
From: Christopher Browne @ 2013-03-07 16:59 UTC (permalink / raw)
  To: rahul; +Cc: Zsh Users

On Thu, Mar 7, 2013 at 12:25 AM, rahul <rahul2012@gmail.com> wrote:
> THis has been posted on reddit programming, thought I'd share with you.
>
> http://yannesposito.com/Scratch/en/blog/Higher-order-function-in-zsh/index.html

Seems promising.  I added in notes about a case on which it breaks, at this
point.

> touch f1 f2\ with\ space f3\'with\'quotes
> map echo *
f1
f2 with space
f3withquote

The "obvious" change to map is thus:

cbbrowne@cbbrowne /tmp/maptest> function map {
    local func_name=$1
    shift
    for elem in $@; print -- $(eval $func_name "${elem}")
}

But somewhat curiously that doesn't help :-(.
-- 
When confronted by a difficult problem, solve it by reducing it to the
question, "How would the Lone Ranger handle this?"


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Higher order functions in zsh (article link)
  2013-03-07 16:59 ` Christopher Browne
@ 2013-03-07 19:08   ` Phil Pennock
  2013-03-07 21:12     ` Oliver Kiddle
  0 siblings, 1 reply; 5+ messages in thread
From: Phil Pennock @ 2013-03-07 19:08 UTC (permalink / raw)
  To: Christopher Browne; +Cc: rahul, Zsh Users

On 2013-03-07 at 11:59 -0500, Christopher Browne wrote:
> cbbrowne@cbbrowne /tmp/maptest> function map {
>     local func_name=$1
>     shift
>     for elem in $@; print -- $(eval $func_name "${elem}")
> }
> 
> But somewhat curiously that doesn't help :-(.

Why does this code have an eval in there?  It's:
 (1) breaking the f3 example
 (2) introducing a security flaw

Hint:  touch 'f4 `mkdir snert`'

This works fine:
----------------------------8< cut here >8------------------------------
function map {
	local func_name=$1
	shift
	for elem in "$@"; print -- $($func_name "${elem}") 
}
----------------------------8< cut here >8------------------------------

With the eval in there, with { map echo * } f3 loses the quotes and f4
just shows "f4" and creates a new sub-directory, snert.

Lose the eval and things work, no new security hole.

Oh, and you need to quote the $@ to "$@" if you want to preserve empty
elements -- whether you do or not depends on what you're mapping across,
but I tend to think "present but empty" is distinct from "not present".

-Phil


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Higher order functions in zsh (article link)
  2013-03-07 19:08   ` Phil Pennock
@ 2013-03-07 21:12     ` Oliver Kiddle
  0 siblings, 0 replies; 5+ messages in thread
From: Oliver Kiddle @ 2013-03-07 21:12 UTC (permalink / raw)
  To: Zsh Users; +Cc: Christopher Browne, rahul

Phil Pennock wrote:
> Why does this code have an eval in there?  It's:

I suspect that the eval was there to allow the command to include
arguments: e.g. map 'print -l' *
Using $=func_name will add word-splitting to get that back without
reintroducing the security flaw.

> Oh, and you need to quote the $@ to "$@" if you want to preserve empty
> elements -- whether you do or not depends on what you're mapping across,
> but I tend to think "present but empty" is distinct from "not present".

Actually "$@" is the default for a for loop so you don't need it. And I
can't see that the use of print and command-substitution gets you
anything unless I'm missing something. So:

function map {
    local func_name=$1 elem
    shift
    for elem; $=func_name "${elem}"        
} 

This is unrelated but it seems the warncreateglobal option does't warn
about variables created using a for loop. Does anyone know if that is
intentional or a bug?

Oliver


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Higher order functions in zsh (article link)
  2013-03-07  5:25 Higher order functions in zsh (article link) rahul
  2013-03-07 16:59 ` Christopher Browne
@ 2013-03-08  8:40 ` Stephen Blott
  1 sibling, 0 replies; 5+ messages in thread
From: Stephen Blott @ 2013-03-08  8:40 UTC (permalink / raw)
  To: Zsh Users

On Thu, Mar 07, 2013 at 10:55:01AM +0530, rahul wrote:
>THis has been posted on reddit programming, thought I'd share with you.
>
>http://yannesposito.com/Scratch/en/blog/Higher-order-function-in-zsh/index.html
---end quoted text---

The examples look like those of a Java programmer translating their Java 
programming style to shell programming.  They can be better solved with 
zargs or, if you insist on a Posix shell, then pipes.  Pipes in Unix-like 
systems play exactly the role of map, filter and reduce in functional 
systems.

That's not to say that there's no merit to the idea.  I'd just like to see 
more compelling examples.

Steve


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-03-08  8:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-03-07  5:25 Higher order functions in zsh (article link) rahul
2013-03-07 16:59 ` Christopher Browne
2013-03-07 19:08   ` Phil Pennock
2013-03-07 21:12     ` Oliver Kiddle
2013-03-08  8:40 ` Stephen Blott

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).