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

subsection on let blocks #188

Closed
StefanKarpinski opened this issue Aug 28, 2011 · 6 comments
Closed

subsection on let blocks #188

StefanKarpinski opened this issue Aug 28, 2011 · 6 comments
Assignees
Labels
docs This change adds or pertains to documentation

Comments

@StefanKarpinski
Copy link
Member

We don't explain let blocks since they're not control structures, per se. However, they're mentioned as introducing new variable scope blocks in [[Variables and Scoping]]. We need to explain them somewhere. @JeffBezanson, can you explain this, optimally with a few example to distinguish from other variables binding constructs — such as plain old assignment? I.e. what's the difference between these three blocks:

let x = 1
  # do something
end

begin
  local x = 1
  # do something
end

for x = [1]
  # do something
end
@ghost ghost assigned JeffBezanson Aug 28, 2011
@JeffBezanson
Copy link
Member

The difference is that other scope blocks just control visibility, but let allocates new bindings (where a binding is defined as the location where a variable is stored). This difference is only visible when you have closures, which allow variables to live longer than the blocks that introduced them:

Fs = cell(2)
for i=1:2
    Fs[i] = ()->i
end

Fs[1]()  =>  2
Fs[2]()  =>  2
Fs = cell(2)
for i=1:2
    let x=i
        Fs[i] = ()->x
    end
end

Fs[1]()  =>  1
Fs[2]()  =>  2

@StefanKarpinski
Copy link
Member Author

Awesome, I can include this example in the docs. I wonder if this shouldn't be the behavior of for though, especially in the context of parallel computation. It's a little unorthodox, but what if each iteration of a for loop actually semantically had a different binding. Of course, in most cases it wouldn't make any difference. When is the former behavior ever more useful?

@JeffBezanson
Copy link
Member

It's also the case that

let a=b
  code
end

is equivalent to (a->code)(b).

In the case of for loops, the former behavior doesn't seem too useful, but it's much more efficient for common uses, even when closures are involved:

for i=a:b
    map(x->x+i, ...)
    ...
end

Here the closure is only used in the loop, so not only is a new binding not needed, but allocating the closure itself can be lifted out of the loop because you can keep reusing it and just update the binding inside.

@JeffBezanson
Copy link
Member

What section of the docs is this going in?

@StefanKarpinski
Copy link
Member Author

Variables and Scoping seems like the only reasonable place, right?

On Aug 31, 2011, at 12:21 PM, [email protected] wrote:

What section of the docs is this going in?

Reply to this email directly or view it on GitHub:
#188 (comment)

@JeffBezanson
Copy link
Member

Done!

StefanKarpinski pushed a commit that referenced this issue Feb 8, 2018
Support kw argument in call overload.
cmcaine pushed a commit to cmcaine/julia that referenced this issue Sep 24, 2020
LilithHafner pushed a commit to LilithHafner/julia that referenced this issue Oct 11, 2021
KristofferC pushed a commit that referenced this issue Feb 14, 2025
Stdlib: Statistics
URL: https://github.com/JuliaStats/Statistics.jl.git
Stdlib branch: master
Julia branch: master
Old commit: d49c2bf
New commit: 77bd570
Julia version: 1.13.0-DEV
Statistics version: 1.11.2(Does not match)
Bump invoked by: @nalimilan
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
JuliaStats/Statistics.jl@d49c2bf...77bd570

```
$ git log --oneline d49c2bf..77bd570
77bd570 Fix `quantile` doctest (#188)
bfa5c6b Merge pull request #184 from JuliaStats/an/quantilemuladd
6bd1531 Update src/Statistics.jl
44d51c7 Use muladd in aleph calculation in _quantile to avoid some rounding errors.
793733e Bump codecov/codecov-action from 4 to 5 (#181)
```

Co-authored-by: nalimilan <[email protected]>
KristofferC pushed a commit that referenced this issue Feb 14, 2025
Stdlib: Statistics
URL: https://github.com/JuliaStats/Statistics.jl.git
Stdlib branch: master
Julia branch: master
Old commit: d49c2bf
New commit: 77bd570
Julia version: 1.13.0-DEV
Statistics version: 1.11.2(Does not match)
Bump invoked by: @nalimilan
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
JuliaStats/Statistics.jl@d49c2bf...77bd570

```
$ git log --oneline d49c2bf..77bd570
77bd570 Fix `quantile` doctest (#188)
bfa5c6b Merge pull request #184 from JuliaStats/an/quantilemuladd
6bd1531 Update src/Statistics.jl
44d51c7 Use muladd in aleph calculation in _quantile to avoid some rounding errors.
793733e Bump codecov/codecov-action from 4 to 5 (#181)
```

Co-authored-by: nalimilan <[email protected]>
(cherry picked from commit 504cbc3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

2 participants