Created
March 29, 2013 03:45
-
-
Save wonderful-panda/5268637 to your computer and use it in GitHub Desktop.
VBでeach_slice
This file contains hidden or 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
| Imports System.Runtime.CompilerServices | |
| Namespace Hoge | |
| Class EachSliceEnumerator(Of T) | |
| Implements IEnumerator(Of T()) | |
| Private ReadOnly _internal As IEnumerator(Of T) | |
| Private ReadOnly _size As Integer | |
| Private _current As T() = Nothing | |
| Sub New(internal As IEnumerator(Of T), size As Integer) | |
| _internal = internal | |
| _size = size | |
| End Sub | |
| Public ReadOnly Property Current As T() Implements System.Collections.Generic.IEnumerator(Of T()).Current | |
| Get | |
| If _current Is Nothing Then | |
| Throw New InvalidOperationException() | |
| End If | |
| Return _current | |
| End Get | |
| End Property | |
| Public ReadOnly Property Current1 As Object Implements System.Collections.IEnumerator.Current | |
| Get | |
| Return Me.Current | |
| End Get | |
| End Property | |
| Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext | |
| Dim list As New List(Of T) | |
| For i As Integer = 0 To _size - 1 | |
| If _internal.MoveNext() Then | |
| list.Add(_internal.Current) | |
| Else | |
| Exit For | |
| End If | |
| Next | |
| If list.Count > 0 Then | |
| _current = list.ToArray() | |
| Return True | |
| Else | |
| _current = Nothing | |
| Return False | |
| End If | |
| End Function | |
| Public Sub Reset() Implements System.Collections.IEnumerator.Reset | |
| _internal.Reset() | |
| End Sub | |
| #Region "IDisposable Support" | |
| Private disposedValue As Boolean ' 重複する呼び出しを検出するには | |
| ' IDisposable | |
| Protected Overridable Sub Dispose(disposing As Boolean) | |
| If Not Me.disposedValue Then | |
| If disposing Then | |
| _internal.Dispose() | |
| End If | |
| End If | |
| Me.disposedValue = True | |
| End Sub | |
| Public Sub Dispose() Implements IDisposable.Dispose | |
| Dispose(True) | |
| GC.SuppressFinalize(Me) | |
| End Sub | |
| #End Region | |
| End Class | |
| Class EachSliceEnumerable(Of T) | |
| Implements IEnumerable(Of T()) | |
| Private ReadOnly _target As IEnumerable(Of T) | |
| Private ReadOnly _size As Integer | |
| Public Sub New(target As IEnumerable(Of T), size As Integer) | |
| _target = target | |
| _size = size | |
| End Sub | |
| Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of T()) Implements System.Collections.Generic.IEnumerable(Of T()).GetEnumerator | |
| Return New EachSliceEnumerator(Of T)(_target.GetEnumerator(), _size) | |
| End Function | |
| Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator | |
| Return Me.GetEnumerator() | |
| End Function | |
| End Class | |
| Public Module EachSliceModule | |
| ''' <summary> | |
| ''' シーケンスを指定されたサイズごとに分割する | |
| ''' </summary> | |
| ''' <param name="target">処理対象のシーケンス</param> | |
| ''' <param name="size">分割するサイズ</param> | |
| <Extension()> | |
| Public Function EachSlice(Of T)(target As IEnumerable(Of T), size As Integer) As IEnumerable(Of T()) | |
| Return New EachSliceEnumerable(Of T)(target, size) | |
| End Function | |
| End Module | |
| End Namespace |
This file contains hidden or 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
| Enumerable.Range(1, 10).EachSlice(3) | |
| > {1, 2, 3} | |
| > {4, 5, 6} | |
| > {7, 8, 9} | |
| > {10} | |
| みたいな |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment