Results 1 to 14 of 14

Thread: [VB 11] Iterators

  1. #1

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Lightbulb [VB 11] Iterators

    You may have heard of VB11, currently in the VS11 developer preview. It adds two main syntax elements - Iterator functions and Async methods. I don't know much about Async methods yet, but here's a quick Iterator tutorial.

    Basically, you put the Iterator keyword before the Function keyword and it becomes an iterator and returns an IEnumerable(Of t). Then, you have some kind of loop, and use the new Yield keyword to "return" a value. For example, this method generates lines from an array of points:

    Code:
        ''' <summary>
        ''' Represents a line.
        ''' </summary>
        Private Structure Line
            Public Start As Point
            Public [End] As Point
    
            Public Function IsVertical() As Boolean
                Return Me.Start.X = Me.End.X
            End Function
    
            Public Function IsHorizontal() As Boolean
                Return Me.Start.Y = Me.End.Y
            End Function
    
            Public Function IsEmpty() As Boolean
                Return Me.IsHorizontal() AndAlso Me.IsVertical()
            End Function
    
            Public Shared Operator =(ByVal a As Line, ByVal b As Line) As Boolean
                Return a.Start = b.Start AndAlso a.End = b.End
            End Operator
    
            Public Shared Operator <>(ByVal a As Line, ByVal b As Line) As Boolean
                Return a.Start <> b.Start OrElse a.End <> b.End
            End Operator
        End Structure
    
        ''' <summary>
        ''' Gets the lines between each point.
        ''' </summary>
        ''' <param name="points">The points for which to create lines.</param>
        Private Shared Iterator Function Lines(ByVal points As IEnumerable(Of Point)) As IEnumerable(Of Line)
            Dim last As Point?
    
            For Each p As Point In points
                If last IsNot Nothing Then Yield New Line With {
                    .Start = last.Value,
                    .End = p
                }
    
                last = p
            Next
        End Function
    Here's how you might use it:

    Code:
    ' Draw a square in a *very* roundabout way :)
    ' By the way, you can omit types in For Each statements in VB 11:
    '          v
    For Each line In Lines({
                Point.Empty,
                New Point(100, 0),
                New Point(100, 100),
                New Point(0, 100),
                Point.Empty
            })
        myGraphics.DrawLine(Pens.Red, line.Start, line.End)
    Next
    Use iterators well - they can be really great sometimes.

  2. #2

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: [VB 11] Iterators

    Bunch of resources here going into the details: http://msdn.microsoft.com/en-us/vstudio/async.aspx.

    Get the CTP off of the page to use the features now in Visual Studio 2010. Even though it's still in CTP they had such a demand for the new features that they've allowed it to be used in production since CTP Version 2.

  4. #4

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: [VB 11] Iterators

    1) Honestly, what does CTP stand for?
    2) Async isn't at all what I'd hoped for, it turns out. All it does is let you wait

  5. #5
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: [VB 11] Iterators

    I'm struggling to see a good use for them, if I had to recreate the example you did without iterators I would simply have the function return an array of line, and then loop through the array. Not much of an improvement. I would like to see them used in a place where conventional methods would be hard or tedious.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  6. #6
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: [VB 11] Iterators

    Quote Originally Posted by minitech View Post
    1) Honestly, what does CTP stand for?
    2) Async isn't at all what I'd hoped for, it turns out. All it does is let you wait
    Community Technology Preview.

    As for all you'd hoped for check out the LINQPad interactive tutorials from here: Interactive Tutorial on Async Functions.

    They're in C# but do an excellent job of explaining the uses / advantages of the Async CTP.

  7. #7
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [VB 11] Iterators

    Quote Originally Posted by minitech
    By the way, you can omit types in For Each statements in VB 11
    You've been able to omit types in For Each statements ever since Option Infer strolled into town. That was back with VS2008.

  8. #8
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: [VB 11] Iterators

    Quote Originally Posted by minitech View Post
    1) Honestly, what does CTP stand for?
    Community Technology Preview.

  9. #9

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: [VB 11] Iterators

    Quote Originally Posted by ForumAccount View Post
    You've been able to omit types in For Each statements ever since Option Infer strolled into town. That was back with VS2008.
    Yeah, but it only half worked, at least for me - when I hovered over the loop variable, it gave me "x As Object."

    @BlindSniper: That was just a simple example. The best part is the fact that it actually suspends the code there. It's an IEnumerable, not an array.

  10. #10
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [VB 11] Iterators

    Quote Originally Posted by minitech View Post
    Yeah, but it only half worked, at least for me - when I hovered over the loop variable, it gave me "x As Object."
    See picture from VS2008.
    Attached Images Attached Images  

  11. #11
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: [VB 11] Iterators

    Quote Originally Posted by minitech View Post
    @BlindSniper: That was just a simple example. The best part is the fact that it actually suspends the code there. It's an IEnumerable, not an array.
    I wasn't meaning any disrespect to your tutorial, because it is a good tutorial. But I fail to see the practical use for this function.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  12. #12

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Lightbulb Re: [VB 11] Iterators

    Quote Originally Posted by BlindSniper View Post
    I wasn't meaning any disrespect to your tutorial, because it is a good tutorial. But I fail to see the practical use for this function.
    None taken. All I meant was that mine is not the best example, because what Yield/Iterators might be more useful in is enumeration of things that don't have a fixed length. Normally, you'd need to create an instance of a custom IEnumerable type, which would delegate to an IEnumerator in the For Each. But with Iterators, you don't have to. So something to read the contents of a stream, byte-by-byte, might look like this:

    Code:
    Public Shared Iterator Function IterateStream(ByVal s As Stream) As IEnumerable(Of Byte)
        While s.Position < s.Length
            Yield CByte(s.ReadByte())
        End While
    End Function
    You could do some interesting things with sockets and asynchronous functions, probably.

  13. #13
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: [VB 11] Iterators

    Now I get it. You can for Each every byte in a file like It was loaded into memory, when in reality it only reads a byte each time when next is called. (The lightbulb has switched on)

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  14. #14
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: [VB 11] Iterators

    Quote Originally Posted by minitech View Post
    None taken. All I meant was that mine is not the best example, because what Yield/Iterators might be more useful in is enumeration of things that don't have a fixed length. Normally, you'd need to create an instance of a custom IEnumerable type, which would delegate to an IEnumerator in the For Each. But with Iterators, you don't have to. So something to read the contents of a stream, byte-by-byte, might look like this:

    Code:
    Public Shared Iterator Function IterateStream(ByVal s As Stream) As IEnumerable(Of Byte)
        While s.Position < s.Length
            Yield CByte(s.ReadByte())
        End While
    End Function
    You could do some interesting things with sockets and asynchronous functions, probably.
    Nice to see that VB.NET now has Yield

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width