I need help to write an iterator in a class

6 views
Skip to first unread message

Jean-Philippe Humbert

unread,
Apr 15, 2015, 5:52:27 AM4/15/15
to moonscri...@googlegroups.com
Hi,

I am a newbie and I hope that my question is not too stupid.
This code does not work as expected (it prints nothing).
I expect to print the table (key and value).

class example_iterator
  new: =>
    @mytable = {
      name: "Bill"
      age: 200
    }
  iterator: =>
    return ->
      {k,v} for k,v in pairs @mytable
        
myexample = example_iterator!
for k,v in myexample\iterator!
  print k,v

What am I doing wrong? 

Thanks
Jp

Harley Laue

unread,
Apr 15, 2015, 7:16:36 PM4/15/15
to moonscri...@googlegroups.com
Maybe you want to try this:
iterator: => pairs @mytable
--
You received this message because you are subscribed to the Google Groups "MoonScript Users" group.
To post to this group, send email to moonscri...@googlegroups.com.
Visit this group at http://groups.google.com/group/moonscript-users.

Jean-Philippe Humbert

unread,
Apr 17, 2015, 4:18:16 AM4/17/15
to moonscri...@googlegroups.com
Yes it works and now I feel really stupid :-)
thank you

Reply all
Reply to author
Forward
0 new messages