[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
gauche.sequence
- シーケンスフレームワーク シーケンスに関するジェネリックな操作を提供するモジュールです。 シーケンスとは、コレクションのうち要素の順序が規定されているものです。 コレクションの操作全てに加え、シーケンスに対しては、 各要素に整数のインデックスを関連づけること、 それから要素の順序が影響する操作を適用することができます。
このモジュールはgauche.collection
を継承しています
(gauche.collection
- コレクションフレームワーク参照)。
コレクションに使えるジェネリックな操作は全てシーケンスに対しても適用可能です。
Gauche組み込みクラスのうち、リスト、ベクター、そして文字列は
シーケンスであり、このモジュールでメソッドが定義されます。
またgauche.uvector
のユニフォームベクタ等、
いくつかの拡張データタイプはシーケンスとなっています。
9.19.1 基本的なシーケンスのアクセサ | ||
9.19.2 シーケンスのスライス | ||
9.19.3 シーケンス上のマップ | ||
9.19.4 その他のシーケンス上の操作 | ||
9.19.5 シーケンスを実装する |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
シーケンスseqのindex番目の要素を返します。 このメソッドによって、全てのシーケンスが統一的にアクセスできます。
indexが負値だったりシーケンスのサイズ以上だった場合は、 fallbackが与えられていればそれが返され、 そうでなければエラーとなります。
(ref '(a b c) 1) ⇒ b (ref '#(a b c) 1) ⇒ b (ref "abc" 1) ⇒ #\b |
統一的なシーケンスの変更メソッドです。 シーケンスseqのindex番目の要素にvalueをセットします。
註: シーケンスによってはインデックスによる変更をサポートしていない
場合があります。例えば「ソートされた整数」を表すシーケンスがあった場合、
i
番目の要素を適当な整数で置き換えることはできないでしょう。
そのようなシーケンスでも、要素の挿入や削除など、別の方法でシーケンスを
変更する手段が与えられるかもしれません。
(let ((x (list 'a 'b 'c))) (set! (ref x 1) 'z) x) ⇒ (a z c) (let ((x (vector 'a 'b 'c))) (set! (ref x 1) 'z) x) ⇒ #(a z c) (let ((x (string #\a #\b #\c))) (set! (ref x 1) #\z) x) ⇒ "azc" |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
シーケンスseqの、start番目の要素からend番目の要素の直前 までの部分シーケンスを返します。endが省略された場合はシーケンスの 最後までが取られます。返されるシーケンスの型はseqと同じになります。
(subseq '(a b c d e) 1 4) ⇒ (b c d) (subseq '#(a b c d e) 1 4) ⇒ #(b c d) (subseq "abcde" 1 4) ⇒ "bcd" (subseq '(a b c d e) 3) ⇒ (d e) |
value-seqの各要素を、シーケンスseqのstart番目から end番目の直前まで順にセットします。 value-seqはどんなシーケンスでも構いませんが、 (end - start) よりは長くなくてはなりません。
2番目の形式では、endがvalue-seqの長さから算出されます。
(define s (vector 'a 'b 'c 'd 'e)) (set! (subseq s 1 4) '(4 5 6)) s ⇒ #(a 4 5 6 e) (set! (subseq s 0) "ab") s ⇒ #(#\a #\b 5 6 e) |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
シーケンスはまたコレクションでもあるので、シーケンスについて
fold
、map
、for-each
や他のジェネリック関数を
拡張することができます。しかし、時にはイテレーション中に要素そのものと
そのインデックスを知りたいことでしょう。そのためのジェネリック関数が
いくつかあります。
ジェネリックなfold
と似ていますが、konsにはseqの
インデックス内から、第1引数としてseqの要素と増加する値が渡る
点が異なります。
(fold-with-index acons '() '(a b c)) ⇒ ((2 . c) (1 . b) (0 . a)) |
map
、map-to
、for-each
と似ていますが、procが
第1引数としてインデックスを受け取る点が違います。
(map-with-index list '(a b c d) '(e f g h)) ⇒ ((0 a e) (1 b f) (2 c g) (3 d h)) (map-to-with-index <vector> cons '(a b c d)) ⇒ #((0 . a) (1 . b) (2 . c) (3 . d)) |
find
のように、seqの中でpredを満足する最初の要素を
探しますが、2つの値、要素のインデックスと要素自身を返します。
predを満足する要素がなかったら、2つの#f
が返ります。
(find-with-index char-upper-case? "abraCadabra") ⇒ 4 and #\C (find-with-index char-numeric? "abraCadabra") ⇒ #f and #f |
find
に似ていますが、seqの中でpredを満足する最初の、
要素自身ではなくインデックスを返す点が異なります。
seqの中にpredを満足する要素がなかったら、#f
が返ります。
(find-index char-upper-case? "abraCadabra") ⇒ 4 (find-index char-numeric? "abraCadabra") ⇒ #f |
SRFI-1 (リストの探索参照)のlist-index
も見て下さい。
リストに対するfold-right
の総称関数版です。
fold
と同じように、このメソッドは種となる値 (初期値はknil)
を受渡しながら、高階関数konsを与えられたシーケンスの各要素に
適用してゆきます。fold
とfold-right
の違いは
要素間の結合の順序にあります。
ひとつだけのシーケンス[E0, E1, ..., En]
に適用する場合、
fold
とfold-right
はそれぞれ以下のように動作します。
fold: (kons En (kons En-1 (kons ... (kons E1 (kons E1 knil)) ...))) fold-right (kons E0 (kons E1 (kons ... (kons En-1 (kons En knil)) ...))) |
このメソッドは<collection>
に対しては提供されていません。
コレクションは要素の順序を規定しないからです。
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
コレクションに対する選択と探索はシーケンスにも使えます。 コレクションからの選択と探索を参照して下さい。
シーケンスseqの連続する要素で、同じキー値を持つもの同士を
グループ化します。
キーの値は要素に手続きkeyを適用することで得られます。keyの
デフォルト値はidentity
です。sedqの各要素に対して、
keyは正確に一回だけ呼ばれます。
キーの等価性判定には手続きtestが使われます。デフォルト値はeqv?
です。
(group-sequence '(1 1 1 2 3 4 4 2 2 3 1 1 3)) ⇒ ((1 1 1) (2) (3) (4 4) (2 2) (3) (1 1) (3)) (group-sequence '(1 1 1 2 3 4 4 2 2 3 1 1 3) :key (cut modulo <> 2))) ⇒ ((1 1 1) (2) (3) (4 4 2 2) (3 1 1 3)) (group-sequence '#("a" "a" "b" "b" "c" "d" "d") :test string=?) ⇒ (("a" "a") ("b" "b") ("c") ("d" "d")) (group-sequence "aabbcdd" :test char=?) ⇒ ((#\a #\a) (#\b #\b) (#\c) (#\d #\d)) |
このメソッドはHaskellのgroup
と似ています。
隣り合っていない要素もグループ化したい場合は、
group-collection
(コレクションからの選択と探索参照)を使って下さい。
Returns a newly created sequence of the same type as src, in which the elements are permuted from src according to permuter.
Permuter is a sequence of exact integers. When the k-th element
of permuter is i, the k-th element of the result
is (ref src i)
. Therefore, the size of the result
sequence is the same as the size of permuter. Permuter
can be any kind of sequence, unrelated to the type of src.
It is allowed that the same index i can appear more than once in permuter.
(permute '(a b c d) '(3 2 0 1)) ⇒ (d c a b) (permute '(a b c d) '(0 2)) ⇒ (a c) (permute '(a b c d) '(0 0 1 1 2 2)) ⇒ (a a b b c c) |
If an integer in permuter is out of the valid range as the index
of src, then an error is signalled unless fallback is given.
If fallback is given, what value is used depends on the result of
(ref src i fallback)
—which usually returns
fallback for the out-of-range index i.
(permute '#(a b c) '(3 2 1 0) 'foo) ⇒ #(foo c b a) (permute "!,HWdelor" #(2 5 6 6 7 1 -1 3 7 8 6 4 0) #\space) ⇒ "Hello, World!" |
Like permute
, but the result will be an instance of the given class
instead of the class of src.
(permute-to <string> '(#\a #\b #\c #\d #\r) '(0 1 4 0 2 0 3 0 1 4 0)) ⇒ "abracadabra" |
Also like permute
, but the result is stored back to src.
Src must be a mutable sequence, and the length of src and
permuter must be the same.
Returns a new sequence of the same type and size as src, in which elements are randomly permuted.
(shuffle '(a b c d e)) ⇒ (e b d c a) (shuffle "abcde") ⇒ "bacde" |
This generic function uses srfi-27
(See section srfi-27
- ランダムビットのソース).
By default it uses default-random-source
, but you can pass
an alternative random source by the optional argument.
Like shuffle
, except that the result will be an instance of
class instead of the class of src.
Like shuffle
, but the result is stored back to src.
Src must be a mutable sequence.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] |
This document was generated by Shiro Kawai on November, 22 2009 using texi2html 1.78.