0 | module Libraries.Data.String.Iterator
2 | import public Data.List.Lazy
4 | import Control.Monad.Identity
20 | data StringIterator : String -> Type where [external]
25 | "scheme:blodwen-string-iterator-new"
26 | "RefC:stringIteratorNew"
27 | "javascript:stringIterator:new"
29 | fromString : (str : String) -> StringIterator str
34 | withString : (str : String) -> ((1 it : StringIterator str) -> a) -> a
35 | withString str f = f (fromString str)
40 | "scheme:blodwen-string-iterator-to-string"
41 | "RefC:stringIteratorToString"
42 | "javascript:stringIterator:toString"
44 | withIteratorString : (str : String)
45 | -> (1 it : StringIterator str)
46 | -> (f : (res : String) -> a)
56 | data UnconsResult : String -> Type where
57 | EOF : UnconsResult str
58 | Character : (c : Char) -> (1 it : StringIterator str) -> UnconsResult str
65 | "scheme:blodwen-string-iterator-next"
66 | "RefC:stringIteratorNext"
67 | "javascript:stringIterator:next"
69 | uncons : (str : String) -> (1 it : StringIterator str) -> UnconsResult str
72 | foldl : (accTy -> Char -> accTy) -> accTy -> String -> accTy
73 | foldl op acc str = withString str (loop acc)
75 | loop : accTy -> (1 it : StringIterator str) -> accTy
77 | case uncons str it of
79 | Character c it' => loop (acc `op` c) (assert_smaller it it')
82 | unpack : String -> LazyList Char
83 | unpack str = runIdentity $
withString str unpack'
87 | mapId : forall a, b. (a -> b) -> (1 x : Identity a) -> Identity b
88 | mapId f (Id x) = Id (f x)
90 | unpack' : (1 it : StringIterator str) -> Identity (Lazy (LazyList Char))
92 | case uncons str it of
94 | Character c it' => mapId (c ::) (unpack' $
assert_smaller it it')