Add unsnoc function.

This commit is contained in:
Stephen Diehl
2016-07-01 11:25:10 -04:00
parent f6586ba8b4
commit f0fd11791c
2 changed files with 9 additions and 0 deletions
+1
View File
@@ -11,6 +11,7 @@
* Hides all Show class functions. Only the Class itself is exported. Forbids
* custom instances that are not GHC derived.
* Export`` encodeUtf8`` and ``decodeUtf8`` functions by default.
* Adds ``unsnoc`` function.
0.1.5
=====
+8
View File
@@ -12,6 +12,7 @@ module Protolude (
map,
(&),
uncons,
unsnoc,
applyN,
print,
show,
@@ -362,6 +363,13 @@ uncons :: [a] -> Maybe (a, [a])
uncons [] = Nothing
uncons (x:xs) = Just (x, xs)
unsnoc :: [x] -> Maybe ([x],x)
unsnoc = foldr go Nothing
where
go x mxs = Just (case mxs of
Nothing -> ([], x)
Just (xs, e) -> (x:xs, e))
applyN :: Int -> (a -> a) -> a -> a
applyN n f = X.foldr (.) identity (X.replicate n f)