-
-
Notifications
You must be signed in to change notification settings - Fork 95
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
array: generalize S.head, S.last, S.tail, and S.init #610
array: generalize S.head, S.last, S.tail, and S.init #610
Conversation
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.
Good progress, @syves! I will wait for you to address the outstanding issues before I comment on your changes.
I've addressed the above changes, but for some reason doctest is not running.
|
index.js
Outdated
function(maybe, x) { | ||
return maybe.isNothing ? | ||
Just (Z.empty (foldable.constructor)) : | ||
Z.map (append (x), maybe); |
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.
Something doesn't feel right about using Z.map
in conjunction with the isNothing
check. If we're to keep the isNothing
check we could safely use maybe.value
in the Just case:
return maybe.isNothing ?
Just (Pair (Z.empty (foldable.constructor)) (x)) :
Just (Pair (Z.append (maybe.value.snd, maybe.value.fst)) (x));
This could be improved by removing the common code from the branches:
return Just (maybe.isNothing ?
Pair (Z.empty (foldable.constructor)) (x) :
Pair (Z.append (maybe.value.snd, maybe.value.fst)) (x));
return Just (Pair (maybe.isNothing ?
Z.empty (foldable.constructor) :
Z.append (maybe.value.snd, maybe.value.fst))
(x));
We could even remove the explicit conditional logic by using Z.reduce
:
return Just (Pair (Z.reduce (function(_, pair) { return Z.append (pair.snd, pair.fst); },
Z.empty (foldable.constructor),
maybe))
(x));
The problem with this approach is that Z.empty (foldable.constructor)
would be evaluated once for each element of the structure. Can you think of a solution to this problem?
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.
The comment above applies to init
rather than to tail
. I'm sorry for causing confusion.
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 stored the empty in a var.
Let's complete this pull request together. I will push some commits to your branch. :) |
6006db3
to
0844962
Compare
0844962
to
8588595
Compare
I just added fast paths for arrays, so these functions will continue to operate efficiently on arrays. @syves, if you're happy with these changes please squash the commits in preparation for merging. :) |
129ad7a
to
e6485da
Compare
Thank you, @syves! |
Closes #570
Initial attempt to generalize head, tail, last and init functions for Foldables.