[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

11.53 util.stream - ストリームライブラリ

Module: util.stream

このモジュールは遅延ストリームのライブラリを提供します。このモジュール には SRFI-40 で定義されている関数および構文が含まれています。

Function: stream? obj

[SRFI-40] objutil.streamの手続きによって作成されたストリームであ る場合にかぎり#tを返します。

Variable: stream-null

[SRFI-40] NULLストリームのシングルトンインスタンス。

Macro: stream-cons object stream

[SRFI-40] ストリームの基本構成子。objectstreamの先頭に追加し、新し いストリームを返します。

Function: stream-null? obj

[SRFI-40] objがNULLストリームの場合にのみ#tを返します。

Function: stream-pair? obj

[SRFI-40] objがNULLストリームではないストリームのときにのみ#tを返します。

Function: stream-car s

[SRFI-40] ストリームsの最初の要素を返します。

Function: stream-cdr s

[SRFI-40] ストリームsの最初の要素をのぞいたのこりの要素をストリームとして 返します。

Macro: stream-delay expr

[SRFI-40] exprの遅延形式であるストリームを返します。

原則として、ストリームを生成する関数はすべからく結果を stream-delayでラップすべきです。

Function: stream obj …

[SRFI-40] 要素がobj …であるような新しいストリームを返します。

Function: stream-unfoldn generator seed n

[SRFI-40] 互いに関連する n 本のストリームを生成します。それぞれの内容は generatorおよびseedを使って生成します。

generatorは現在のシード値とともに呼ばれ、n+1個の値 を返します。

 
(generator seed)
  => seed result_0 result_1 … result_n-1

最初の値は次のシード値になります。Result_kは以下の形式のどれかで なければなりません。

(val)

valk-番目のストリームの次のcar部になります。

#f

k-番目のストリームの新しい情報はありません。

()

k-番目のストリームの最後に到達しました。

以下の例では2つのストリームが作られます。最初のものは奇数の無限ストリー ムで、2つめのものは偶数の無限ストリームです。

 
gosh> (define-values (s0 s1)
        (stream-unfoldn (lambda (i)
                          (values (+ i 2)          ;; next seed
                                  (list i)         ;; for the first stream
                                  (list (+ i 1)))) ;; for the second stream
                        0 2))
#<undef>
gosh> (stream->list (stream-take s0 10))
(0 2 4 6 8 10 12 14 16 18)
gosh> (stream->list (stream-take s1 10))
(1 3 5 7 9 11 13 15 17 19)
Function: stream-map func . streams

[SRFI-40] streamsの各要素にfuncを適用した値を要素とする新しいストリー ムを返します。

Function: stream-for-each func . streams

[SRFI-40] funcstreamsの各要素に適用します。 streamsが終端にたっしたところで停止します。

Function: stream-filter pred? stream

[SRFI-40] pred?をパスする要素のみからなる新しいストリームを返す。

以下の手続きは Alejandro Forero Cuervo が Chicken Schemeのために書いた ライブラリから取ってきたものです。これらの手続きの名前は srfi-1 (srfi-1 - List library)の命名規則にならったものです。

Function: stream-xcons a b

(stream-cons b a)のこと。利便性のためだけにある。

Function: stream-cons* elt … stream

streamの前にelt …を連結した新しいストリームを生成し ます。

Function: make-stream n &optional init

n個のinitを要素とする新しいストリームを生成します。 initが省略された場合#fが使われます。nを負の値にする と無限ストリームが生成されます。

Function: stream-tabulate n init-proc

n個の要素をもつ新しいストリームを生成します。k-番目の要素 は init-prock に適用して得られます。nを負の値にする と無限ストリームが生成されます。

Function: stream-iota count &optional start step

startからはじまり、stepずつ要素が増加する整数のストリーム を生成します。ストリームの長さはcountが正ならその値になります。 countが負なら無限ストリームになります。startのデフォルト値 は 0、stepのデフォルト値は 1 です。

Function: stream-format fmt arg …

string->stream(format fmt arg …)に適用した結果の ストリームを返します。

Function: stream->list stream
Function: stream->string stream

Converts a stream to a list or a string. All of stream's elements are forced; if stream is infinite, these procedures won't terminate. For stream->string, all stream must be characters, or an error is signalled.

Function: list->stream list

Converts a list to a stream of its elements.

Function: string->stream string &optional stream

Convers a string to a stream of characters. If an optional stream is given, it becomes the tail of the resulting stream.

(stream->list (string->stream "abc" (list->stream '(1 2 3)))) ⇒ (#\a #\b #\c 1 2 3)

Function: port->stream &optional iport reader closer

Creates a stream, whose elements consist of the items read from the input port iport. The default iport is the current input port. The default reader is read-char.

The result stream terminates at the point where reader returns EOF (EOF itself is not included in the stream). If closer is given, it is called with iport as an argument just after reader reads EOF.

Function: iterator->stream iter

A generic procedure to turn an internal iterator iter into a stream of iterated results.

The iter argument is a procedure that takes two arguments, next and end, where next is a procedure that takes one argument and end is a thunk. Iter is supposed to iterate over some set and call next for each argument, then call end to indicate the end of the iteration. Here's a contrived example:

 
(stream->list
 (iterator->stream 
  (lambda (next end) (for-each next '(1 2 3 4 5)) (end))))
 ⇒ (1 2 3 4 5)

Internally iterator->stream uses the “inversion of iterator” technique, so that iter only iterates to the element that are needed by the stream. Thus iter can iterate over an infinite set. In the following example, iter is an infinite loop calling next with increasing integers, but only the first 10 elements are calculated because of stream-take:

 
(stream->list 
 (stream-take
  (iterator->stream
   (lambda (next end)
     (let loop ((n 0)) (next n) (loop (+ n 1))))) 
  10))
 ⇒ (0 1 2 3 4 5 6 7 8 9)
Function: stream-lines stream

Splits stream where its element equals to #\n, and returns a stream of splitted streams.

 
(stream->list 
 (stream-map stream->string 
             (stream-lines (string->stream "abc\ndef\nghi"))))
 ⇒ ("abc" "def" "ghi")
Function: stream= elt= stream …

Returns true iff each corresponding element of stream … are the same in terms of elt=. This procedure won't terminate if any of streams is infinite.

Function: stream-prefix= stream prefix &optional elt=

Compares initial elements of stream against a list prefix by elt=. Only as many elements of stream as prefix has are checked.

Function: stream-caar s
Function: stream-cadr s

Function: stream-cdddar s
Function: stream-cddddr s

(stream-caar s) = (stream-car (stream-car s)) etc.

Function: stream-ref stream pos

Returns the pos-th element in the stream. Pos must be a nonnegative exact integer.

Function: stream-first s
Function: stream-second s
Function: stream-third s
Function: stream-fourth s
Function: stream-fifth s
Function: stream-sixth s
Function: stream-seventh s
Function: stream-eighth s
Function: stream-ninth s
Function: stream-tenth s

(stream-first s) = (stream-ref s 0) etc.

Function: stream-take stream count
Function: stream-take-safe stream count

Returns a new stream that consists of the first count elements of the given stream. If the given stream has less than count elements, the stream returned by stream-take would raise an error when the elements beyond the original stream is accessed. On the other hand, the stream returned by stream-take-safe will return a shortened stream when the given steram has less than count elements.

 
(stream->list (stream-take (stream-iota -1) 10))
 ⇒ (0 1 2 3 4 5 6 7 8 9)

(stream-take (stream 1 2) 5)
 ⇒ stream

(stream->list (stream-take (stream 1 2) 5))
 ⇒ error

(stream->list (stream-take-safe (stream 1 2) 5))
 ⇒ (1 2)
Function: stream-drop stream count
Function: stream-drop-safe stream count

Returns a new stream that consists of the elements in the given stream except the first count elements. If the given stream has less than count elements, stream-drop returns a stream that raises an error if its element is accessed, and stream-drop-safe returns an empty stream.

Function: stream-intersperse stream element

Returns a new stream in which element is inserted between elements of stream.

Function: stream-split stream pred
Function: stream-last stream
Function: stream-last-n stream count
Function: stream-butlast stream
Function: stream-butlast-n stream count
Function: stream-length stream
Function: stream-length>= stream n
Function: stream-append stream …
Function: stream-concatenate streams
Function: stream-reverse stream &optional tail-stream
Function: stream-count pred stream …
Function: stream-remove pred stream
Function: stream-partition pred stream
Function: stream-find pred stream
Function: stream-find-tail pred stream
Function: stream-take-while pred stream
Function: stream-drop-while pred stream
Function: stream-span pred stream
Function: stream-break pred stream
Function: stream-any pred stream …
Function: stream-every pred stream …
Function: stream-index pred stream …
Function: stream-member obj stream &optional elt=
Function: stream-memq obj stream
Function: stream-memv obj stream
Function: stream-delete obj stream &optional elt=
Function: stream-delete-duplicates stream &optional elt=
Function: stream-grep re stream
Function: write-stream stream &optional oport writer

[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by Shiro Kawai on November, 22 2009 using texi2html 1.78.