yamasushi(2013/04/20 23:26:23 UTC)要素が一つのものを扱う方法についてまとめてみます。
数学における単集合(たんしゅうごう、英: singleton; 単元集合、単項集合、一元集合)あるいは単位集合(unit set)は、唯一の元からなる集合である。一つ組 (1-tuple) や単項列 (a sequence with one element) と言うこともできる。
中身が1つだけのcollectionから中身を取り出す。
Collection cの中身が1つだけとわかっている場合、
(fold (lambda (x _) x) '() c)なんてやって値を取り出しているんですが、他にもっとスマートな方法は無いでしょうか。
Sequenceなら(ref s 0)でおしまいなんでしょうけど。 ちなみに、dbi/dbdの結果から、値を取り出したいのです。katsujiro
- (find (lambda (_) #t) c) とか。
- ねるWiki:ねる こんなのつくってます。
(define-method collection-car ((self <collection>)) (call-with-iterator self (lambda (end? next) (if (end?) #f (next)))))- とりあえず、dbiの方のインタフェースが変わって、一部result-setがsequenceになったので(ref s 0)で済むようになりました。findとか使えますね。効率考えたら、イテレータでということになりますでしょうか。ありがとうございます。katsujiro
- single?
1つの要素をもつリストかどうかを調べる。
(define (single? l) (and (pair? l) (null? (cdr l))))補記:(= (length l) 1)こうして調べるのはとても非効率的だ.第1要素を見終わった直後にはもう必要な情報は分かっているからだ.
; (a) ?
; TODO ジェネリック関数化?
(define (singleton? t)
(and (pair? t) ($ not $ list? $ car t) ($ null? $ cdr t) )
)
; (a) ---> a
; TODO ジェネリック関数化?
(define (peel-singleton t)
(if (singleton? t) (car t) t)
)
; リストのなかの(a)をaにする
(define (sexp-peel-singleton t)
(cond
(($ not $ pair? t) t)
((singleton? t) (car t))
(else
(map (^x (if (list? x) (sexp-peel-singleton x) x) ) t) ) ) )
Tag: gauche.mop.singleton