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

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")
# 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)

Reply via email to