chriskuech / functional Goto Github PK
View Code? Open in Web Editor NEWFunctional Programming functions for PowerShell
License: MIT License
Functional Programming functions for PowerShell
License: MIT License
The Merge-Object function pipes $input to Reduce-Object, but the $input variable is not created anywhere. It looks like it should be piping the $Object parameter of the Merge-Object function.
Consequently, when I ran the Merge-Object function, I was getting no output, because it is always sending $null to Reduce-Object.
I want to use the functional "reduce" function to calculate things, such as the factorial of a number. e.g. The factorial of 5 is 5x4x3x2x1 = 120. In Ruby I can do it with something like:
# factorial1.rb
(1..5).reduce{|accumulator,element| accumulator*element}
In Powershell I would do something like:
# factorial.ps
# ------ Install this library before using the "reduce" function
# Install-Module -Name functional
1..5 | reduce {$a*$b}
In both languages, the answer is 120, and the accumulator is assumed to be an integer, with an
initial value of the first element in the range.
If I redefined the factorial to have an initial value of 10, in Ruby I
would code it as
# factorial2.rb
# (1..5).reduce {|accumulator,element| accumulator*element} #uninitialized accumulator
(1..5).reduce(10){|accumulator,element| accumulator*element}
This yields the expected answer of 1200.
QUESTION 1: How does one initialize a "reduce" accumulator in Powershell?
In the next scenario, I want to generate a list of factorials. If
I were to code it in Ruby, I could initialize the reduce accumulator
with [1] (because 0! is 1), resulting in the following code:
# factorial.rb
# (1..5).reduce {|accumulator,element| accumulator*element} #uninitialized accumulator
(1..5).reduce([1]){|accumulator,element| accumulator << accumulator[-1]*element}
The output looks something like
=> [1, 1, 2, 6, 24, 120]
QUESTION 2: How does one use an expandible array to initialize the accumulator for the
Powershell "reduce" function of the "functional" module?
Scoping rules are explained the opposite of how they actually work.
If Test-All
is true, then Test-Any
should automatically be true too
@() | functional\Test-All
@() | functional\Test-Any
@() | functional\Test-All
True
@() | functional\Test-Any
False
Merge-Object @{a = @{b = 1}} @{a = @{c = 2}} -Strategy Fail
Merge-Object : A positional parameter cannot be found that accepts argument 'System.Collections.Hashtable'.
At line:1 char:1
+ Merge-Object @{a = @{b = 1}} @{a = @{c = 2}} -Strategy Fail
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Merge-Object], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Merge-Object
I found reading the pester tests helpful.
How would you do compare-object?
It would be really helpful to generalize the "Reduce-Object", adding also an initial value inline to be used as accumulator.
I'm not quite sure on how would it be the idiomatic way to go with Powershell.
Comparing Ordered-dictionaries using recursiveEquality does not work.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.