ズンドコキヨシ

In [1]:
function zundokochannel()
    function _producer(channel::Channel{String})
        i = 1
        while i > 0
            zd, i = rand([("ズン", i + 1), ("ドコ", +(i<5))])
            put!(channel, zd)
        end
        put!(channel, "キ・ヨ・シ!")
        close(channel)
    end
    channel = Channel{String}(32)
    @schedule _producer(channel)
    channel
end
Out[1]:
zundokochannel (generic function with 1 method)
In [2]:
for cw in zundokochannel()
    print(cw)
end
ズンドコズンズンドコドコドコドコドコドコドコドコドコドコズンズンドコドコズンズンズンズンズンドコキ・ヨ・シ!
In [3]:
collect(zundokochannel())
Out[3]:
82-element Array{String,1}:
 "ドコ"    
 "ズン"    
 "ズン"    
 "ドコ"    
 "ドコ"    
 "ズン"    
 "ドコ"    
 "ズン"    
 "ズン"    
 "ドコ"    
 "ズン"    
 "ズン"    
 "ドコ"    
 ⋮       
 "ドコ"    
 "ドコ"    
 "ドコ"    
 "ズン"    
 "ドコ"    
 "ズン"    
 "ズン"    
 "ズン"    
 "ズン"    
 "ズン"    
 "ドコ"    
 "キ・ヨ・シ!"

ズンドコキヨシ(厳密版)

In [4]:
function zundokostrictchannel()
    function _producer(channel::Channel{String})
        i = 1
        while i > 0
            zd, i = rand([("ズン", i + 1), ("ドコ", +(i!=5))])
            put!(channel, zd)
        end
        put!(channel, "キ・ヨ・シ!")
        close(channel)
    end
    channel = Channel{String}(32)
    @schedule _producer(channel)
    channel
end
Out[4]:
zundokostrictchannel (generic function with 1 method)
In [5]:
for cw in zundokostrictchannel()
    print(cw)
end
ズンドコズンドコドコドコズンドコドコズンズンドコドコドコドコドコズンドコドコドコズンズンドコズンズンドコズンズンズンズンドコキ・ヨ・シ!
In [6]:
collect(zundokostrictchannel())
Out[6]:
51-element Array{String,1}:
 "ズン"    
 "ドコ"    
 "ズン"    
 "ズン"    
 "ドコ"    
 "ドコ"    
 "ドコ"    
 "ズン"    
 "ズン"    
 "ズン"    
 "ズン"    
 "ズン"    
 "ドコ"    
 ⋮       
 "ドコ"    
 "ドコ"    
 "ドコ"    
 "ドコ"    
 "ズン"    
 "ドコ"    
 "ズン"    
 "ズン"    
 "ズン"    
 "ズン"    
 "ドコ"    
 "キ・ヨ・シ!"

ズンドコキヨシ(for v0.6.x)

In [7]:
VERSION
Out[7]:
v"0.6.1"
In [8]:
function zundokochannel_v06()
    Channel(ctype=String, csize=32) do channel::Channel{String}
        i = 1
        while i > 0
            zd, i = rand([("ズン", i + 1), ("ドコ", +(i<5))])
            put!(channel, zd)
        end
        put!(channel, "キ・ヨ・シ!")
    end
end
Out[8]:
zundokochannel_v06 (generic function with 1 method)
In [9]:
for cw in zundokochannel_v06()
    print(cw)
end
ズンズンドコドコドコドコズンズンドコドコズンドコドコズンズンズンズンドコキ・ヨ・シ!
In [10]:
collect(zundokochannel_v06())
Out[10]:
27-element Array{String,1}:
 "ドコ"    
 "ドコ"    
 "ドコ"    
 "ズン"    
 "ズン"    
 "ドコ"    
 "ズン"    
 "ドコ"    
 "ドコ"    
 "ズン"    
 "ドコ"    
 "ズン"    
 "ズン"    
 ⋮       
 "ズン"    
 "ドコ"    
 "ドコ"    
 "ドコ"    
 "ドコ"    
 "ズン"    
 "ズン"    
 "ズン"    
 "ズン"    
 "ズン"    
 "ドコ"    
 "キ・ヨ・シ!"

参考:ズンドコキヨシ(Task Iteration)

In [11]:
function zundokotask()
    function _producer()
        i = 1
        while i > 0
            zd, i = rand([("ズン", i + 1), ("ドコ", +(i<5))])
            produce(zd)
        end
        produce("キ・ヨ・シ!")
    end
    @task _producer()
end
Out[11]:
zundokotask (generic function with 1 method)
In [12]:
for cw in zundokotask()
    print(cw)
end
WARNING: Task iteration is now deprecated. Use Channels for inter-task communication.  A for-loop on a Channel object is terminated by calling `close` on the object.
Stacktrace:
 [1] depwarn(::String, ::Symbol) at ./deprecated.jl:70
 [2] start at ./deprecated.jl:986 [inlined]
 [3] anonymous at ./<missing>:?
 [4] include_string(::String, ::String) at ./loading.jl:522
 [5] include_string(::Module, ::String, ::String) at /path/to/userhome/.julia/v0.6/Compat/src/Compat.jl:174
 [6] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /path/to/userhome/.julia/v0.6/IJulia/src/execute_request.jl:154
 [7] eventloop(::ZMQ.Socket) at /path/to/userhome/.julia/v0.6/IJulia/src/eventloop.jl:8
 [8] (::IJulia.##14#17)() at ./task.jl:335
while loading In[12], in expression starting on line 1
WARNING: consume is now deprecated. Use Channels for inter-task communication.
Stacktrace:
 [1] depwarn(::String, ::Symbol) at ./deprecated.jl:70
 [2] consume(::Task) at ./deprecated.jl:955
 [3] done at ./deprecated.jl:992 [inlined]
 [4] anonymous at ./<missing>:?
 [5] include_string(::String, ::String) at ./loading.jl:522
 [6] include_string(::Module, ::String, ::String) at /path/to/userhome/.julia/v0.6/Compat/src/Compat.jl:174
 [7] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /path/to/userhome/.julia/v0.6/IJulia/src/execute_request.jl:154
 [8] eventloop(::ZMQ.Socket) at /path/to/userhome/.julia/v0.6/IJulia/src/eventloop.jl:8
 [9] (::IJulia.##14#17)() at ./task.jl:335
while loading In[12], in expression starting on line 1
ドコズンドコドコドコドコズンズンドコドコドコドコドコズンズンズンドコズンズンズンズンズンズンズンズンズンズンドコキ・ヨ・シ!
WARNING: produce is now deprecated. Use Channels for inter-task communication.
Stacktrace:
 [1] depwarn(::String, ::Symbol) at ./deprecated.jl:70
 [2] produce(::String) at ./deprecated.jl:909
 [3] (::#_producer#10)() at ./In[11]:6
 [4] (::##9#11{#_producer#10})() at ./task.jl:80
while loading In[12], in expression starting on line 1
WARNING: produce is now deprecated. Use Channels for inter-task communication.
Stacktrace:
 [1] depwarn(::String, ::Symbol) at ./deprecated.jl:70
 [2] produce(::String) at ./deprecated.jl:909
 [3] (::#_producer#10)() at ./In[11]:8
 [4] (::##9#11{#_producer#10})() at ./task.jl:80
while loading In[12], in expression starting on line 1
In [13]:
collect(zundokotask())
WARNING: Task iteration is now deprecated. Use Channels for inter-task communication.  A for-loop on a Channel object is terminated by calling `close` on the object.
Stacktrace:
 [1] depwarn(::String, ::Symbol) at ./deprecated.jl:70
 [2] start at ./deprecated.jl:986 [inlined]
 [3] grow_to!(::Array{Any,1}, ::Task) at ./array.jl:525
 [4] collect(::Task) at ./array.jl:431
 [5] include_string(::String, ::String) at ./loading.jl:522
 [6] include_string(::Module, ::String, ::String) at /path/to/userhome/.julia/v0.6/Compat/src/Compat.jl:174
 [7] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /path/to/userhome/.julia/v0.6/IJulia/src/execute_request.jl:154
 [8] eventloop(::ZMQ.Socket) at /path/to/userhome/.julia/v0.6/IJulia/src/eventloop.jl:8
 [9] (::IJulia.##14#17)() at ./task.jl:335
while loading In[13], in expression starting on line 1
WARNING: consume is now deprecated. Use Channels for inter-task communication.
Stacktrace:
 [1] depwarn(::String, ::Symbol) at ./deprecated.jl:70
 [2] consume(::Task) at ./deprecated.jl:955
 [3] done at ./deprecated.jl:992 [inlined]
 [4] grow_to!(::Array{Union{},1}, ::Task, ::Void) at ./array.jl:531
 [5] grow_to!(::Array{Any,1}, ::Task) at ./array.jl:525
 [6] collect(::Task) at ./array.jl:431
 [7] include_string(::String, ::String) at ./loading.jl:522
 [8] include_string(::Module, ::String, ::String) at /path/to/userhome/.julia/v0.6/Compat/src/Compat.jl:174
 [9] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /path/to/userhome/.julia/v0.6/IJulia/src/execute_request.jl:154
 [10] eventloop(::ZMQ.Socket) at /path/to/userhome/.julia/v0.6/IJulia/src/eventloop.jl:8
 [11] (::IJulia.##14#17)() at ./task.jl:335
while loading In[13], in expression starting on line 1
WARNING: produce is now deprecated. Use Channels for inter-task communication.
Stacktrace:
 [1] depwarn(::String, ::Symbol) at ./deprecated.jl:70
 [2] produce(::String) at ./deprecated.jl:909
 [3] (::#_producer#10)() at ./In[11]:6
 [4] (::##9#11{#_producer#10})() at ./task.jl:80
while loading In[13], in expression starting on line 1
WARNING: produce is now deprecated. Use Channels for inter-task communication.
Stacktrace:
 [1] depwarn(::String, ::Symbol) at ./deprecated.jl:70
 [2] produce(::String) at ./deprecated.jl:909
 [3] (::#_producer#10)() at ./In[11]:8
 [4] (::##9#11{#_producer#10})() at ./task.jl:80
while loading In[13], in expression starting on line 1
Out[13]:
19-element Array{String,1}:
 "ドコ"    
 "ドコ"    
 "ズン"    
 "ドコ"    
 "ドコ"    
 "ドコ"    
 "ドコ"    
 "ドコ"    
 "ドコ"    
 "ズン"    
 "ズン"    
 "ドコ"    
 "ドコ"    
 "ズン"    
 "ズン"    
 "ズン"    
 "ズン"    
 "ドコ"    
 "キ・ヨ・シ!"
In [ ]: