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

Generalized comprehension syntax #4470

Closed
lindahua opened this issue Oct 10, 2013 · 13 comments
Closed

Generalized comprehension syntax #4470

lindahua opened this issue Oct 10, 2013 · 13 comments
Milestone

Comments

@lindahua
Copy link
Contributor

What about the following?

(f(x) for x in a)   
# this may be some indexable view of a special type that can be 
# passed as arguments, such that we can do

sum(f(x) for x in a) 

In this way, statistics can be easily implemented in an efficient way, for example

var(a) = sum(abs2(x - mean(x)) for x in a) / (length(a) - 1)

This is a one-liner, but it does not create any temporary arrays.

If the Cartesian iteration discussed in #1917 can be implemented, this would become even more powerful:

# weighted mean
wmean(a, ws) = dot(a, ws) / sum(ws)

# weighted variance
wvar(a, ws) = sum(abs2(x - mean(x)) * w for (x, w) in (a, ws)...)
@kmsquire
Copy link
Member

A known as a 'generator' in python parlance. I swear this was discussed previously, but I can't find an issue, so perhaps it was only on the mailing list (see here: https://groups.google.com/forum/#!topic/julia-dev/iGVJ-61UmVk).

@lindahua
Copy link
Contributor Author

I also remembered I saw the discussion somewhere. But I did not actually find an issue here, so I open one.

This, IMHO, is worth our attention.

@JeffBezanson
Copy link
Member

Yes this would be a really good feature.

@johnmyleswhite
Copy link
Member

+1

@StefanKarpinski
Copy link
Member

Yes, I've long wanted this. One nice feature is that although it in principle uses tasks which may require multiple stacks and therefore might be slow, there are probably many cases where one can determine that it isn't necessary to actually have two stacks so it can be optimized away to very efficient code.

@JeffBezanson
Copy link
Member

It's a Map iterator, that wraps another iterator and applies some function to each of its elements.

@StefanKarpinski
Copy link
Member

Ah, ok. That will work as well.

@quinnj
Copy link
Member

quinnj commented Aug 21, 2014

+1. Looking forward to having this.

@mbauman
Copy link
Member

mbauman commented Aug 21, 2014

See my experiments on the mailing list with an implementation on my generators branch. (You have to manually include("base/generator.jl"), which I had forgotten myself when I rebased it today).

(edit: thanks @rfourquet)

@rfourquet
Copy link
Member

without an "s" : include("base/generator.jl")

@rfourquet
Copy link
Member

On top of @mbauman branch, I tried to prevent expensive allocations, at least up to dim 3.

@quinnj
Copy link
Member

quinnj commented Aug 21, 2014

As there's quite a bit of interest/work going on here, it'd be great if we could knock out a few other long-standing comprehension issues as well:

@diegozea
Copy link
Contributor

About #1457 and #550... Could the output be preallocated and finally resized to its final length?
Maybe...

T[ f(a) for a in A  if g(a) ]

Can be something like...

N = length(A)
out = Array(T,N)
j = 0
for i in 1:N
  if g( A[i] )
    j += 1
    out[j] = f( A[i] )
  end
end
resize!(out, j)

(push! is slower than this)

JeffBezanson added a commit that referenced this issue Jan 24, 2016
fix #4470 Generalized comprehension syntax
fix #7258 type-inference-independent comprehensions
JeffBezanson added a commit that referenced this issue Jan 26, 2016
fix #4470 Generalized comprehension syntax
fix #7258 type-inference-independent comprehensions

[ci skip]
JeffBezanson added a commit that referenced this issue Jan 26, 2016
fix #4470 Generalized comprehension syntax
fix #7258 type-inference-independent comprehensions

[ci skip]
JeffBezanson added a commit that referenced this issue Jan 27, 2016
fix #4470 Generalized comprehension syntax
fix #7258 type-inference-independent comprehensions

[ci skip]
JeffBezanson added a commit that referenced this issue Jan 29, 2016
fix #4470 Generalized comprehension syntax
fix #7258 type-inference-independent comprehensions

[ci skip]
JeffBezanson added a commit that referenced this issue Jan 29, 2016
fix #4470 Generalized comprehension syntax
fix #7258 type-inference-independent comprehensions

[ci skip]
JeffBezanson added a commit that referenced this issue Feb 10, 2016
This introduces the types `Generator`, which maps a function over
an iterator, and `IteratorND`, which wraps an iterator with a
shape tuple.
JeffBezanson added a commit that referenced this issue Feb 11, 2016
This introduces the types `Generator`, which maps a function over
an iterator, and `IteratorND`, which wraps an iterator with a
shape tuple.
JeffBezanson added a commit that referenced this issue Feb 12, 2016
This introduces the types `Generator`, which maps a function over
an iterator, and `IteratorND`, which wraps an iterator with a
shape tuple.
JeffBezanson added a commit that referenced this issue Feb 15, 2016
This introduces the types `Generator`, which maps a function over
an iterator, and `IteratorND`, which wraps an iterator with a
shape tuple.
JeffBezanson added a commit that referenced this issue Feb 17, 2016
This introduces the types `Generator`, which maps a function over
an iterator, and `IteratorND`, which wraps an iterator with a
shape tuple.
JeffBezanson added a commit that referenced this issue Feb 18, 2016
This introduces the types `Generator`, which maps a function over
an iterator, and `IteratorND`, which wraps an iterator with a
shape tuple.
JeffBezanson added a commit that referenced this issue Feb 18, 2016
This introduces the types `Generator`, which maps a function over
an iterator, and `IteratorND`, which wraps an iterator with a
shape tuple.
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