Skip to content

Instantly share code, notes, and snippets.

@peace2048
Created November 27, 2013 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peace2048/7673873 to your computer and use it in GitHub Desktop.
Save peace2048/7673873 to your computer and use it in GitHub Desktop.
T型のシーケンスを、任意サイズの配列のシーケンスに変える ref: http://qiita.com/peace2048/items/3ffd5dd1d5ddee4f6c06
Module EnumerableExBuffer
<Extension>
Public Function Buffer(Of T)(
ByVal source As IEnumerable(Of T),
ByVal count As Integer
) As IEnumerable(Of T())
If source Is Nothing Then Throw New ArgumentNullException("source")
If count < 1 Then Throw New ArgumentOutOfRangeException("count")
Return Buffer_(source, count)
End Function
Private Iterator Function Buffer_(Of T)(
ByVal source As IEnumerable(Of T),
ByVal count As Integer
) As IEnumerable(Of T())
Dim list = New List(Of T)(count)
For Each item In source
list.Add(item)
If list.Count = count Then
Yield list.ToArray()
list.Clear()
End If
Next
If list.Count > 0 Then
Yield list.ToArray()
End If
End Function
End Module
For Each a In Enumerable.Range(1, 10).Buffer(5)
For Each b In a
Console.Write("{0} ", b)
Next
Console.WriteLine()
Next
Dim s = Encoding.ASCII.GetString(
"414243444546"
.Buffer(2)
.Select(Function(a) New String(a))
.Select(Function(a) Convert.ToByte(a, 16))
.ToArray())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment