Skip to content

Instantly share code, notes, and snippets.

@AnthonyDGreen
Created February 3, 2019 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnthonyDGreen/d7dbb7a5b98a940765c4adc33e3eaeee to your computer and use it in GitHub Desktop.
Save AnthonyDGreen/d7dbb7a5b98a940765c4adc33e3eaeee to your computer and use it in GitHub Desktop.
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