filter p . map f
foldr :: (a -> b -> b) -> b -> [a] -> b
build :: (forall b. (a -> b -> b) -> b -> b) -> [a]
forall g k z.
foldr k z (build g) = g k z
destroy :: (forall b. (b -> Maybe (a, b)) -> b -> c)
-> [a] -> c
unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
forall g f e.
destroy g (unfoldr f e) = g f e
loop :: (s -> a -> (s, Maybe a))
-> s -> Array a -> (Array a, s)
forall f g s t.
loop f s . fst . loop g t
= loop (fuse f g) (s,t)
concatMap or map on UTF8We want something more flexible
Consider unfoldr again:
unfoldr :: (s -> Maybe (a, s)) -> s -> [a]
Take (b -> Maybe (a,b)) and b as the representation
of an abstract sequence:
data Stream a = forall s. Stream (s -> Step s a) s
data Step s a = Done
| Yield a s
| Skip s
readList :: [a] -> Stream a
writeList :: Stream a -> [a]
readArray :: Array a -> Stream a
writeArray :: Stream a -> Array a
data Stream a =
forall s. Stream (s -> Step s a) s Int
mapS :: (a -> b) -> Stream a -> Stream b
mapS f (Stream next s n) = Stream next' s n
where next' s = case next s of
Done -> Done
Yield x s' -> Yield (f x) s'
Skip s' -> Skip s'
Which then can be lifted to work on arrays:
map :: (a -> b) -> Array a -> Array b
map f = write . mapS f . read
read . write = id
For example:
filter p . map f
= write . filterS p . read . write . mapS f . read
= write . filterS p . mapS f . read
foldl' k z = foldlS' k z . read
replicate x n = write . replicateS x n
For example:
foldl' k z . filter p . map f
= foldlS' k z . read . write . filterS p
. read . write . mapS f . read
= foldlS' k z . filterS p . mapS f . read
main = print
. foldl' hash 5381
. map toLower
. filter isAlpha
=<< readFile f
where hash h c = h * 33 + ord c
import Data.ByteString.Lazy.Char8 as B
main = print
. B.foldl' hash 5381
. B.map toLower
. B.filter isAlpha
=<< B.readFile f
where hash h c = h * 33 + ord c
#include <stdio.h>
void main () {
long h = 5381;
int c;
while ((c = getchar()) != EOF)
if (isalpha(c))
h = h * 33 + tolower(c);
printf("%li\n", h);
}
#include <stdio.h>
void main() {
long h = 5381;
char buf[4096];
int i, count;
while (count = fread(&buf, 1, 4096, stdin))
for (i = 0; i < count; i++)
if (isalpha(buf[i]))
h = h * 33 + tolower (buf[i]);
printf("%li\n", h);
}
readUp :: Array a -> Stream a
writeUp :: Stream a -> Array a
readDn :: Array a -> Stream a
writeDn :: Stream a -> Array a
scanr f z = writeDn . scanrS f z . readDn
foldr f z = foldS f z . readUp
foldl f z = foldS (flip f) z . readDn
foldl' f z = foldS' (flip f) z . readUp
foldr' f z = foldS' f z . readDn
map, filter, sum
producerUp f = writeUp f
consumerUp f = f . readUp
transformerUp f = writeUp . f . readUp
consumerUp f (producerUp g) = f g
consumerUp f . transformerUp g = consumerUp (f . g)
transformerUp f (producerUp g) = producerUp (f g)
transformerUp f . transformerUp g
= transformerUp (f . g)
producerBi :: Stream a
-> Array a
consumerBi :: (Stream a -> b)
-> (Array a -> b)
transformerBi :: (Stream a -> Stream b)
-> (Array a -> Array b)
producerBi f = reverse . producerBi f
consumerBi f = consumerBi f . reverse
transformerBi f = reverse . transformerBi f
. reverse
consumerDn f . transformerBi g
= consumerDn (f . g)
foldl to foldl'
foldl['] f z . map g
(:) and (++)