lib: make applyN more robust (#852)

This commit is contained in:
Simon Michael 2018-07-30 11:04:33 +01:00
parent d9d94e2cf6
commit eb6baac6e7

View File

@ -129,9 +129,12 @@ isLeft _ = False
isRight :: Either a b -> Bool
isRight = not . isLeft
-- | Apply a function the specified number of times. Possibly uses O(n) stack ?
-- | Apply a function the specified number of times,
-- which should be > 0 (otherwise does nothing).
-- Possibly uses O(n) stack ?
applyN :: Int -> (a -> a) -> a -> a
applyN n f = (!! n) . iterate f
applyN n f | n < 1 = id
| otherwise = (!! n) . iterate f
-- from protolude, compare
-- applyN :: Int -> (a -> a) -> a -> a
-- applyN n f = X.foldr (.) identity (X.replicate n f)
@ -218,4 +221,4 @@ mapM' f = sequence' . map f
tests_Hledger_Utils :: Test
tests_Hledger_Utils = TestList [
tests_Hledger_Utils_Text
]
]