# What does this 'do' syntax mean?

**URL:** <https://discourse.julialang.org/t/what-does-this-do-syntax-mean/25041>\
**Category:** New to Julia\
**Created:** [June 7, 2019, 11:13am UTC](https://discourse.julialang.org/t/what-does-this-do-syntax-mean/25041 "2019-06-07T11:13:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)\
**Post date:** [June 7, 2019, 11:13am UTC](https://discourse.julialang.org/t/what-does-this-do-syntax-mean/25041/1 "2019-06-07T11:13:48Z")

</div>

I found the following example in the [README for the Meshing package](https://github.com/JuliaGeometry/Meshing.jl)

```julia
using Meshing
using GeometryTypes
using LinearAlgebra: dot, norm
using FileIO

# generate an SDF of a sphere
sdf_sphere = SignedDistanceField(HyperRectangle(Vec(-1,-1,-1.),Vec(2,2,2.))) do v
    sqrt(sum(dot(v,v))) - 1 # sphere
end

m = GLNormalMesh(sdf_sphere, MarchingCubes())

save("sphere.ply",m)

```

I am trying to work out what that `SignedDistanceField(...) do ... end` syntax means. It seems that this code samples the function `sqrt(sum(dot(v,v))) - 1` at intervals of 0.1, but what Julia syntax is being used to accomplish this?

More specifically, for `SignedDistanceField`, how does one change the resolution to something other than 0.1? I looked at the documentation for SignedDistanceField (in the GeometryTypes package), but unfortunately it’s not much help:

```julia
  A SignedDistanceField is a uniform sampling of an implicit function. The bounds field corresponds to the sampling
  space intervals on each axis. The data field represents the value at each point whose exact location can be
  rationalized from bounds. The type is parameterized by:

    • N - The dimensionality of the sampling space.

    • SpaceT - the type of the space where we will uniformly sample.

    • FieldT - the type resulting from evaluation of the implicit function.

  Note that decoupling the space and field types is useful since geometry can be formulated with integers and distances
  can be measured with floating points.

  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

  Construct a SignedDistanceField by sampling a function over the bounds at the specified resolution (default = 0.1).
  Note that the sampling grid must be regular, so a new HyperRectangle will be generated for the SignedDistanceField
  that may have larger maximum bounds than the input HyperRectangle. The default Field type is Float64, but this can be
  changed with the fieldT argument.

```

It mentions that the default resolution but doesn’t mention how to change it.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [June 7, 2019, 11:19am UTC](https://discourse.julialang.org/t/what-does-this-do-syntax-mean/25041/2 "2019-06-07T11:19:14Z")

</div>

Have a look at [https://docs.julialang.org/en/v1/manual/functions/index.html#Do-Block-Syntax-for-Function-Arguments-1](https://docs.julialang.org/en/v1/manual/functions/index.html#Do-Block-Syntax-for-Function-Arguments-1). The `do` syntax basically just passes an anonymous function as the first argument to the function.

---

<div class="post-metadata">

**Author:** ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)\
**Post date:** [June 7, 2019, 2:33pm UTC](https://discourse.julialang.org/t/what-does-this-do-syntax-mean/25041/3 "2019-06-07T14:33:23Z")

</div>

Here is a trivial example:

```julia
my_function(f, container) = begin
    for element in container
        f(element)
    end
    return nothing
end

my_function([1,2,3]) do x # equivlent to my_function(print, [1,2,3])
    print(x)
end

```

Instead of specifying `(x)->print(x)` as an argument of `my_function`, with the `do` synatx one can write (generally larger) functions that operate on the rest of the arguments in the signature (in this case, the argument `container`)

---

<div class="post-metadata">

**Author:** ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)\
**Post date:** [June 7, 2019, 3:39pm UTC](https://discourse.julialang.org/t/what-does-this-do-syntax-mean/25041/4 "2019-06-07T15:39:28Z")

</div>

Maybe this is obvious from the other replies, but the `do` syntax plays no part in (directly) determining the resolution.

I’ve never used this package but [these lines](https://github.com/JuliaGeometry/Meshing.jl/blob/2c0995c3f42944723a5113d0ff53044669e06676/test/runtests.jl#L84-L86) from the tests file of Meshing, seem to indicate that you can specify a different resolution with the third argument to `SignedDistanceField`. So in the example you cite, you could write the following to change the resolution to 0.025.

```julia
resolution = 0.025
sdf_sphere = SignedDistanceField(HyperRectangle(Vec(-1,-1,-1.),Vec(2,2,2.)),resolution) do v
    sqrt(sum(dot(v,v))) - 1 # sphere
end

```

This is because, with the `do` syntax, the first argument is the body of code after `do`, and the second, third, etc… are specified as the arguments before `do`. So you could, equivalently, write the following:

```julia
sdf_sphere = SignedDistanceField(v -> sqrt(sum(dot(v,v))) - 1,
    HyperRectangle(Vec(-1,-1,-1.),Vec(2,2,2.)),0.025)

```

I often find that I can identify missing functionality of an undocumented feature just be reading through `tests/runtests.jl`, as that generally has to demonstrate most of the intended use cases of a package. That’s how I figured out the answer to your question, in this case.
