-
Notifications
You must be signed in to change notification settings - Fork 51
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
indexed foldable-traversable instances #134
indexed foldable-traversable instances #134
Conversation
Thanks! Maybe we should deprecate |
Not sure how deprecation works in these libs, but I've done the rest. |
Thanks! I think a comment is enough for now. |
The alternative is to use a |
13602b8
to
b85bd27
Compare
Looks like I've made a test fail now. Will look into this. |
That test passes on my machine. Any idea what's going on there? |
It appears there is actually a bug:
|
src/Data/List/Lazy/Types.purs
Outdated
mapWithIndex f xs = List (go 0 <$> unwrap xs) | ||
where | ||
go _ Nil = Nil | ||
go i (Cons x xs') = Cons (f i x) (f (i + 1) <$> xs') |
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.
Shouldn't there be a recursive call to go
here?
Maybe it's better to try to write this as a foldr
?
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.
Ah, indeed. I think I was running my tests locally on the wrong branch by accident.
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.
Actually I think this is a different mistake I've made, besides the one breaking the test above. I'll go through and add some more thorough testing wherever it makes sense.
in turn fixes List.mapWithIndex
reuse left fold for reversals
Okay, this should be good to go now. (That is to say, ready for further review.) |
@@ -120,6 +125,22 @@ instance foldableList :: Foldable List where | |||
|
|||
foldMap f = foldl (\b a -> b <> f a) mempty | |||
|
|||
instance foldableWithIndexList :: FoldableWithIndex Int List where | |||
foldrWithIndex f b xs = |
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 seems like we could extract a default implementation in terms of foldlWithIndex
. What do you think?
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'll try.
I don't think we can build a polymorphic default My reasoning: We know But this depends on a polymorphic There's one here for reverseT :: (Traversable t) => t a -> t a
reverseT t = snd (mapAccumR (\ (x:xs) _ -> (xs, x)) (toList t) t) But this, in turn, depends on a Now, we do have a default implementation of Please let me know if you think of something I've missed here. I would really love to get an efficient, stack-safe default implementation into |
Looks good, and I think you're right about the default implementations. Could you please add a test for each of the new methods though? |
Good idea. I will do that.
…On Tue, Nov 28, 2017 at 8:36 PM, Phil Freeman ***@***.***> wrote:
Looks good, and I think you're right about the default implementations.
Could you please add a test for each of the new methods though?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#134 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AARBS8iWQbv183ksrQGZTU6A7YWgj_dkks5s7LUxgaJpZM4QlPjH>
.
|
tests done |
src/Data/List/Types.purs
Outdated
@@ -91,6 +110,11 @@ instance traversableList :: Traversable List where | |||
traverse f = map (foldl (flip (:)) Nil) <<< foldl (\acc -> lift2 (flip (:)) acc <<< f) (pure Nil) | |||
sequence = traverse id | |||
|
|||
instance traversableWithIndexList :: TraversableWithIndex Int List where | |||
traverseWithIndex f = | |||
map (foldl (flip (:)) Nil) |
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.
Isn't this reverse
?
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.
Yes, but we can't use it because of the reverse dependency.
assert $ traverseWithIndex (const Just) xs == Just xs | ||
|
||
log "traverseWithIndex should be correct" | ||
assert $ traverseWithIndex (\i a -> Just $ i + a) (fromFoldable [2, 2, 2]) |
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.
Nitpicking, sorry: could you use an Applicative which tests that the order of effects is correct, like Array
?
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.
Hmm, actually that wouldn't work, never mind.
Thanks! |
* indexed foldable-traversable instances * update foldable-traversable dep * TraversableWithIndex implementation * Data.List: reuse mapWithIndex from Data.FunctorWithIndex * fix List.foldrWithIndex in turn fixes List.mapWithIndex * simplify List right fold code reuse left fold for reversals * add failing mapWithIndex test for Lazy List * fix Lazy List foldrWithIndex * indexed instance tests * clarify reversal in traversableWithIndexList
No description provided.