Skip to content

Instantly share code, notes, and snippets.

@vel-inet
Created March 21, 2024 12:57
Show Gist options
  • Save vel-inet/9ea002f5ec93f1183613ec137c0b0171 to your computer and use it in GitHub Desktop.
Save vel-inet/9ea002f5ec93f1183613ec137c0b0171 to your computer and use it in GitHub Desktop.
идиоматичный полиморфизм в powershell
$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