Idris2/libs/contrib/Data/String/Iterator.idr

28 lines
569 B
Idris
Raw Normal View History

2020-09-19 15:22:54 +03:00
module Data.String.Iterator
2020-09-19 22:54:34 +03:00
%default total
2020-09-19 15:22:54 +03:00
2020-09-19 22:54:34 +03:00
export
data StringIterator : Type where [external]
2020-09-19 15:22:54 +03:00
2020-09-19 22:54:34 +03:00
%foreign
"scheme:blodwen-string-iterator-new"
2020-09-19 15:22:54 +03:00
export
fromString : String -> StringIterator
2020-09-19 22:54:34 +03:00
%foreign
"scheme:blodwen-string-iterator-next"
2020-09-19 15:22:54 +03:00
export
uncons : StringIterator -> Maybe (Char, StringIterator)
2020-09-19 22:54:34 +03:00
covering export
2020-09-19 15:22:54 +03:00
foldl : (a -> Char -> a) -> a -> String -> a
2020-09-19 22:54:34 +03:00
foldl f acc = loop acc . fromString
2020-09-19 15:22:54 +03:00
where
2020-09-19 22:57:54 +03:00
covering
2020-09-19 22:54:34 +03:00
loop : a -> StringIterator -> a
loop acc it =
case uncons it of
Nothing => acc
Just (ch, it') => loop (f acc ch) it'