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

RFC/WIP: Conditional 1-D Generators #15023

Closed
wants to merge 3 commits into from
Closed

Conversation

quinnj
Copy link
Member

@quinnj quinnj commented Feb 10, 2016

This is based on the currently open PR #14848.

This adds the ability, in the 1-D case, to support conditional generators, otherwise known as "guards".

Ref #550.

Example

julia> t = (i for i in 1:10)
Base.Generator{##1#2,UnitRange{Int64},Base.##1#2}(#1,1:10,Base.#1)

julia> collect(t)
10-element Array{Int64,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

# manually construct a Generator with a "guard" or condition
julia> t = Base.Generator(x->x, 1:10, x->x%2 == 0)
Base.Generator{##7#9,UnitRange{Int64},##8#10}(#7,1:10,#8)

julia> collect(t)
5-element Array{Int64,1}:
  2
  4
  6
  8
 10

This doesn't yet support the necessary syntax parsing of the form:

t = (i for i in 1:10 if i % 2 == 0)

The collect implementation is probably too naive (not sure if we could get away with not having to evaluate every element), but I'm also not up to speed on the machinery map uses for determining the result element type.

This PR is more just to show a proof of concept and invite discussion of any other issues we'd need to consider with adding this feature.

@JeffBezanson
Copy link
Member

That was quick :)

I think the right way to do this is to wrap the iterator with a Filter iterator.

@tkelman
Copy link
Contributor

tkelman commented Feb 10, 2016

I like the sound of composing Generators with a Filter iterator.

@quinnj
Copy link
Member Author

quinnj commented Feb 16, 2016

There's an old Julian tradition that if you take a stab at something, it's a good way to encourage Jeff to do it 10x better & sooner. It was fun playing around with this, but a Filter sounds much better.

@quinnj quinnj closed this Feb 16, 2016
@quinnj quinnj deleted the jq/conditionalgenerators branch February 16, 2016 21:17
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

Successfully merging this pull request may close these issues.

5 participants