# Do block syntax examples

**URL:** <https://discourse.julialang.org/t/do-block-syntax-examples/40757>\
**Category:** General Usage\
**Created:** [June 4, 2020, 7:34pm UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757 "2020-06-04T19:34:04Z")\
**Posts on this page:** 17\
**Page:** 2

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [June 4, 2020, 11:38pm UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/22 "2020-06-04T23:38:33Z")

</div>

Oh, sorry, that’s what you meant, yeah that get’s closed as well. As you said the do is really syntactic sugar for passing in a function.

---

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [June 5, 2020, 12:04am UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/23 "2020-06-05T00:04:07Z")

</div>

[Miles Lucas posted on zulip a few weeks ago a fun example of using a do-block for multi-line broadcast](https://julialang.zulipchat.com/#narrow/stream/225583-appreciation/topic/do.20blocks/near/196610684):

```julia
broadcast(A, B) do a, b
    q = a + b
    c = 3q - 4
    c / q
end

```

---

<div class="post-metadata">

**Author:** ![elron](https://avatars.discourse-cdn.com/v4/letter/e/4af34b/32.png) [@elron](https://discourse.julialang.org/u/elron)\
**Post date:** [June 5, 2020, 1:02am UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/24 "2020-06-05T01:02:56Z")

</div>

I’d like it even better if there was something like the Haskell `where` construct (not just for do notation), along the lines of

```julia
begin
c / q
where
q = a + b
c = 3q - 4
end

```

in the spirit of “show me the big picture, then the details”.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 5, 2020, 11:36am UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/25 "2020-06-05T11:36:38Z")

</div>

> [@klaff](#):
>
> Using an anonymous function for the first argument or creating a function first and then passing its name in do the same thing and are (to me - I recognize the subjectivity here!) more readable.

The following are equivalent (_inside a function body_):

1. An anonymous closure

2. a `do` block

3. a function that you name

While I like the `do` block syntax, I frequently use the third option too, especially because if `_f` is complicated, I can just return it from the containing function for further debugging (as a closure).

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 15, 2020, 9:48pm UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/26 "2020-12-15T21:48:16Z")

</div>

I saw today for the first time (I’m a beginner) the use of the map () do … end block.  
Trying to understand how it works, I did some tests.

The example I saw had this structure roughly:

```julia
itr=zip(1:12,'a':'l')

map(itr) do (n,c)
    c^n
end

```

I tried to rewrite it like this(which should be the translation in the in-line form)::

```julia
map((n,c)->c^n,itr)

```

but the result is different. indeed it does not seem to be a correct syntax.

to get the same result(\*) I had to use two different syntax:

1. `map(c->string(c[2]^c[1]),itr)`

2. `map((n,c)->c^n, collect(1:12),collect('a':'l'))`

therefore the two forms are not always "directly and simply " exchangeable?!?!?

(\*)

```julia
12-element Array{String,1}:
 "a"
 "bb"
 "ccc"
 "dddd"
 "eeeee"
 ⋮
 "hhhhhhhh"
 "iiiiiiiii"
 "jjjjjjjjjj"
 "kkkkkkkkkkk"
 "llllllllllll"

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 15, 2020, 10:07pm UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/27 "2020-12-15T22:07:29Z")

</div>

> [@rocco\_sprmnt21](#):
>
> `map((n,c)->c^n,itr)`

You want `map(((n,c),)->c^n,itr)` to [destructure the tuples](https://docs.julialang.org/en/v1/manual/functions/#Argument-destructuring) obtained by iterating over a `zip`.

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [December 16, 2020, 4:17am UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/28 "2020-12-16T04:17:17Z")

</div>

I like Python’s version much better. It has an `as` keyword which is more intuitive to me.

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [December 16, 2020, 4:50am UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/29 "2020-12-16T04:50:52Z")

</div>

It’s interesting to see how much preferences can vary here. I really enjoy the Julia/Ruby/Kotlin/Scala/Elixir model of exposing a lambda as a block instead of having dedicated context managers. Python context managers are inflexible and require a lot of boilerplate for custom implementations. `with` blocks also don’t create new scope and thus pollute the current scope with more names that you can’t use for other variables.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 16, 2020, 8:51am UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/30 "2020-12-16T08:51:41Z")

</div>

Then this means that the _do … end_ block does this _destructuring operation_ automatically (whatever that means 😀)?

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [December 16, 2020, 10:58am UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/31 "2020-12-16T10:58:16Z")

</div>

It’s not specific to the `do` block. Maybe it’s clearer when looking at a function that has both normal and destructured arguments:

```julia
function f((a,b), c) # Function of 2 arguments
    a+b+c
end

tup = (1,2)

f(tup, 3) # result: 6

```

An anonymous function doing the same would be `((a,b), c) -> a+b+c`.

With a `do` block it’s the same. Instead of `map(f, [tup, tup, tup], [3, 4, 5])` you can write

```julia
map([tup, tup, tup], [3, 4, 5]) do (a,b), c
    a+b+c
end

```

The only difference is that the `do` arguments are not written in parentheses, so `do a, b` declares a function of two arguments while `do (a, b)` declares a function of one argument, which is then destructed in two variables `a` and `b`. This is admittedly not obvious when looking at the `do (a, b)` syntax in isolation 🙂

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [December 16, 2020, 11:01am UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/32 "2020-12-16T11:01:25Z")

</div>

Since the thread has been revived, here’s an example where I find the `do` syntax particularly intuitive:

```julia
julia> using Makie

julia> scene = Scene()

julia> on(scene.events.mouseposition) do pos
           println("Mouse is at $pos")
       end

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 16, 2020, 11:48am UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/33 "2020-12-16T11:48:47Z")

</div>

I see the point.  
tanks.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 17, 2020, 12:30pm UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/34 "2020-12-17T12:30:08Z")

</div>

I ran into a situation where I tried to brodcast on a vector of tuples and ran into difficulties, which I got over.  
But I would like to understand better, how things are going.

I wanted to check if a given tuple was contained in a list of tuples and I used a similar to this expression which gave me error:

```
    julia> (2,3) .==[(2,3),(1,1),(5,4)]
    ERROR: DimensionMismatch("arrays could not be broadcast to a common size; got a dimension with lengths 2 and 3")

```

I solved it by changing it like this:

```
     ((2,3),) .==[(2,3),(1,1),(5,4)]

```

I would like to understand how it works instead in the following case:

```
julia> (2,) .== [(2,),3,4,2,5]
5-element BitArray{1}:
0
0
0
1
0

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 17, 2020, 12:35pm UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/35 "2020-12-17T12:35:19Z")

</div>

That’s just how broadcasting works - the tuple `(2,3)` on the left hand side is iterable, so broadcasting will try to iterate over its contents. You are trying to do

```julia
[2 == (2,3), 3 == (1,1), ⋆ == (5,4)]

```

where `⋆` here denotes the missing element on the left hand side that the error complains about (2 elements in the tuple on the LHS, 3 elements in your vector of tuples on the RHS).

Wrapping the LHS in a tuple is one way of doing this, another would be to use `Ref` to make clear that the LHS is to be treated as a scalar:

```julia
julia> Ref((2, 3)) .== [(2,3), (1,1), (5,4)]
3-element BitArray{1}:
 1
 0
 0

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 17, 2020, 1:14pm UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/36 "2020-12-17T13:14:14Z")

</div>

i tried this but without success:smile:

```
 julia> do (2,3) end .==[(2,3),(1,1),(5,4)]
 ERROR: syntax: invalid "do" syntax

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [December 17, 2020, 1:54pm UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/37 "2020-12-17T13:54:57Z")

</div>

What are you trying to do here?

An equivalent approach to the broadcast would be:

```julia
map([(2,3),(1,1),(5,4)]) do elem
    elem == (2, 3)
end

```

The `do` syntax is to be used after some method call that takes a `function`/closure/callable as first parameter. You ommit the first parameter (the callable) and instead define you a closure with the parameters after the `do` (in the same line) and the body of the closure in the following lines until the `end`.

---

<div class="post-metadata">

**Author:** ![spcogg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/spcogg/32/16943_2.png) [@spcogg](https://discourse.julialang.org/u/spcogg)\
**Post date:** [May 13, 2021, 12:09pm UTC](https://discourse.julialang.org/t/do-block-syntax-examples/40757/38 "2021-05-13T12:09:43Z")

</div>

Ok guys this confused the hell out of me until I saw the official do keyword documentation: [Essentials · The Julia Language](https://docs.julialang.org/en/v1/base/base/#do)

Basically, here’s what do does:

```julia
map(1:10, 11:20) do x, y
    x + y
end

```

is the same as:

```julia
map((x,y)->(x+y), 1:10, 11:20)

```

So anywhere that a function takes another function as a first argument, it is valid to use a do block to define what that first argument function is.

[Previous page](https://discourse.julialang.org/t/do-block-syntax-examples/40757.md?page=1)
