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

Would takewhile be a natural candidate for IterTools.jl? #51

Closed
reinermartin opened this issue Nov 14, 2018 · 16 comments
Closed

Would takewhile be a natural candidate for IterTools.jl? #51

reinermartin opened this issue Nov 14, 2018 · 16 comments

Comments

@reinermartin
Copy link
Contributor

reinermartin commented Nov 14, 2018

Would a takewhile function be a natural function to add to IterTools? It consumes a given iterator as long as a given condition is met.

So collect(TakeWhile(Base.Iterators.countfrom(1), x-> x^2 <= 50)) would yield [1, 2, 3, 4, 5, 6].

My own (simplistic?) implementation looks like this:

struct TakeWhile{I}
    xs::I
    cond::Function
end

Base.iterate(it::TakeWhile) = iterate(it.xs)

function Base.iterate(it::TakeWhile, state)
    (val, state) = iterate(it.xs, state)
    it.cond(state) || return nothing
    val, state
end

Base.IteratorSize(it::TakeWhile) = Base.SizeUnknown()
@iamed2
Copy link
Contributor

iamed2 commented Nov 14, 2018

Yes, although it could also be takeuntil

@reinermartin
Copy link
Contributor Author

reinermartin commented Nov 14, 2018

Sure; I was thinking of the Python module itertools, which provides takewhile(). Best is to have both, as this is such a basic functional programming concept.

@iamed2
Copy link
Contributor

iamed2 commented Nov 14, 2018

Ah well we can choose the Python-similar one then :)

@ararslan
Copy link
Member

FWIW takewhile is actually the standard name for this, and is provided in Scheme, C#, Haskell, Java, Kotlin, ...

@ararslan
Copy link
Member

Your implementation looks good, I'd just swap the argument order to the constructor so that the function comes first, which is conventional.

@reinermartin
Copy link
Contributor Author

reinermartin commented Dec 17, 2018

In the meantime, I have prepared code for takewhile with tests and doc, but I can not 'publish branch' or create a 'pull request'. I get

"Authentication failed. You may not have permission to access the repository or the repository may have been archived. Open options and verify that you're signed in with an account that has permission to access this repository"

if I try to publish from GitHub Desktop. Are there any special rights I need to do this?

Sorry, this is a total beginner's question - I have never contributed to any open source before.

@ararslan
Copy link
Member

You can fork the package to your account by clicking "Fork" in the upper right corner of the page, next to "Star" and "Watch." You can then add add your fork as a Git remote, push your changes to your fork, then open a PR based on that.

@reinermartin
Copy link
Contributor Author

Thanks ararslan, I'll try that now.

@reinermartin
Copy link
Contributor Author

reinermartin commented Dec 17, 2018

OK, I created a fork, added my code to three files manually, and created a pull request - first time for me!

@reinermartin
Copy link
Contributor Author

Oh, "All checks have failed" - would have been too easy... :-(

@reinermartin
Copy link
Contributor Author

So I fixed (one) bug (copied one 'end' too many) - do I have to create new pull request, or what do I need to do?

@ararslan
Copy link
Member

You can commit the changes to your branch then push to the remote again, which will automatically update the existing pull request.

@reinermartin
Copy link
Contributor Author

If this works out I'll add 'takeuntil' some other day

@iamed2
Copy link
Contributor

iamed2 commented Dec 18, 2018

If this works out I'll add 'takeuntil' some other day

Don't worry about it :)

@iamed2
Copy link
Contributor

iamed2 commented Dec 19, 2018

Added in #54

@iamed2 iamed2 closed this as completed Dec 19, 2018
@ararslan
Copy link
Member

I have never contributed to any open source before.

Your first contribution was a great one!

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

No branches or pull requests

3 participants