Skip to content

Instantly share code, notes, and snippets.

@shiguruikai
Created April 12, 2023 12:27
Show Gist options
  • Save shiguruikai/ee08f19acbe6a0f683291f32342670f5 to your computer and use it in GitHub Desktop.
Save shiguruikai/ee08f19acbe6a0f683291f32342670f5 to your computer and use it in GitHub Desktop.
PowerShellでwindowed関数的なやつ。
[OutputType("System.Array")]
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[psobject]$InputObject,
[Parameter(Mandatory, Position = 0)]
[int]$Size,
[Parameter()]
[int]$Step = 1
)
begin {
if ($Size -le 0 -or $Step -le 0) {
throw "Both size and step must be greater than 0."
}
$buffer = [System.Collections.Generic.List[psobject]]::new()
}
process {
$InputObject | ForEach-Object {
[void]$buffer.Add($_)
}
}
end {
return 0..(($buffer.Count - $Size) / $Step) `
| ForEach-Object { $_ * $Step } `
| ForEach-Object { , $buffer[$_..($_ + $Size - 1)] }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment