Creating Generators


#1

I’ve been playing around with Julia lately and have been enjoying it. I have come from python and as a learning exercise was looking at re-writing some code I had written to do least significant bit embedding.

I’m wondering if my re-write below is the best way to write a more complicated generator or if there are better or more efficient ways to implement one.

Apologies if I’ve just missed something obvious.

Original python code below:

def bit_generator(s):
    for x in s:
        a = ord(x)

        i = 0
        while i < 7:
            yield a & 1
            a = a >> 1  # bit shifting embeds character backwards
            i += 1

    # signify end with 14 zeros (double ascii null)
    for x in xrange(14):
           yield 0

Julia equivalent (without the nulls at the end). This is where I missed yield as I ended up creating two generators to achieve the same - I could have easily just missed something here, but this is what I came up with.

type OrdGenerator
    s::String
end

Base.start(x::OrdGenerator) = 1
Base.done(x::OrdGenerator, state) = length(x.s) == state - 1
Base.next(x::OrdGenerator, state) = Int8(x.s[state]), state + 1


type BitGenerator
    b::Int8
end

Base.start(x::BitGenerator) = 1
Base.done(x::BitGenerator, state) = state > 7

function Base.next(x::BitGenerator, state)
    a = x.b & 1
    x.b = x.b >> 1
    a, state + 1
end


s = "word? w"
println("$s : length = $(length(s))")
for (i, x) in enumerate(OrdGenerator(s))
    println("$i: $x | $(bin(x))")
    for (j, bit) in enumerate(BitGenerator(x))
        println("  $j: $bit")
    end
end

Any comments or advice would be appreciated.


From generator (with Channel) to iterator
#2

The best way to build a “complicated generator” is to make a type which implements the iterator interface. That’s explained very well in the manual:

https://docs.julialang.org/en/stable/manual/interfaces/#iteration


#3

Thanks Chris.

I think my attempt above does use the iterator interface - happy that I’ve used the best method to create one.

After seeing your response and thinking about this a bit more, my main issue was probably more around the fact that I had to create two generators to achieve what I wanted as there was a nested while under the for.

I ended up working out that you can create a type for the state variable that has an accumulator, which works (see code below), but I’m still wondering if this approach is reasonable or what other people do?

Also, by “more complicated generator”, I really meant anything that couldn’t be easily achieved in something like a list comprehension.

type Generator
    s::String
end

type State
    i::Int64
    index_bit::Int8
    acc::Int8
end

Base.start(x::Generator) = State(1, 0, 0)
Base.done(x::Generator, state::State) = (length(x.s), 7) == (state.i, state.index_bit)

function Base.next(x::Generator, state::State)
    if state.index_bit == 0
        state = State(state.i, state.index_bit + 1, Int8(x.s[state.i]))
    elseif state.index_bit == 7
        state = State(state.i + 1, 1, Int8(x.s[state.i + 1]))
    else
        state = State(state.i, state.index_bit + 1, state.acc >> 1)
    end
    println("$(state.acc & 1) | $state")
    state.acc & 1, state
end
    
s = "here"
for (i, x) in enumerate(Generator(s)) end

#4

Have you seen Channels? They are the direct Julia equivalent of Python generators.


#5

@fengyang.wang Thanks for the response.

I had looked at Tasks but as they were used in parallel computing, I thought they might be overkill for what I was doing.

I tried to create a generator using Channels (running Julia v0.5.2) this morning with no luck. I had success with the Tasks equivalent:

bit_generator(s::String) = Task() do
    for x in s
        a = Int8(x[1])
        i = 0
        while i < 7
            produce(a & 1)
            a = a >> 1
            i += 1
        end
    end
    
    for x in 1:14
        produce(0)
    end
end

for bit in bit_generator("text") 
    @printf("%d", bit)
end

My Channels attempt was essentially replacing as per below:

Tasks() do -> Channels() do c
produce(y) -> push!(c, y)

but I get the following error.

MethodError: Cannot `convert` an object of type ##45#46{String} to an object of type Channel{T}
This may have arisen from a call to the constructor Channel{T}(...),
since type constructors fall back to convert methods.

 in anonymous at ./<missing>:?

What am I missing?


#6

Task is the right way to do this on 0.5; it is only on 0.6 that the concepts of Channel and Task have been merged.


#7

@fengyang.wang Thanks. I suspected that might be the case.


#8

Hello,

I also looking for a way to define a generator using Channel

Generator1() = Channel() do c
    i = 1
    while(true)
        push!(c, i)
        i = i + 1
    end
end

function Generator2()
    c = Channel{Int}(1)
    i = 1
    while(true)
        push!(c, i)
        i = i + 1
    end
end

# println(Generator1())

for i in Generator1()
    @show i
    sleep(1)
end

My issue is that I don’t like Generator1 to be a Channel of Any I’d prefer it to return a Channel of Int.

I tried to implement such an idea in Generator2 but nothing is displayed on console.

Any idea?


#9
Generator1() = Channel(ctype=Int) do c
           i = 1
           while(true)
               push!(c, i)
               i = i + 1
           end
       end

The above code is calling this constructor Channel(func::Function; ctype=Any, csize=0, taskref=nothing). The first argument comes from the do syntax and the rest of the arguments can be used normally.


#10

Did you end up resolving this error?


#11

In Julia 1.0, are Channels still the preferred way of implementing Python-like generators in Julia?


#12

Depends on your performance requirements. For the highest performance, create a type that implements the iteration protocol. If your performance demands aren’t that stringent then a channel is a fine way to go as well.


#13

If you’re willing to import additional packages, ResumableFunctions provides a nice way to create efficient generators.


#14

Suppose you have this in python

# aka riemann serie
def p_serie(p):
    assert(p>0)
    s = 0
    n = 1
    while True:
        s += 1.0/pow(n,p)
        yield s
        n += 1

then run

itr = p_serie(2)
[next(itr) for _ in range(10)]

that output (reformatted)

[ 1.0,
  1.25,
  1.3611111111111112,
  1.4236111111111112,
  1.4636111111111112,
  1.4913888888888889,
  1.511797052154195,
  1.527422052154195,
  1.5397677311665408,
  1.5497677311665408
]

(1/2)


PyGen - python style generators
#15

In julia, you can write

struct PSerie
    p
end

function Base.iterate(ps::PSerie, itr=(0,1,0))
    s,n,y = itr
    if y == 1; @goto y1 end

    while true
        s += 1.0/(n^ps.p)
        return s,(s,n,1)
        @label y1
        n += 1
    end
end

p_serie(n) = (x for x in PSerie(n))

then run

using Base.Iterators: take

[take(p_serie(2),10)...]

that will output:

10-element Array{Float64,1}:
 1.0
 1.25
 1.3611111111111112
 1.4236111111111112
 1.4636111111111112
 1.4913888888888889
 1.511797052154195
 1.527422052154195
 1.5397677311665408
 1.5497677311665408


You have translated a yield using a goto with the help of an iterator.
I like to to think that sometimes goto can lead to clear code.


#16

Thanks for the example. What I am interested in: Is there anyone here
who doesn’t think the Python code is easier and clearer?

ffevotte> ‘If you’re willing to import additional packages’.

Honestly, no. This is such an elementary and frequent task that
I think Base should provide it. Do I remember correctly that this
worked in version 0.6?


#17

I don’t think this is a good example of that. It’s true that your solution closely resembles the original yielding python code, but for Julia developers used to Julia style iterators (and who don’t have the Python code to compare to), I think that style of iterator may be confusing. Compare your suggestion:

function Base.iterate(ps::PSerie, itr=(0,1,0))
    s,n,y = itr
    if y == 1; @goto y1 end

    while true
        s += 1.0/(n^ps.p)
        return s,(s,n,1)
        @label y1
        n += 1
    end
end

To the equivalent Julia style iterator:

function Base.iterate(ps::PSerie, (s,n) = (0,1))
    s += 1 / (n^ps.p)
    s, (s, n+1)
end

#18

I’m not sure I agree anymore. I used to use generators all the time when choosing in Python, and I definitely missed this functionality when I first started working on Julia. But I can honestly say in the past year of doing basically everything in Julia, it just hasn’t been an issue.


#19

Make it
|> work
|> readable
|> idiomatic

:slight_smile:
borrowed to a great friend coder

Secondly, it’s an example. so it was kept simple deliberately.

I like a lot generators too but for any functions that will have two yields or more you will got an irreducible control flow graph that you won’t be able to recode with a generator. Your solution will won’t work for two yields or more. The goto one will.

For a single case yield, use generator. They’re great


#20

IMO, the goto solution is not more readable than the two line iterator I posted. My initial reaction is “why is there a while loop in an iterator, there must be something wrong here”. Of course, it depends on what you’re used to. Someone used to yielding iterators might find it easier to read.

Could you give an example of such a function? Perhaps there’s an alternative way to do it.