-
Notifications
You must be signed in to change notification settings - Fork 23
Add Unfoldable1
#22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Unfoldable1
#22
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
module Data.Unfoldable1 | ||
( class Unfoldable1, unfoldr1 | ||
, replicate1 | ||
, replicate1A | ||
, singleton | ||
, range | ||
) where | ||
|
||
import Prelude | ||
|
||
import Data.Maybe (Maybe(..)) | ||
import Data.Semigroup.Traversable (class Traversable1, sequence1) | ||
import Data.Tuple (Tuple(..)) | ||
|
||
-- | This class identifies non-empty data structures which can be _unfolded_. | ||
-- | | ||
-- | The generating function `f` corresponds to the `uncons` operation of a | ||
-- | non-empty list or array; it always return a value, and then optionally | ||
-- | a value to continue unfolding from. | ||
class Unfoldable1 t where | ||
unfoldr1 :: forall a b. (b -> Tuple a (Maybe b)) -> b -> t a | ||
|
||
-- | Replicate a value `n` times. At least one value will be produced, so values | ||
-- | `n < 1` less than one will be ignored. | ||
-- | | ||
-- | ``` purescript | ||
-- | replicate1 0 "foo" == NEL.singleton "foo" :: NEL.NonEmptyList String | ||
-- | replicate1 2 "foo" == NEL.cons "foo" (NEL.singleton "foo") :: NEL.NonEmptyList String | ||
-- | ``` | ||
replicate1 :: forall f a. Unfoldable1 f => Int -> a -> f a | ||
replicate1 n v = unfoldr1 step (n - 1) | ||
where | ||
step :: Int -> Tuple a (Maybe Int) | ||
step i | ||
| i <= 0 = Tuple v Nothing | ||
| otherwise = Tuple v (Just (i - 1)) | ||
|
||
-- | Perform an `Applicative` action `n` times (at least once, so values `n < 1` | ||
-- | less than one will be ignored), and accumulate the results. | ||
replicate1A | ||
:: forall m f a | ||
. Apply m | ||
=> Unfoldable1 f | ||
=> Traversable1 f | ||
=> Int | ||
-> m a | ||
-> m (f a) | ||
replicate1A n m = sequence1 (replicate1 n m) | ||
|
||
-- | Contain a single value. For example: | ||
-- | | ||
-- | ``` purescript | ||
-- | singleton "foo" == NEL.singleton "foo" :: NEL.NonEmptyList String | ||
-- | ``` | ||
singleton :: forall f a. Unfoldable1 f => a -> f a | ||
singleton = replicate1 1 | ||
|
||
-- | Create an `Unfoldable1` containing a range of values, including both | ||
-- | endpoints. | ||
-- | | ||
-- | ``` purescript | ||
-- | range 0 0 "foo" == NEL.singleton 0 :: NEL.NonEmptyList Int | ||
-- | range 1 2 "foo" == NEL.cons 1 (NEL.singleton 2) :: NEL.NonEmptyList Int | ||
-- | range 2 0 "foo" == NEL.cons 2 (NEL.cons 1 (NEL.singleton 0)) :: NEL.NonEmptyList Int | ||
-- | ``` | ||
range :: forall f. Unfoldable1 f => Int -> Int -> f Int | ||
range start end = | ||
let delta = if end >= start then 1 else -1 in unfoldr1 (go delta) start | ||
where | ||
go delta i = | ||
let i' = i + delta | ||
in Tuple i (if i == end then Nothing else Just i') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if you want this to still say
Applicative
, though "Perform anApply
action" doesn't read well either.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went with saying exactly that in the end, it does read a little strangely because of the syntax at the English level, but it's not inaccurate 😄