|
Module Module1
Sub Main()
Dim Naturals = 1.Generate(Function(n) n + 1)
Dim Squares = From n In Naturals Select n ^ 2
Dim Fibonacci = (New With {.a = 1, .b = 1}).Generate( _
Successor:=Function(p) New With {.a = p.b, .b = p.a + p.b}, _
Selector:=Function(p) p.a)
Dim TriangleNumbers = (New With {.Sum = 1, .i = 1}).Generate( _
Function(p) New With {.Sum = p.Sum + p.i, .i = p.i + 1}, _
Function(p) p.Sum)
Console.WriteLine("Natürliche Zahlen: {0}", Naturals.Show(8))
Console.WriteLine("Quadratzahlen: {0}", Squares.Show(8))
Console.WriteLine("Fibonaccizahlen: {0}", Fibonacci.Show(8))
Console.WriteLine("Dreieckszahlen: {0}", TriangleNumbers.Show(8))
Console.ReadKey()
End Sub
End Module
Option Strict On
Imports System.Runtime.CompilerServices
<DebuggerStepThrough()> _
Public Module Sequences
<Extension()> _
Public Function Generate(Of T)(ByVal Init As T, _
ByVal Successor As Func(Of T, T)) As IEnumerable(Of T)
Return New GeneratorEnumerable(Of T)(Init, Function(x) Successor(x))
End Function
<Extension()> _
Public Function Generate(Of TIn, TOut)(ByVal Init As TIn, _
ByVal Successor As Func(Of TIn, TIn), _
ByVal Selector As Func(Of TIn, TOut)) As IEnumerable(Of TOut)
Return (New GeneratorEnumerable(Of TIn)(Init, _
Function(x) Successor(x))).Select(Selector)
End Function
<Extension()> _
Public Function Show(Of T)(ByVal Sequence As IEnumerable(Of T), _
Optional ByVal Length As Integer = 4, _
Optional ByVal Delimiter As String = ", ") As String
Return "[" & String.Join(Delimiter, Sequence.Take(Length).Select( _
Function(x) x.ToString()).ToArray) & Delimiter & "...]"
End Function
<Extension()> _
Public Sub ForEach(Of T)(ByVal Sequence As IEnumerable(Of T), _
ByVal [Function] As Action(Of T))
For Each It In Sequence
Call [Function](It)
Next
End Sub
<DebuggerStepThrough()> _
Private Class GeneratorEnumerable(Of T) : Implements IEnumerable(Of T)
Public Delegate Function Generator(ByVal Arg As T) As T
<DebuggerStepThrough()> _
Private Class Enumerator : Implements IEnumerator(Of T)
Private m_Out As T
Private m_Current As T
Private ReadOnly m_Init As T
Private ReadOnly m_Generator As Generator
Public Sub New(ByVal Init As T, ByVal Generator As Generator)
m_Init = Init
m_Current = Init
m_Generator = Generator
End Sub
Public ReadOnly Property Current() As T _
Implements System.Collections.Generic.IEnumerator(Of T).Current
Get
Return m_Out
End Get
End Property
Public ReadOnly Property Current1() As Object _
Implements System.Collections.IEnumerator.Current
Get
Return Current
End Get
End Property
Public Function MoveNext() As Boolean _
Implements System.Collections.IEnumerator.MoveNext
m_Out = m_Current
m_Current = m_Generator(m_Current)
Return True
End Function
Public Sub Reset() Implements System.Collections.IEnumerator.Reset
m_Current = m_Init
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
GC.SuppressFinalize(Me)
End Sub
End Class
Private ReadOnly m_Init As T
Private ReadOnly m_Generator As Generator
Public Sub New(ByVal Init As T, ByVal Generator As Generator)
m_Init = Init
m_Generator = Generator
End Sub
Public Function GetEnumerator() _
As System.Collections.Generic.IEnumerator(Of T) _
Implements _
System.Collections.Generic.IEnumerable(Of T).GetEnumerator
Return New Enumerator(m_Init, m_Generator)
End Function
Public Function GetEnumerator1() _
As System.Collections.IEnumerator _
Implements System.Collections.IEnumerable.GetEnumerator
Return GetEnumerator()
End Function
End Class
End Module |