Skip to content

Instantly share code, notes, and snippets.

@shirok
Last active August 29, 2015 14:25
Show Gist options
  • Save shirok/acf7c10f4b617a323a27 to your computer and use it in GitHub Desktop.
Save shirok/acf7c10f4b617a323a27 to your computer and use it in GitHub Desktop.
;; -*- coding:utf-8 -*-
(use util.match)
(use data.random)
(set! (random-data-seed) (sys-time))
(define *parts* (map (^s (cons s (string-length s))) '("進捗" "どう" "です" "か")))
(define index-gen (integers$ (length *parts*)))
(define *stream* (generator->lseq (^[] (~ *parts* (index-gen)))))
(let loop ([s *stream*] [cnt 0])
(match s
[() (print "???") (print cnt "文字で煽られました。")]
[(("進捗" . _) ("どう" . _) ("です" . _) ("か" . _) x . _) (loop (take s 4) cnt)]
[((x . a) . r) (display x) (loop r (+ cnt a))]))
@shirok
Copy link
Author

shirok commented Jul 19, 2015

ちょっと直した。終端文字列にマッチ成功の時にストリームをぶった切れば、特別扱いする必要が減る。

@shirok
Copy link
Author

shirok commented Jul 19, 2015

一定のコレクションからランダムに選んでくるジェネレータがあるのも便利だなと思ったので足した。HEADを使うと*stream*はこう書ける。

(define *stream*
  (generator->lseq
   (samples$ (map (^s (cons s (string-length s))) '("進捗" "どう" "です" "か")))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment