Created
February 3, 2019 17:02
Star
You must be signed in to star a gist
This file contains 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
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