2010/03/31

『プログラミング Clojure』の「第4章 シーケンスと使ったデータの統合」を Common Lisp の SERIES でやってみる

まだ途中まで。

;;;; 『プログラミング Clojure』の「第4章 シーケンスと使ったデータの統合」を
;;;; Common Lisp の SERIES でやってみる。
(eval-when (:compile-toplevel :load-toplevel :execute)
(require :series)
(use-package :series))

(series::install)

;; (range 10)
(scan-range :below 10)
;;=> #Z(0 1 2 3 4 5 6 7 8 9)

;; (range 10 20)
(scan-range :from 10 :below 20)
;;=> #Z(10 11 12 13 14 15 16 17 18 19)

;; (range 1 5 2)
(scan-range :from 1 :below 25 :by 2)
;;=> #Z(1 3 5 7 9 11 13 15 17 19 21 23)

;; (repeat 5 1)
(subseries (series 1) 0 5)
;;=> #Z(1 1 1 1 1)

;; (repeat 10 "x")
(subseries (series "x") 0 10)
;;=> #Z("x" "x" "x" "x" "x" "x" "x" "x" "x" "x")

;; (take 10 (iterate inc 1))
(subseries (scan-fn t #'* #'1+) 0 10)
;;=> #Z(1 2 3 4 5 6 7 8 9 10)
(scan-range :from 1 :upto 10)
;;=> #Z(1 2 3 4 5 6 7 8 9 10)

;; (defn whole-numbers [] (iterate inc 1))
(defun whole-numbers ()
(scan-range :from 1))

;; (take 20 (repeat 1))
(subseries (series 1) 0 20)
;;=> #Z(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)

;; (take 10 (cycle (range 3)))
(subseries (series 0 1 2) 0 10)
;;=> #Z(0 1 2 0 1 2 0 1 2 0)
(subseries (apply #'series (collect (scan-range :below 3))) 0 10)
;;=> #Z(0 1 2 0 1 2 0 1 2 0)

;; (interleave (whole-numbers) ["A" "B" "C" "D" "E"])
(collect-append (map-fn t #'list (whole-numbers) #z("A" "B" "C" "D" "E")))
;;=> (1 "A" 2 "B" 3 "C" 4 "D" 5 "E")
;; シリーズを返したいところ。

;; (interpose "," ["apples" "bananas" "grapes"])
;; パス。loop で。
(loop for (x xs) on '("apples" "bananas" "grapes")
collect x if xs collect ",")
;;=> ("apples" "," "bananas" "," "grapes")

0 件のコメント: