Skip to content

Generalized comprehension syntax #4470

Description

@lindahua

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)...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions