functional programming - Trying to get higher order functions to work in powershell -
i can't example work
{ $_ + $_ }, { $_ + 1}, {$_ - 1} | % { $_ 1 }
i want construct list/array/collection/whatever of functions (this part fine), , pipe list code block on right, applies each of functions argument 1 , returns array results (namely; 2,2,0). i've tried using getnewclosure() method, & operator, nothing's worked far.
the dollar underbar variable automatic variable populated in scenarios; in case, when there incoming object pipeline. in order give $_
value of 1, appears intent, you'll have pipe number each , execute scriptblock. easiest way pass directly argument %
(foreach-object
) accepts scriptblock:
ps> { $_ + $_ }, { $_ + 1}, {$_ - 1} | % { 1 | % $_ } 2 2 0
look @ until makes sense :) if doesn't click, comment here , i'll explain in more detail.
if you're interested in functional tinkering in powershell, might enjoy function wrote partial application:
http://www.nivot.org/blog/post/2010/03/11/powershell20partialapplicationoffunctionsandcmdlets
it's bit unwieldy, still possible.
Comments
Post a Comment