# Doto macro?

**URL:** <https://discourse.julialang.org/t/doto-macro/30766>\
**Category:** General Usage\
**Tags:** package\
**Created:** [November 5, 2019, 8:30pm UTC](https://discourse.julialang.org/t/doto-macro/30766 "2019-11-05T20:30:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Orbots](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orbots/32/3392_2.png) [@Orbots](https://discourse.julialang.org/u/Orbots)\
**Post date:** [November 5, 2019, 8:30pm UTC](https://discourse.julialang.org/t/doto-macro/30766/1 "2019-11-05T20:30:07Z")

</div>

Is there something like clojure’s `doto` macro readily available? [doto - clojure.core | ClojureDocs - Community-Powered Clojure Documentation and Examples](https://clojuredocs.org/clojure.core/doto).  
“Evaluates x then calls all of the methods and functions with the  
value of x supplied at the front of the given arguments.”

It also kind of models OOP after you make that first method call. Since `this->` is implicit in any method calls “inside” the class.

I’m sure a number of you could write one the in space below vvvvvv

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [November 5, 2019, 8:54pm UTC](https://discourse.julialang.org/t/doto-macro/30766/2 "2019-11-05T20:54:20Z")

</div>

May I ask why one would want/need this? It seems like writing things out the regular way is perfectly fine?

```julia
x = whatever
f(x, a)
g(x, b,c)
h(x, d)

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 5, 2019, 9:04pm UTC](https://discourse.julialang.org/t/doto-macro/30766/3 "2019-11-05T21:04:20Z")

</div>

So it’s a reverse `do` block, multiple thing in the first line, single argument in the body

---

<div class="post-metadata">

**Author:** ![Orbots](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orbots/32/3392_2.png) [@Orbots](https://discourse.julialang.org/u/Orbots)\
**Post date:** [November 6, 2019, 12:00am UTC](https://discourse.julialang.org/t/doto-macro/30766/4 "2019-11-06T00:00:07Z")

</div>

@carstenbauer Less typing, less stuff to track, better composition with a functional style of programming. The first argument position has a special place in julia.

For example threading treats the first position as special. I’ll take your example and pass the results from f to g to h.

```julia
x = whatever
f(x, a) |> b->g(x, b, c) |> d->h(x, d)

```

or

```julia
  h(x, g(x, f(x, a), c))

```

vs

```julia
@doto whatever
   f(a) |> g(c) |> h
end

@doto whatever
  h( g( f(a), c))
end

```

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [November 6, 2019, 12:41am UTC](https://discourse.julialang.org/t/doto-macro/30766/6 "2019-11-06T00:41:14Z")

</div>

> [@Orbots](#):
>
> ```julia
> @doto whatever
> f(a) |> g(c) |> h
> end
> 
> @doto whatever
> h( g( f(a), c))
> end
> 
> ```

Are these really legit uses of Clojure’s `doto` macro, and do they really do what you (seem to) want them to do?

For instance, taking your second example and following the documentation of `doto`,

```julia
@doto x begin
  h( g( f(a), c))
end

```

should be equivalent to

```julia
h(x, g(f(a), c))

```

I.e. I don’t see why inner calls to `g` or `f` would get an additional first `x` argument…

---

<div class="post-metadata">

**Author:** ![Orbots](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orbots/32/3392_2.png) [@Orbots](https://discourse.julialang.org/u/Orbots)\
**Post date:** [November 6, 2019, 1:24am UTC](https://discourse.julialang.org/t/doto-macro/30766/7 "2019-11-06T01:24:23Z")

</div>

> [@ffevotte](#):
>
> Are these really legit uses of Clojure’s `doto` macro

Not really. Which is why I asked for something “like” doto. Nesting, no nesting, different syntax but similar effect, whatever.

I started on something but it had enough annoying corner-cases that I was spending more time on it than it was going to save me! It’s not a simple thing to design, and I don’t think I had it right. I suppose you end up with monads if you think about it long enough.

Actually Transducers.jl might give me what I want. Then I can sort of defer referencing the object to the reducing step and compose whatever methods I need without explicitly referencing the object I want stuff “done” to/with. I’d need to write methods with an arity one smaller ( no object ) for everything.
