Skip to content
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.take and S.drop #627

Merged
merged 1 commit into from
Apr 26, 2019
Merged

Conversation

davidchambers
Copy link
Member

The code is more complex than I would like it to be, but I believe the complexity is necessary. Please correct me if I'm wrong about this! :)

Copy link
Member

@Bradcomp Bradcomp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it looks OK. It is a little hard to follow what is going on, but once I sat with it a bit I got what was going on.

I think it should be possible to do it in one pass, but right now I am struggling to figure out how, so I could be wrong.

index.js Show resolved Hide resolved
@davidchambers
Copy link
Member Author

I think it should be possible to do it in one pass

Could you elaborate, Brad? I believe we're traversing the structure just once, with Z.reduce.

@Bradcomp
Copy link
Member

Ugh, I am tired today, but I see what is happening now. reduce is returning a Just Pair (Int (f a)), and we are rejecting if the int is > 0 because it means the foldable wasn't long enough. Otherwise we take the resultant array.

I had things inside out in my thinking.

Looks good to me!

@davidchambers davidchambers merged commit 52b661a into master Apr 26, 2019
@davidchambers davidchambers deleted the davidchambers/take-drop branch April 26, 2019 12:45
@masaeedu
Copy link
Member

Hi @davidchambers. I talked to @Bradcomp for a bit about this PR, I think maybe we don't really need the Applicative constraint on here.

@davidchambers
Copy link
Member Author

The Applicative constraint is not obvious. It does not arise from _takeDrop itself, but from the generalCase functions provided to _takeDrop in the definitions of take and drop:

var take = _takeDrop (
  function(n, xs) { return xs.slice (0, n); },
  function(n, xs, x) { return n > 0 ? Z.append (x, xs) : xs; }
  //                                  ^^^^^^^^^^^^^^^^
);
var drop = _takeDrop (
  function(n, xs) { return xs.slice (n); },
  function(n, xs, x) { return n > 0 ? xs : Z.append (x, xs); }
  //                                       ^^^^^^^^^^^^^^^^
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants