Concurrent Clean : 便利関数(2)

[id:lethevert:20060219:p4]に、さらに便利関数を追加です。

reel :: ![(.u -> .u)] !.u -> .u
reel [] u = u
reel [x:xs] u = reel xs (x u)

reelWhile :: ![(.u -> (Bool, .u))] .u -> .u
reelWhile [] u = u
reelWhile [x:xs] u
    # (b, u) = x u
    | b = reelWhile xs u
        = u

最後のは、[id:lethevert:20060202:p1]で登場した関数ですね。あと、ここには書きませんが、($)などの優先順位を1にして、(>>=)との優先順位を調整しました。(関数名は、もうちょっと検討中です。)
追記)昔、矢型演算子として定義した(-->)の優先順位を0にすると、[id:lethevert:20060202:p1]で登場した(>>=)と同じ関数になるので、それを一意型の時に使うことにしました。あと、reelは「巻き取る」という意味のつもり。
そして、関数から関数へ値を受け渡すには、タプルを使います。
次の例は、"Hello World!"を出力しながら、その文字数をカウントして、最後にカウントした文字数を出力します。文字数のカウントは、タプルを使って受け渡されます。

count_write :: String (Int, *File) -> (Int, *File)
count_write text (sum, f)
    # f = fwrites text f
    = (sum + size text, f)

Start w
    # (f, w) = stdio w
    # (sum, f) = (0, f) --> reel $ map count_write ["Hello"," ","World","!"]
    # f = f --> fwrites $ "\ncount = " +++ toString sum
    = fclose f w