Created
November 27, 2013 10:54
-
-
Save peace2048/7673873 to your computer and use it in GitHub Desktop.
T型のシーケンスを、任意サイズの配列のシーケンスに変える ref: http://qiita.com/peace2048/items/3ffd5dd1d5ddee4f6c06
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
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 |
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
For Each a In Enumerable.Range(1, 10).Buffer(5) | |
For Each b In a | |
Console.Write("{0} ", b) | |
Next | |
Console.WriteLine() | |
Next |
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
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