# Help understanding an anonymous function

**URL:** https://discourse.julialang.org/t/help-understanding-an-anonymous-function/47591
**Category:** New to Julia
**Tags:** function
**Created:** [October 1, 2020, 10:29am UTC](https://discourse.julialang.org/t/help-understanding-an-anonymous-function/47591 "2020-10-01T10:29:02Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![psabela](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/psabela/32/17910_2.png) [@psabela](https://discourse.julialang.org/u/psabela)
#### Post date: [October 1, 2020, 10:29am UTC](https://discourse.julialang.org/t/help-understanding-an-anonymous-function/47591/1 "2020-10-01T10:29:02Z")

</div>

I am reviewing someone else’s code and came across this anonymous functions. Could you please help me understand: (1) when it is executed and how it is called, and (2) the syntax why there is “(token)” the second type passed in separate pair of parenthesis. I would have expected “function(m::Int64, token)”

function(m::Int64) (token)  
…  
…  
return a, b  
end

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [October 1, 2020, 12:29pm UTC](https://discourse.julialang.org/t/help-understanding-an-anonymous-function/47591/2 "2020-10-01T12:29:54Z")

</div>

The code exactly as you typed should not work (extra space) and what you are seeing is most likely not an anonymous function. I don’t know where you get that code from but you are most likely seeing [(d::Int)(x::Int) = d + x?](https://discourse.julialang.org/t/d-int-x-int-d-x/47321)

---

<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: [October 1, 2020, 12:39pm UTC](https://discourse.julialang.org/t/help-understanding-an-anonymous-function/47591/3 "2020-10-01T12:39:26Z")

</div>

That looks like a [callable object](https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects), not a closure (anonymous function).

---

<div class="post-metadata">

### Author: ![psabela](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/psabela/32/17910_2.png) [@psabela](https://discourse.julialang.org/u/psabela)
#### Post date: [October 1, 2020, 12:56pm UTC](https://discourse.julialang.org/t/help-understanding-an-anonymous-function/47591/4 "2020-10-01T12:56:51Z")

</div>

This is an exact cut and paste from JuliaCon2020 presentation, the url is [https://github.com/Ayushk4/JuliaCon20\_Talk/blob/master/Sentiment.ipynb](https://github.com/Ayushk4/JuliaCon20_Talk/blob/master/Sentiment.ipynb)  
(the code is a stand alone, 4th block).

My question is more about how this function is defined and executed.

function (m::SentimentModel)(token\_onehot)  
Flux.reset!(m.lstm1)  
Flux.reset!(m.lstm2)  
embed(x) = m.W\_Embed \* x’  
e = embed.(token\_onehot)  
w = @. m.lstm2(m.lstm1(e))  
logits = m.d(m.lstm2.state[1]) # We just need the last hidden state of the LSTM  
return Flux.softmax(logits), logits  
end

---

<div class="post-metadata">

### Author: ![psabela](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/psabela/32/17910_2.png) [@psabela](https://discourse.julialang.org/u/psabela)
#### Post date: [October 1, 2020, 1:19pm UTC](https://discourse.julialang.org/t/help-understanding-an-anonymous-function/47591/5 "2020-10-01T13:19:02Z")

</div>

Thank you for the direction. It is a FUNCTION-LIKE-OBJECT. More info here:  
[https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects](https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects)

---

<div class="post-metadata">

### Author: ![psabela](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/psabela/32/17910_2.png) [@psabela](https://discourse.julialang.org/u/psabela)
#### Post date: [October 1, 2020, 1:20pm UTC](https://discourse.julialang.org/t/help-understanding-an-anonymous-function/47591/6 "2020-10-01T13:20:35Z")

</div>

FYI. I found the answer. It is function-like-object. [Methods · The Julia Language](https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects)
