Aristarkos/notebooks/scope.jl

62 lines
1.3 KiB
Julia

### A Pluto.jl notebook ###
# v0.12.6
using Markdown
using InteractiveUtils
# ╔═╡ cef9e1f8-1b9f-11eb-1572-d776b0dd8817
arr_while = let
arr = Array{Function}(undef, 3)
i = 1
while i <= length(arr)
arr[i] = ()->i
i += 1
end
arr
end
# ╔═╡ 3d9ec092-1ba0-11eb-29b8-63b3f1e75a13
# the while loop "i" is capture in all the closures and is the same for all of them
arr_while[1](), arr_while[2]()
# ╔═╡ 651d6c0e-1ba0-11eb-2af8-1b1784dbe9b1
arr_for = let
arr = Array{Function}(undef, 4)
for i=1:length(arr)
arr[i] = ()->i
end
arr
end
# ╔═╡ 4b602bfc-1ba1-11eb-12a9-e59a34c1eaae
# the captured i is different here: for allocates a new variable each time
arr_for[2](), arr_for[3]()
# ╔═╡ 7a853616-1ba1-11eb-3ec2-b116cb4e08d3
# reuse of variables as loop index
begin
local i = 0
for outer i in 1:4
#...
end
local msg :: String
for outer msg in ["Hello", "World"]
#...
end
i, msg
end
# ╔═╡ 2941d9fc-1ba2-11eb-0edb-eb46ed882aa0
# constants
const a = 3
# ╔═╡ Cell order:
# ╠═cef9e1f8-1b9f-11eb-1572-d776b0dd8817
# ╠═3d9ec092-1ba0-11eb-29b8-63b3f1e75a13
# ╠═651d6c0e-1ba0-11eb-2af8-1b1784dbe9b1
# ╠═4b602bfc-1ba1-11eb-12a9-e59a34c1eaae
# ╠═7a853616-1ba1-11eb-3ec2-b116cb4e08d3
# ╠═2941d9fc-1ba2-11eb-0edb-eb46ed882aa0