-
-
Save vel-inet/9ea002f5ec93f1183613ec137c0b0171 to your computer and use it in GitHub Desktop.
идиоматичный полиморфизм в powershell
This file contains 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
$PI = [Math]::PI | |
# два скриптблока, которые в ответ на параметры возвращают hashtable | |
$square = { | |
param ($name, $side) | |
@{ | |
Name = $name | |
Perimeter = ($side * 4) | |
Area = ($side * $side) | |
} | |
} | |
$circle = { | |
param ($name, $radius) | |
@{ | |
Name = $name | |
Perimeter = ($PI * 2 * $radius) | |
Area = ($PI * $radius * $radius) | |
} | |
} | |
# массив из хэштаблиц, которые получаются в результате вызовов скриптблоков | |
$examples = (& $square "sq" 3.0), (& $circle "ci" 2.0) | |
$examples | ForEach-Object { | |
Write-Host ("{0} {1:N2} {2:N2}" -f $_.Name, $_.Perimeter, $_.Area) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment