Created
August 1, 2018 17:59
-
-
Save 4mitch/a68993d383b19766fa8b0705e198894b to your computer and use it in GitHub Desktop.
Functional programming with Powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Convert-ByFilter($values, $predicate) { | |
| return $values | where { & $predicate $_ } | |
| } | |
| Describe "Higher Order Functions" { | |
| $values = @(1, 2, 3, 4) | |
| It "Should filter even" { | |
| $evenPredicate = { param($value) return $value % 2 -eq 0 } | |
| $even = Convert-ByFilter $values $evenPredicate | |
| $even.Length | Should Be 2 | |
| $even[0] | Should Be 2 | |
| $even[1] | Should Be 4 | |
| } | |
| It "Should filter odd" { | |
| $oddPredicate = { param($value) return $value % 2 -eq 1 } | |
| $odd = Convert-ByFilter $values $oddPredicate | |
| $odd.Length | Should Be 2 | |
| $odd[0] | Should Be 1 | |
| $odd[1] | Should Be 3 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment