From f0fd11791c363222d73b34133a87bcb356cd6c26 Mon Sep 17 00:00:00 2001 From: Stephen Diehl Date: Fri, 1 Jul 2016 11:25:10 -0400 Subject: [PATCH] Add unsnoc function. --- CHANGES.md | 1 + src/Protolude.hs | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 099d6444a..2330e0a6f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 ===== diff --git a/src/Protolude.hs b/src/Protolude.hs index cd79d98d0..429192b01 100644 --- a/src/Protolude.hs +++ b/src/Protolude.hs @@ -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)