Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. function map([scriptblock]$map, [Collections.IEnumerable]$x, $y) { $x.ForEach({& $map $_ $y}) }
  2.  
  3. # Two parameters
  4. map { param($x, $y) $x + $y } @(1,2,3) 10
  5.  
  6. # Anonymous function as a value
  7. $squareIt = { param($x) $x + $x }
  8. map $squareIt @(1,2,3)
  9.  
  10. # One parameter
  11. map { param($x) [math]::Pow($x,2) } @(1,2,3)
  12.  
  13. # Strings
  14. map { param($x,$y) ' ' * ($y - $x.Length) + $x } (echo bob chris jack) 12
  15.  
  16.  
  17. function fold([scriptblock]$fold, [Collections.IEnumerable]$x, $a) {
  18. $x.ForEach({
  19. $a = & $fold $a $_
  20. })
  21. return $a
  22. }
  23.  
  24. $sum = { param($a,$x) $a + $x }
  25.  
  26. # Basic aggregation
  27. fold $sum @(1,2,3)
  28.  
  29. # With initializer
  30. fold { param($a,$x) $a + $x } @(1,2,3) 1
  31.  
  32. # Combining functions
  33. fold $sum (map $squareIt @(1,2,3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement