Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Module Program
Sub Main()
Dim list = New List(Of Integer) From {1, 2, 3, 4, 5}
Dim info = list.FirstAndRest()
If info.First IsNot Nothing Then
Console.Write(info.First.GetValueOrDefault())
For Each other In info.Additional
Console.Write(", ")
Console.Write(other)
Next
Console.WriteLine()
End If
End Sub
<Runtime.CompilerServices.Extension>
Function FirstAndRest(Of T As Structure)(sequence As IEnumerable(Of T)) As (First As T?, Additional As IEnumerator(Of T))
Dim enumerator = sequence.GetEnumerator()
If enumerator.MoveNext() Then
Return (enumerator.Current, enumerator)
Else
Return (Nothing, enumerator)
End If
End Function
<Runtime.CompilerServices.Extension>
Function GetEnumerator(Of T)(enumerator As IEnumerator(Of T)) As IEnumerator(Of T)
Return enumerator
End Function
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.