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

iteration variables should be local to comprehensions #547

Closed
rtzui opened this issue Mar 8, 2012 · 11 comments
Closed

iteration variables should be local to comprehensions #547

rtzui opened this issue Mar 8, 2012 · 11 comments
Assignees

Comments

@rtzui
Copy link

rtzui commented Mar 8, 2012

x="a"
y=[ sqrt(x) | x = 1:10 ]

x==10 is true
I think that's strange. x should be a local variable in that loop. Also Haskell style comprehensions are cool. On the one hand like map:

x=[1,2,3,4,510,1,2,3,1,9]
y=[sqrt(a) | a ∈ x]

On the other hand more powerful:

x=[1,2,3,4,510,1,2,3,1,9]
y=[sqrt(a) | a ∈ x, x%2==0]

I 'd love unicode keywords (as valid alternative form), but i guess nobody else does.

@pao
Copy link
Member

pao commented Mar 9, 2012

See also #524 for scoping in list comprehensions at the REPL. What does your example do if you embed it in a function, making x non-global? I can check when I get home if no one else gets to it by then.

function lctest()
    x = "a"
    y = [ sqrt(x) | x = 1:10 ]
    x == 10
end

@pao
Copy link
Member

pao commented Mar 9, 2012

Okay, this does look like something different, then.

julia> function lctest()
           x = "a"
           y = [ sqrt(x) | x = 1:10 ]
           x == 10
       end

julia> lctest()
true

julia> function lctest()
           x::String = "a"
           y = [ sqrt(x) | x = 1:10 ]
           x == 10
       end

julia> lctest()
in ref: arrayref: index out of range
 in ref at /home/patrick/julia/jl/array.jl:165
 in tuple_elim_pass at /home/patrick/julia/jl/inference.jl:1521
 in typeinf at /home/patrick/julia/jl/inference.jl:1059
 in typeinf_ext at /home/patrick/julia/jl/inference.jl:805

@JeffBezanson
Copy link
Member

Congratulations, you found a compiler bug :)

@JeffBezanson
Copy link
Member

It's maybe not ideal, but you can accomplish this as:

x=[1,2,3,4,510,1,2,3,1,9]
y=[sqrt(a) | a in x[x%2==0] ]

@pao
Copy link
Member

pao commented Mar 9, 2012

Okay, with that out of the way, it looks like there's three separate issues here:
(1) Variables assigned to within the context of a listcomp may be captured from the enclosing scope iff the variable is already defined, which is unexpected:

julia> [ i | i = 1:10 ]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

julia> i
i not defined

julia> i = 'a'
'a'

julia> [ i | i = 1:10 ]
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

julia> i
10

The same thing happens with enclosing function definitions.

(2) Request that list comprehensions support filters/guards as in Haskell or Python. My Scheme reading abilities aren't terribly good but it doesn't look like that's in there. I agree it would be nice. I've spun this into #550. And Jeff is working faster than I can type this.

(3) Request for support for Unicode forms for certain operators. Editor and REPL support would be required to make this practical for people with normal keyboards. The Haskell editor Yi supports certain automatic Unicode replacements (see https://github.com/yi-editor/yi/blob/master/yi/src/library/Yi/Char/Unicode.hs). Given how few special operators exist in the language I'm not sure how useful this is overall. There's ->, I suppose.

@rtzui
Copy link
Author

rtzui commented Mar 9, 2012

to unicode: I'm supprised this works:

julia> π=3.141
3.141

julia> r=5
5

julia> 円=π_r_r
78.525

awesome.
I guess unicode keywords should be just an alternative, so no hacking the REPL would be necessary, but editors could (didn't have to) implement this. I just got new a idear for unicode symbols and created issue #552.

@JeffBezanson
Copy link
Member

Of course that works! :)
I agree it would be nice to parse unicode operators properly so they can optionally be used. No reason not to, as long as they're not required in order to use core language features.

@JeffBezanson
Copy link
Member

Since we have issues for the other parts of this, I'm going to change the title of this one.

@diegozea
Copy link
Contributor

This examples doesn't work... Am I missing something?

julia> [ sqrt(x) | x = 1:10 ]
ERROR: syntax: malformed function argument (call sqrt x)

julia> [ i | i = 1:10 ]
ERROR: syntax: function argument names not unique

@JeffBezanson
Copy link
Member

We've used for instead of | for quite a while now.

@diegozea
Copy link
Contributor

Thanks @JeffBezanson I was using for since I have memory of Julia. So when I saw this notation I tried to reproduce it by curiosity.

ViralBShah pushed a commit that referenced this issue Aug 12, 2024
Stdlib: SparseArrays
URL: https://github.com/JuliaSparse/SparseArrays.jl.git
Stdlib branch: main
Julia branch: master
Old commit: e61663a
New commit: 55976a6
Julia version: 1.12.0-DEV
SparseArrays version: 1.12.0
Bump invoked by: @ViralBShah
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
JuliaSparse/SparseArrays.jl@e61663a...55976a6

```
$ git log --oneline e61663a..55976a6
55976a6 Keep sparse solvers docs as before (#552)
95fd7ff Missing space in error message (#554)
b8a13ef implement in-place `ldiv!` for Cholesky factorization (#547)
1527014 Do not use nested dissection by default. (#550)
```

Co-authored-by: Dilum Aluthge <[email protected]>
lazarusA pushed a commit to lazarusA/julia that referenced this issue Aug 17, 2024
…aLang#55469)

Stdlib: SparseArrays
URL: https://github.com/JuliaSparse/SparseArrays.jl.git
Stdlib branch: main
Julia branch: master
Old commit: e61663a
New commit: 55976a6
Julia version: 1.12.0-DEV
SparseArrays version: 1.12.0
Bump invoked by: @ViralBShah
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
JuliaSparse/SparseArrays.jl@e61663a...55976a6

```
$ git log --oneline e61663a..55976a6
55976a6 Keep sparse solvers docs as before (JuliaLang#552)
95fd7ff Missing space in error message (JuliaLang#554)
b8a13ef implement in-place `ldiv!` for Cholesky factorization (JuliaLang#547)
1527014 Do not use nested dissection by default. (JuliaLang#550)
```

Co-authored-by: Dilum Aluthge <[email protected]>
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

4 participants