unfoldr

The unfoldr function is a `dual' to foldr: while foldr reduces a list to a summary value, unfoldr builds a list from a seed value. The function takes the element and returns Nothing if it is done producing the list or returns Just (a,b), in which case, a is a prepended to the list and b is used as the next element in a recursive call. For example,
iterate f == unfoldr (\x -> Just (x, f x))
In some cases, unfoldr can undo a foldr operation:
unfoldr f' (foldr f z xs) == xs
if the following holds:
f' (f x y) = Just (x,y)
f' z       = Nothing

Laziness

>>> take 1 (unfoldr (\x -> Just (x, undefined)) 'a')
"a"

Examples

>>> unfoldr (\b -> if b == 0 then Nothing else Just (b, b-1)) 10
[10,9,8,7,6,5,4,3,2,1]
>>> take 10 $ unfoldr (\(x, y) -> Just (x, (y, x + y))) (0, 1)
[0,1,1,2,3,5,8,13,21,54]
The unfoldr function is analogous to Data.List's unfoldr operation.
O(n), where n is the length of the result. The unfoldr function is analogous to the List 'unfoldr'. unfoldr builds a ByteString from a seed value. The function takes the element and returns Nothing if it is done producing the ByteString or returns Just (a,b), in which case, a is the next byte in the string, and b is the seed value for further production. Examples:
unfoldr (\x -> if x <= 5 then Just (x, x + 1) else Nothing) 0
== pack [0, 1, 2, 3, 4, 5]
O(n), where n is the length of the result. The unfoldr function is analogous to the List 'unfoldr'. unfoldr builds a ByteString from a seed value. The function takes the element and returns Nothing if it is done producing the ByteString or returns Just (a,b), in which case, a is the next character in the string, and b is the seed value for further production. Examples:
unfoldr (\x -> if x <= '9' then Just (x, succ x) else Nothing) '0' == "0123456789"
O(n) The unfoldr function is analogous to the List 'unfoldr'. unfoldr builds a ByteString from a seed value. The function takes the element and returns Nothing if it is done producing the ByteString or returns Just (a,b), in which case, a is a prepending to the ByteString and b is used as the next element in a recursive call.
O(n) The unfoldr function is analogous to the List 'unfoldr'. unfoldr builds a ByteString from a seed value. The function takes the element and returns Nothing if it is done producing the ByteString or returns Just (a,b), in which case, a is a prepending to the ByteString and b is used as the next element in a recursive call.
O(n), where n is the length of the result. The unfoldr function is analogous to the List 'unfoldr'. unfoldr builds a ShortByteString from a seed value. The function takes the element and returns Nothing if it is done producing the ShortByteString or returns Just (a,b), in which case, a is the next byte in the string, and b is the seed value for further production. This function is not efficient/safe. It will build a list of [Word8] and run the generator until it returns Nothing, otherwise recurse infinitely, then finally create a ShortByteString. If you know the maximum length, consider using unfoldrN. Examples:
unfoldr (\x -> if x <= 5 then Just (x, x + 1) else Nothing) 0
== pack [0, 1, 2, 3, 4, 5]
O(n), where n is the length of the result. The unfoldr function is analogous to the List unfoldr. unfoldr builds a Text from a seed value. The function takes the element and returns Nothing if it is done producing the Text, otherwise Just (a,b). In this case, a is the next Char in the string, and b is the seed value for further production. Performs replacement on invalid scalar values.
O(n), where n is the length of the result. The unfoldr function is analogous to the List unfoldr. unfoldr builds a stream from a seed value. The function takes the element and returns Nothing if it is done producing the stream or returns Just (a,b), in which case, a is the next Char in the string, and b is the seed value for further production. Properties
unstream . unfoldr f z = unfoldr f z
Builds a sequence from a seed value. Takes time linear in the number of generated elements. WARNING: If the number of generated elements is infinite, this method will not terminate.
The natural unfold into a Producer with a step function and a seed
unfoldr next = id
unfoldr f z is the DList constructed from the recursive application of f. The recursion starts with the seed value z and ends when, for some z' : b, f z' == Nothing. <math>(length (unfoldr f z)). unfoldr obeys the law:
toList (unfoldr f z) = unfoldr f z
unfoldr f z is the DNonEmpty constructed from the recursive application of f. The recursion starts with the seed value z and ends when, for some z' : b, f z' == Nothing. <math>(length (unfoldr f z)). unfoldr obeys the law:
toNonEmpty (unfoldr f z) = unfoldr f z
The unfoldr function is a `dual' to foldr: while foldr reduces a list to a summary value, unfoldr builds a list from a seed value. The function takes the element and returns Nothing if it is done producing the list or returns Just (a,b), in which case, a is a prepended to the list and b is used as the next element in a recursive call. For example,
iterate f == unfoldr (\x -> Just (x, f x))
In some cases, unfoldr can undo a foldr operation:
unfoldr f' (foldr f z xs) == xs
if the following holds:
f' (f x y) = Just (x,y)
f' z       = Nothing
A simple use of unfoldr:
>>> unfoldr (\b -> if b == 0 then Nothing else Just (b, b-1)) 10
[10,9,8,7,6,5,4,3,2,1]
Laziness:
>>> take 1 (unfoldr (\x -> Just (x, undefined)) 'a')
"a"
Build a Stream by unfolding steps starting from a seed. In particular note that S.unfoldr S.next = id. The seed can of course be anything, but this is one natural way to consume a pipes Producer. Consider:
>>> S.stdoutLn $ S.take 2 $ S.unfoldr Pipes.next Pipes.stdinLn
hello<Enter>
hello
goodbye<Enter>
goodbye
>>> S.stdoutLn $ S.unfoldr Pipes.next (Pipes.stdinLn >-> Pipes.take 2)
hello<Enter>
hello
goodbye<Enter>
goodbye
>>> S.effects $ S.unfoldr Pipes.next (Pipes.stdinLn >-> Pipes.take 2 >-> Pipes.stdoutLn)
hello<Enter>
hello
goodbye<Enter>
goodbye
Pipes.unfoldr S.next similarly unfolds a Pipes.Producer from a stream.
The unfoldr function is a `dual' to foldr: while foldr reduces a list to a summary value, unfoldr builds a list from a seed value. The function takes the element and returns Nothing if it is done producing the list or returns Just (a,b), in which case, a is a prepended to the list and b is used as the next element in a recursive call. For example,
iterate f == unfoldr (\x -> Just (x, f x))
In some cases, unfoldr can undo a foldr operation:
unfoldr f' (foldr f z xs) == xs
if the following holds:
f' (f x y) = Just (x,y)
f' z       = Nothing
A simple use of unfoldr:
>>> unfoldr (\b -> if b == 0 then Nothing else Just (b, b-1)) 10
[10,9,8,7,6,5,4,3,2,1]
O(n), where n is the length of the result. The unfoldr function is analogous to the List unfoldr. unfoldr builds a Text from a seed value. The function takes the element and returns Nothing if it is done producing the Text, otherwise Just (a,b). In this case, a is the next Char in the string, and b is the seed value for further production. Subject to fusion. Performs replacement on invalid scalar values.
O(n) Construct a vector by repeatedly applying the generator function to a seed. The generator function yields Just the next element and the new seed or Nothing if there are no more elements.
unfoldr (\n -> if n == 0 then Nothing else Just (n,n-1)) 10
= <10,9,8,7,6,5,4,3,2,1>
O(n) Construct a vector by repeatedly applying the generator function to a seed. The generator function yields Just the next element and the new seed or Nothing if there are no more elements.
unfoldr (\n -> if n == 0 then Nothing else Just (n,n-1)) 10
= <10,9,8,7,6,5,4,3,2,1>
O(n) Construct a vector by repeatedly applying the generator function to a seed. The generator function yields Just the next element and the new seed or Nothing if there are no more elements.
unfoldr (\n -> if n == 0 then Nothing else Just (n,n-1)) 10
= <10,9,8,7,6,5,4,3,2,1>
O(n) Construct a vector by repeatedly applying the generator function to a seed. The generator function yields Just the next element and the new seed or Nothing if there are no more elements.
unfoldr (\n -> if n == 0 then Nothing else Just (n,n-1)) 10
= <10,9,8,7,6,5,4,3,2,1>
Like unfoldM, but yields a final r when the Word8 generation is complete.
Given some pure process that produces characters, generate a stream of bytes. The r produced by the final Left will be the return value at the end of the stream. Note also that the Char values will be truncated to 8-bits.
O(n), where n is the length of the result. The unfoldr function is analogous to the List unfoldr. unfoldr builds a JSString from a seed value. The function takes the element and returns Nothing if it is done producing the JSString, otherwise Just (a,b). In this case, a is the next Char in the string, and b is the seed value for further production. Subject to fusion. Performs replacement on invalid scalar values.