-
Notifications
You must be signed in to change notification settings - Fork 55
Add -WithIndex classes #72
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
Conversation
This adds `FunctorWithIndex`, `FoldableWithIndex` and `TraversableWithIndex` and some combinators. Implementations are mostly copied from the versions without index with minor adaptions. Similarly for the documentation. I think the corresponding haskell package does not state all laws for these type classes, so there are TODO's in the doc strings of the three classes. `StateL` and `StateR` in TraversableWithIndex.purs are exact copies of the versions in Traversable.purs because they are not exported there. Maybe it would be better to merge TraversableWithIndex.purs into Traversable.purs and similarly for Foldable. The Foldable and Traverse instances of Array could then be along the lines of ```purescript traverse f = itraverse (const f)` ``` so that the foreign js imports for the unindexed version could be removed. `FunctorWithIndex`'s `imap` collides with the `imap` of purescript-invariant. Haskell calls the latter `invmap`. I don't know whether that's a problem. This does not yet include the `-1WithIndex` classes, i.e. generalizations of `Foldable1` and `Traversable1` in `Semigroup/`. I guess it would make sense to provide them here as well.
src/Data/FunctorWithIndex.purs
Outdated
-- | - `imap (const f) = map f` | ||
-- | though? | ||
-- | | ||
-- | TODO: `imap` collides with `Invariant`s `imap`. |
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.
Maybe we should either use WithIndex
suffixes on all of these functions, or prefix with ix
as an alternative to i
prefixes?
I wouldn't be too worried about overlapping with Invariant
's map either though, there's always qualification. And Invariant
is one of the less frequently used classes.
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'd prefer ix-
over -WithIndex
. Consistency with the haskell functions (so i-
) is also nice I guess. Your call.
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'd prefer mapWithIndex
since we use that convention elsewhere.
Thanks! I think we can remove the FFI requirement here by defining instances in terms of regular What do you think? |
For the |
Yeah, I agree that the duplication of the FFI implementations is not nice. I guess we can do two things:
I'd prefer the second version slightly, but let me know which one you'd find better. |
I've just done some benchmarks for the second approach. Looks like this would make EDIT: Added benchmarks for the other implementations as well.
So I guess we can definitely drop the foreign implementation of |
Also fixes the analogous bug for ifoldMapDefaultL
The modified `Foldable` and `Traversable` laws that must hold for their -WithIndex versions are missing in the docs. But because the actual unmodified laws for `Foldable` and `Traversable` themselves are not here either I guess it shouldn't be a problem.
Ok, so I've removed the foreign implementations where possible. This means that Regarding the documentation, I've documented some laws that enforce that I guess I'll add some tests now. Can we break up the single test file into one for each class? As far as I can tell, there is no interdependence. |
No tests for - ifoldM - itraverse_ - ifor_ - iall - iany - all instances except for Array These are not tested for Foldable either.
Ok so I've just gone ahead and added some tests. As far as I can tell, this should be OK to merge now unless we want to change the prefix of the indexed functions from |
src/Data/FunctorWithIndex.purs
Outdated
-- | map f = imap (const f) | ||
-- | ``` | ||
class Functor f <= FunctorWithIndex i f | f -> i where | ||
imap :: forall a b. (i -> a -> b) -> f a -> f b |
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.
This is called mapWithIndex
elsewhere actually.
@garyb Any more thoughts on this? Other than naming, I think this looks great. |
Are you suggesting renaming only |
I'm suggesting renaming them all. |
Ok, done. |
src/Data/Bifoldable.purs
Outdated
bifoldr :: forall a b c. (a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c | ||
bifoldl :: forall a b c. (c -> a -> c) -> (c -> b -> c) -> c -> p a b -> c | ||
bifoldMap :: forall m a b. Monoid m => (a -> m) -> (b -> m) -> p a b -> m | ||
bfoldrWithIndex :: forall a b c. (a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c |
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'm confused why these changed.
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.
Uh yeah, and rightly so.
Great work, thank you! I'll make a minor release for this since nothing seems to be breaking here. |
Add -WithIndex classes
This adds
FunctorWithIndex
,FoldableWithIndex
andTraversableWithIndex
and some combinators. Implementations are mostlycopied from the versions without index with minor adaptions. Similarly
for the documentation.
I think the corresponding haskell package does not state all laws for
these type classes, so there are TODO's in the doc strings of the three
classes.
StateL
andStateR
in TraversableWithIndex.purs are exact copies ofthe versions in Traversable.purs because they are not exported there.
Maybe it would be better to merge TraversableWithIndex.purs into
Traversable.purs and similarly for Foldable. The Foldable and Traverse
instances of Array could then be along the lines of
so that the foreign js imports for the unindexed version could be
removed.
FunctorWithIndex
'simap
collides with theimap
ofpurescript-invariant. Haskell calls the latter
invmap
. I don't knowwhether that's a problem.
This does not yet include the
-1WithIndex
classes, i.e.generalizations of
Foldable1
andTraversable1
inSemigroup/
. Iguess it would make sense to provide them here as well.
If this gets merged we should also implement instances in some other packages. purescript-lists and purescript-maps comes to mind. Anywhere else?