zsh-users
 help / color / Atom feed
* A way to apply function to each element of an array
@ 2019-08-29  0:49 Sebastian Gniazdowski
  2019-08-29  2:47 ` Sebastian Gniazdowski
  2019-08-29 10:46 ` Roman Perepelitsa
  0 siblings, 2 replies; 4+ messages in thread
From: Sebastian Gniazdowski @ 2019-08-29  0:49 UTC (permalink / raw)
  To: Zsh Users

Hello,
I thought that I share a method of applying of function to each
element of an array. I think that it's obvious for someone interested
in // substitution and (#m) and (#b) flags, but not necessarily for
others. Also, it would be much more beautiful if zsh would support
mksh's substitution ${|func;} which is replaced by the value that func
assigns to REPLY, assuming that func can take some arguments (i.e. the
$MATCH in the use case):

msfunc() { REPLY="${(Q)1} $RANDOM"; return 0; }
functions -Ms msfunc 1 1 msfunc
myarr=( value1 value2 abc1 abc2 )
myarr=( "${(@)${myarr[@]/(#m)*/$(( msfunc(${(q)MATCH}) ))$REPLY}/(#s)0/}" )
print -rl $myarr

Output:
value1 21720
value2 18920
abc1 3318
abc2 14483

The (#s) matches the start of the string and is just for to ensure
correctness and not actually needed. It however might save some time
when debugging code, when some 0s start to disappear from the strings
because of an specific, not always matching pattern (with (#b) flag,
for example). The 0 can be also changed to some random number in such
case.

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
blog: http://zdharma.org

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

* Re: A way to apply function to each element of an array
  2019-08-29  0:49 A way to apply function to each element of an array Sebastian Gniazdowski
@ 2019-08-29  2:47 ` Sebastian Gniazdowski
  2019-08-29 10:46 ` Roman Perepelitsa
  1 sibling, 0 replies; 4+ messages in thread
From: Sebastian Gniazdowski @ 2019-08-29  2:47 UTC (permalink / raw)
  To: Zsh Users

On Thu, 29 Aug 2019 at 02:49, Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
> ...
> assigns to REPLY, assuming that func can take some arguments (i.e. the
> $MATCH in the use case):

This is not needed – the function could use $MATCH directly.

I was assuming that adding such substitution would require parser
alteration, however it doesn't seem to:

% echo ${|foo;}
zsh: bad substitution

So the syntax correctly passes the parser and meets paramsubst() or
stringsubst(). So adding such substitution should be easy – just a
doshfunc(() followed by getstrvalue("REPLY"). If there are any
pitfalls in it then let me know it.

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

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

* Re: A way to apply function to each element of an array
  2019-08-29  0:49 A way to apply function to each element of an array Sebastian Gniazdowski
  2019-08-29  2:47 ` Sebastian Gniazdowski
@ 2019-08-29 10:46 ` Roman Perepelitsa
  2019-08-29 11:13   ` Sebastian Gniazdowski
  1 sibling, 1 reply; 4+ messages in thread
From: Roman Perepelitsa @ 2019-08-29 10:46 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh Users

On Thu, Aug 29, 2019 at 2:51 AM Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> myarr=( "${(@)${myarr[@]/(#m)*/$(( msfunc(${(q)MATCH}) ))$REPLY}/(#s)0/}" )

I usually use ${foo+} to expand foo for its side effects and to ignore
the substituted value.

    myarr=( "${(@)${myarr[@]/(#m)*/${$((msfunc(${(q)MATCH})))+}$REPLY}}" )

This way you can discard the substituted value without knowing what it is.

Roman.

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

* Re: A way to apply function to each element of an array
  2019-08-29 10:46 ` Roman Perepelitsa
@ 2019-08-29 11:13   ` Sebastian Gniazdowski
  0 siblings, 0 replies; 4+ messages in thread
From: Sebastian Gniazdowski @ 2019-08-29 11:13 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Zsh Users

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

Cool, thanks! It makes the solution much more readable.

czw., 29 sie 2019, 12:47 użytkownik Roman Perepelitsa <
roman.perepelitsa@gmail.com> napisał:

> On Thu, Aug 29, 2019 at 2:51 AM Sebastian Gniazdowski
> <sgniazdowski@gmail.com> wrote:
> >
> > myarr=( "${(@)${myarr[@]/(#m)*/$(( msfunc(${(q)MATCH})
> ))$REPLY}/(#s)0/}" )
>
> I usually use ${foo+} to expand foo for its side effects and to ignore
> the substituted value.
>
>     myarr=( "${(@)${myarr[@]/(#m)*/${$((msfunc(${(q)MATCH})))+}$REPLY}}" )
>
> This way you can discard the substituted value without knowing what it is.
>
> Roman.
>

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

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-29  0:49 A way to apply function to each element of an array Sebastian Gniazdowski
2019-08-29  2:47 ` Sebastian Gniazdowski
2019-08-29 10:46 ` Roman Perepelitsa
2019-08-29 11:13   ` Sebastian Gniazdowski

zsh-users

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-users

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git