文字列のようなリスト処理
yamasushi(2013/03/29 03:27:56 UTC)文字列のようなリスト処理をまとめます。
- string-splitのリスト版
(use srfi-1) ; predでリストを切断する。 ; 切断したリストのリスト、切断点を多値で返す ; (list-split '(2 2 2 1 1 2 3 4 5 6 5 6) odd? ) ; ((2 2 2) () (2) (4) (6) (6)) ; (1 1 3 5 5) ; (list-split '(1 1 2 3 4 5 6 5 6) odd? ) ; (() () (2) (4) (6) (6)) ; (1 1 3 5 5) (define (list-split l pred) ;#?=l (cond ( (null? l) (values '() '() ) ) ( (pair? l) (receive (head tail) (break pred l) ;#?=head ;#?=tail (if (null? tail) (values (list head) '() ) (receive (spl brk) (list-split (cdr tail) pred ) (values (cons head spl ) (cons (car tail) brk) ) ) ) ) ) ( else (if (pred l) (values '() l) (values l '())) ) ) )
これをつかって、標準入力からGaucheのChangeLogを読み、RSSを生成します。(Lifereaの変換フィルタ)(use srfi-1) ;list (use srfi-13) ;string (use sxml.serializer) (use gauche.generator) (define news-title "Gauche - Chenge Log") (define news-uri "http://practical-scheme.net/gauche/ChangeLog.txt") (define (kugiri-pred e) ;#?=e (and (string? e) (or ( #/^\d+-\d+-\d+\s+/ e) ( #/^\;/ e ) ) ) ) (define (parse s) (receive (spl brk) (list-split s kugiri-pred) (zip (map string-trim-both brk) (cdr spl) ) ) ) (define (generate-rss d) (srl:sxml->xml `(rdf (channel (title ,news-title ) (link ,news-uri ) ) ,@(map (^x ;#?=(car x) `(item (title ,(car x)) (description ,(srl:sxml->html `(pre ,@(map (cut format #f "~a\n" <>) (cadr x) ) ) ) ) ) ) d ) )) ) (define (main args) ($ print $ generate-rss $ parse $ generator->list $ port->line-generator (standard-input-port) ) 0 )