Skip to content

Instantly share code, notes, and snippets.

@4mitch
Created August 1, 2018 17:59
Show Gist options
  • Select an option

  • Save 4mitch/a68993d383b19766fa8b0705e198894b to your computer and use it in GitHub Desktop.

Select an option

Save 4mitch/a68993d383b19766fa8b0705e198894b to your computer and use it in GitHub Desktop.
Functional programming with Powershell
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