El lunes, 31 de marzo de 2014 12:14:23 UTC-6, Fab HK escribió: > > Hi David, > not entirely sure it is pertinent, but iterators might be a nice idiomatic > example, as they pass around state explicitly. See here: > https://github.com/JuliaLang/Iterators.jl/blob/master/src/Iterators.jl >
Nice tip, thanks! > > > I found that a bit hard to understand initially, so I attach my "iterator > cheat sheet" below, half copied from the docs, half puzzled together from > reading the code. > > # for item = iterable > # # body > # end > > # is effectively transformed into > > # state = start(iterable) > # while !done(iterable, state) > # (item, state) = next(iterable, state) > # # body > # end > > # Thus, iterators (of type "it") need to implement (in "Haskell notation") > Haskell notation does not help me, unfortunately! Haskell is somewhere on my list of things I should learn at some point, but not likely to be reached any time soon, I'm afraid... > # start :: it -> state > # next :: it -> state -> (item, state) > # done :: it -> state -> Boolean > # where next(it,start(it))[1] is the first item to appear > # (in other words, the first item to appear is NOT what's in start, > # but what results after applying next to start once - which makes sense, > # because might be a different type) > >
