# unfold

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]
package
Just as there's a Foldable class, there should also be an Unfoldable class. This package provides one. Example unfolds are: * Random values * Enumeration of all values (depth-first or breadth-first) * Convert from a list Some examples can be found in the examples directory. Version 0.7.1
Build a forest from a list of seed values
Monadic forest builder, in depth-first order
Monadic forest builder, in breadth-first order, using an algorithm adapted from Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design, by Chris Okasaki, ICFP'00.
unfoldl f x is equivalent to reverse (unfoldr (fmap swap . f) x).
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), 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), 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), 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) 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), 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]
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.
O(n) Like unfoldr, unfoldrN builds a ByteString from a seed value. However, the length of the result is limited by the first argument to unfoldrN. This function is more efficient than unfoldr when the maximum length of the result is known. The following equation relates unfoldrN and unfoldr: > unfoldrN n f s == take n (unfoldr f s)
O(n) Like unfoldr, unfoldrN builds a Text from a seed value. However, the length of the result should be limited by the first argument to unfoldrN. This function is more efficient than unfoldr when the maximum length of the result is known and correct, otherwise its performance is similar to unfoldr. Subject to fusion. Performs replacement on invalid scalar values.
O(n) Like unfoldr, unfoldrN builds a ByteString from a seed value. However, the length of the result is limited by the first argument to unfoldrN. This function is more efficient than unfoldr when the maximum length of the result is known. The following equation relates unfoldrN and unfoldr: > fst (unfoldrN n f s) == take n (unfoldr f s)
O(n) Like unfoldr, unfoldrN builds a Text from a seed value. However, the length of the result should be limited by the first argument to unfoldrN. This function is more efficient than unfoldr when the maximum length of the result is known and correct, otherwise its performance is similar to unfoldr. Performs replacement on invalid scalar values.
Build a tree from a seed value
Monadic tree builder, in depth-first order
Monadic tree builder, in breadth-first order, using an algorithm adapted from Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design, by Chris Okasaki, ICFP'00.