Inventing The Wheel, Simple Practice: SkipTakeFilter(Of T)

The below class represents a simple trail to understand how to bypasses a specified number of elements in a sequence/list/array and then returns the remaining elements.

Public Class SkipTakeFilter(Of TValue)
    Implements IEnumerable(Of TValue)
    Private skip As Integer
    Private take As Integer
    Private values As IEnumerable(Of TValue)

    Public Sub New(values As IEnumerable(Of TValue), take As Integer)
        Me.New(values, 0, take)
    End Sub

    Public Sub New(values As IEnumerable(Of TValue), skip As Integer, take As Integer)
        Me.values = values
        Me.skip = skip
        Me.take = take
    End Sub

    Public Function GetEnumerator() As IEnumerator(Of TValue) Implements IEnumerable(Of TValue).GetEnumerator
        Return New Filter(Of TValue)(Me.values.GetEnumerator(), skip, take)
    End Function

    Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
        Return Me.GetEnumerator
    End Function

    Private Class Filter(Of T)
        Implements IEnumerator(Of T)

        Private values As IEnumerator(Of T)
        Private skip As Integer
        Private take As Integer

        Public Sub New(values As IEnumerator(Of T), skip As Integer, take As Integer)
            Me.values = values
            Me.skip = skip
            Me.take = take
        End Sub

        Public ReadOnly Property Current As T Implements IEnumerator(Of T).Current
            Get
                Return values.Current
            End Get
        End Property

        Private ReadOnly Property IEnumerator_Current As Object Implements IEnumerator.Current
            Get
                Return Me.Current
            End Get
        End Property

        Public Sub Reset() Implements IEnumerator.Reset
            Throw New InvalidOperationException()
        End Sub

        Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
            If take = 0 Then
                Return False
            End If
            While Not skip = 0
                If Not values.MoveNext() Then
                    Return False
                End If
                skip -= 1
            End While
            If Not values.MoveNext() Then
                Return False
            End If
            take -= 1
            Return True
        End Function

        Public Sub Dispose() Implements IDisposable.Dispose
            values.Dispose()
        End Sub

    End Class

End Class

Usage:

 1- define the list or array or sequence

        Dim list As New List(Of String) From {"ahmed", "alaa", "mostfa", "omar", "reda", "memo"}
        Dim strings As String() = New String() {"Ayman", "Ali", "Farid", "Reda", "Mohammed"}

2- Skip or take elements from sequence as per below examples

        Example : 1
        ' Obtain the first 3 items in list
        Dim skf1 As New SkipTakeFilter(Of String)(list, 3)
        For Each v As String In skf1
            Me.ListBox1.Items.Add(v)
        Next



         Example : 2
        ' Obtain  all items in list
        Dim skf2 As New SkipTakeFilter(Of String)(list, 0, list.Count - 1)
        For Each i As String In skf2
            Me.ListBox2.Items.Add(i)
        Next

         Example : 3
        ' skip the first 3 items and obtain other items
        Dim skf3 As New SkipTakeFilter(Of String)(strings.ToList(), 3, list.Count - 1)
        For Each k As String In skf3
            Me.ListBox3.Items.Add(k)
        Next

          Example : 4
         ' skip the first item and obtain the next 2 items
        Dim skf4 As New SkipTakeFilter(Of String)(list.ToList(), 1, 2)
        For Each j As String In skf4
            Me.ListBox4.Items.Add(j)
        Next

Comments

Popular posts from this blog

مقدمة الي تشفير الحروف الأبجدية العربية

VB.NET Translucent Control using GDI+

Add Custom Event to a Class in VB.NET