Doto macro?

Is there something like clojure’s doto macro readily available? 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

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

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

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

@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.

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

or

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

vs

@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,

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

should be equivalent to

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…

1 Like

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.