3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ズンドコキヨシ with Nim

Last updated at Posted at 2016-04-02

ズンドコキヨシのNimバージョン

Nim版が無かったので書いてみました。
(EDIT: quit を利用して一行短くしました。4/03, 2016)

元ネタ:https://twitter.com/kumiromilk/status/707437861881180160

# How to compile: nim c -r FILENAME.nim
import math

type ZunDoko = enum
  doko, zun

iterator zundokoGenerator(): ZunDoko =
  yield case math.random(2).ZunDoko:
          of zun:  echo "ズン"; zun
          of doko: echo "ドコ"; doko

var song: seq[int] = @[]
while true:
  for z_or_d in zundokoGenerator():
    song.add(z_or_d.int)
    if z_or_d == doko:
      defer: song = @[]
      if song.sum >= 4:
        echo "キ ヨ シ!"
        quit 0

出力

ズン 
ドコ 
ズン 
ズン 
ズン 
ズン 
ドコ 
キ ヨ シ!

追記: threadsを利用した実装

# How to compile: nim c -r --threads:on FILENAME.nim
import math, threadpool

type ZunDoko = enum
  doko, zun

iterator zundokoGenerator(id: int): ZunDoko =
  yield case math.random(2).ZunDoko:
          of zun:  echo "ズン" & $id; zun
          of doko: echo "ドコ" & $id; doko

var song {.threadvar.}: seq[int]

proc kiyoshi(id: int = 0) {.thread.} =
  block done:
    song = @[]
    while true:
      for z_or_d in zundokoGenerator(id):
        song.add(z_or_d.int)
        if z_or_d == doko:
          defer: song = @[]
          if song.sum >= 4:
            echo "キ ヨ シ!" & $id
            break done
          
when isMainModule:
  let max_threads = 10
  for i in 1..max_threads:
    spawn kiyoshi(i)
  sync()
3
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?