Skip to content

Commit

Permalink
iterateN (#20)
Browse files Browse the repository at this point in the history
* iterateN

Create an Unfoldable by repeated application of a function to a seed value.

* Port iterateN to Unfoldable1 and re-export

* Update changelog

Co-authored-by: JordanMartinez <[email protected]>
Co-authored-by: Jordan Martinez <[email protected]>
  • Loading branch information
3 people authored Mar 25, 2022
1 parent 69ea03c commit 852e82e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Breaking changes:
- Migrate FFI to ES modules (#37 by @kl0tl and @JordanMartinez)

New features:
- Add `iterateN` function (#20 by @matthewleon and @JordanMartinez)

Bugfixes:

Expand Down
2 changes: 1 addition & 1 deletion src/Data/Unfoldable.purs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Prelude
import Data.Maybe (Maybe(..), isNothing, fromJust)
import Data.Traversable (class Traversable, sequence)
import Data.Tuple (Tuple(..), fst, snd)
import Data.Unfoldable1 (class Unfoldable1, unfoldr1, singleton, range, replicate1, replicate1A)
import Data.Unfoldable1 (class Unfoldable1, unfoldr1, singleton, range, iterateN, replicate1, replicate1A)
import Partial.Unsafe (unsafePartial)

-- | This class identifies (possibly empty) data structures which can be
Expand Down
18 changes: 18 additions & 0 deletions src/Data/Unfoldable1.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Data.Unfoldable1
, replicate1A
, singleton
, range
, iterateN
) where

import Prelude
Expand Down Expand Up @@ -111,3 +112,20 @@ range start end =
go delta i =
let i' = i + delta
in Tuple i (if i == end then Nothing else Just i')

-- | Create an `Unfoldable1` by repeated application of a function to a seed value.
-- | For example:
-- |
-- | ``` purescript
-- | (iterateN 5 (_ + 1) 0 :: Array Int) == [0, 1, 2, 3, 4]
-- | (iterateN 5 (_ + 1) 0 :: NonEmptyArray Int) == NonEmptyArray [0, 1, 2, 3, 4]
-- |
-- | (iterateN 0 (_ + 1) 0 :: Array Int) == [0]
-- | (iterateN 0 (_ + 1) 0 :: NonEmptyArray Int) == NonEmptyArray [0]
-- | ```
iterateN :: forall f a. Unfoldable1 f => Int -> (a -> a) -> a -> f a
iterateN n f s = unfoldr1 go $ Tuple s (n - 1)
where
go (Tuple x n') = Tuple x
if n' > 0 then Just $ Tuple (f x) $ n' - 1
else Nothing
3 changes: 3 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ main = do
assert $ U1.range 0 0 == NonEmpty 0 []
assert $ U1.range 0 2 == NonEmpty 0 [1, 2]

log "Test iterateN"
assert $ U.iterateN 5 (_ + 1) 0 == [0, 1, 2, 3, 4]

log "Test Maybe.toUnfoldable"
assert $ U.fromMaybe (Just "a") == ["a"]
assert $ U.fromMaybe (Nothing :: Maybe String) == []
Expand Down

0 comments on commit 852e82e

Please sign in to comment.