# Do-block for combining structures

**URL:** <https://discourse.julialang.org/t/do-block-for-combining-structures/7053>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 13, 2017, 9:15pm UTC](https://discourse.julialang.org/t/do-block-for-combining-structures/7053 "2017-11-13T21:15:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![igor.cerovsky](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@igor.cerovsky](https://discourse.julialang.org/u/igor.cerovsky)\
**Post date:** [November 13, 2017, 9:15pm UTC](https://discourse.julialang.org/t/do-block-for-combining-structures/7053/1 "2017-11-13T21:15:48Z")

</div>

Trying to find an example for the do-block syntax a I came to the following code: the anonymous function/closure in the do-block combines input arguments into a resulting structure. Is the code below correct as an example for this concept? Are there any cons/restrictions for using the do-block syntax (except the scope of variables inside the do-block); for example capturing the variables inside the do-block?

```julia
struct Foo
  name::String
  data::Vector{Float64}
end

struct Baz
  name::String
  info::Int
end

struct Bar
  name::String
  info::Int
  data::Vector{Vector{Float64}}
end

function combine(inFoo::Vector{Foo}, inBaz::Vector{Baz})
  names = unique([a.name for a in inFoo])
  bars = map(names) do a
    blocks = [b.data for b in inFoo if b.name == a]
    i = findfirst(r -> r.name == a, inBaz)
    info = i !=0 ? bazs[i].info : 0
    return Bar(a, info, blocks)
  end

  return bars
end

foos = [Foo("a", [1.0, 2.0]), Foo("a",[6.0]), Foo("b", [21.0, 22.0]), Foo("b", [25.0])]
bazs = [Baz("a", 10), Baz("b", 11)]
combine(foos, bazs)

```

---

<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:** [November 14, 2017, 8:33am UTC](https://discourse.julialang.org/t/do-block-for-combining-structures/7053/2 "2017-11-14T08:33:20Z")

</div>

> [@igor.cerovsky](#):
>
> correct as an example for this concept

If it does what you want, equivalently to `map(a -> ..., names)`, then yes.

About “capturing the variable”: if `name` wasn’t string but a mutable container that may be modified, then you would get tricky behavior, but that is orthogonal to `do` syntax and isn’t the case here anyway.

---

<div class="post-metadata">

**Author:** ![igor.cerovsky](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@igor.cerovsky](https://discourse.julialang.org/u/igor.cerovsky)\
**Post date:** [November 14, 2017, 8:45am UTC](https://discourse.julialang.org/t/do-block-for-combining-structures/7053/3 "2017-11-14T08:45:59Z")

</div>

thanks for pointing out the **mutable** container, though it is not the case here.
