# Anonymous function/comprehension coding

**URL:** https://discourse.julialang.org/t/anonymous-function-comprehension-coding/107775
**Category:** New to Julia
**Tags:** best-practices
**Created:** [December 18, 2023, 3:59pm UTC](https://discourse.julialang.org/t/anonymous-function-comprehension-coding/107775 "2023-12-18T15:59:01Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![SergeantMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergeantmike/32/202613_2.png) [@SergeantMike](https://discourse.julialang.org/u/SergeantMike)
#### Post date: [December 18, 2023, 3:59pm UTC](https://discourse.julialang.org/t/anonymous-function-comprehension-coding/107775/1 "2023-12-18T15:59:01Z")

</div>

Having a difficult time wrapping my head around Anonymous function and Comprehensions coding, not an easy thing for someone who thinks in ecological terms.

What I am doing is doing a life-table calculation for the proportion of the cohort that has survived at time t. Basically my code looks like this.

```julia
    for idx = 1:length (VarData)                                                                                       
        buf - = VarData [idx]
        push!(cof,(buf/GrandTotal))
    end

```

where  
buf is the variable represents the number of individuals surviving at each time point and comes from taking the current survivorship and subtracting the number who died.

VarData is a vector array that contains the number of individuals that died in that time period.

GrandTotal is the total number of individuals present at time point zero.

So if initially  
buf=60  
GrandTotal=60  
and VarData=[1 1 6 7 16 19 8 2 0]

this results in the following vector array

[0.983 0.966 0.866 0.75 0.483 0.166 0.033]

which is the survivors (buf) divided by GrandTotal

The code above gives the correct answer but I can’t help but think there is some form of anonymous function or comprehension that I can use to make it more Julia (if that is the right word) that I cannot see.

Mike

---

<div class="post-metadata">

### Author: ![IlianPihlajamaa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ilianpihlajamaa/32/28766_2.png) [@IlianPihlajamaa](https://discourse.julialang.org/u/IlianPihlajamaa)
#### Post date: [December 18, 2023, 4:32pm UTC](https://discourse.julialang.org/t/anonymous-function-comprehension-coding/107775/2 "2023-12-18T16:32:06Z")

</div>

Maybe something like

```julia
result = (GrandTotal .- cumsum(VarData, dims=2)) ./ GrandTotal

```

?

Haven’t tested it though…

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 18, 2023, 4:34pm UTC](https://discourse.julialang.org/t/anonymous-function-comprehension-coding/107775/3 "2023-12-18T16:34:08Z")

</div>

In any case, no one-liner will be better than what you wrote, except perhaps for the syntax.

ps: Use commas to separate values in the arrays, to get vector and not 1-line matrices.

---

<div class="post-metadata">

### Author: ![vsoler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vsoler/32/30667_2.png) [@vsoler](https://discourse.julialang.org/u/vsoler)
#### Post date: [December 18, 2023, 4:35pm UTC](https://discourse.julialang.org/t/anonymous-function-comprehension-coding/107775/4 "2023-12-18T16:35:51Z")

</div>

Try this:

> a = [1, 1, 6, 7, 16, 19, 8, 2, 0]  
> (60 .- cumsum(a)) ./ 60

It will give you directly the answer you are looking for.  
Please note that there is a period before the minus (-) and before the slash (/). These are necessay in order to add (or divide into) a scalar and a vector.

So you need no loops at all !!!

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 18, 2023, 4:38pm UTC](https://discourse.julialang.org/t/anonymous-function-comprehension-coding/107775/5 "2023-12-18T16:38:57Z")

</div>

> [@vsoler](#):
>
> These are necessay in order to add (or divide into) a scalar and a vector.

`vector / scalar` is allowed without dot, because `vector * inv(scalar)` is a standard operation on vector spaces, just like `vector + vector`.

However, if you use `vector ./ scalar` and `vector .+ vector`, it computes the same thing but has the advantage of “fusing” with other dot calls. e.g. `(scalar .- vector) ./ scalar)` is computed in a single loop and allocates a single array. See also the [“More Dots” blog post](https://julialang.org/blog/2017/01/moredots/).

(Nor is there is anything wrong with writing a loop “manually” in Julia.)

---

<div class="post-metadata">

### Author: ![IlianPihlajamaa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ilianpihlajamaa/32/28766_2.png) [@IlianPihlajamaa](https://discourse.julialang.org/u/IlianPihlajamaa)
#### Post date: [December 18, 2023, 4:40pm UTC](https://discourse.julialang.org/t/anonymous-function-comprehension-coding/107775/6 "2023-12-18T16:40:03Z")

</div>

For both @vsoler and my solution, if you keep using a row vector as data (numbers separated by spaces instead of commas) you need to add `dims=2` to the cumsum function as keyword argument.

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [December 18, 2023, 4:42pm UTC](https://discourse.julialang.org/t/anonymous-function-comprehension-coding/107775/7 "2023-12-18T16:42:43Z")

</div>

Also note there are some synthatic errors in the code you posted.

Functions (like `length`) and vectors (like `VarData`) don’t want a space between them and the parameters/index.

---

<div class="post-metadata">

### Author: ![SergeantMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergeantmike/32/202613_2.png) [@SergeantMike](https://discourse.julialang.org/u/SergeantMike)
#### Post date: [December 18, 2023, 10:01pm UTC](https://discourse.julialang.org/t/anonymous-function-comprehension-coding/107775/8 "2023-12-18T22:01:47Z")

</div>

I have noted that when Imiq pointed it out.

Note: that the data is imported from a CSV and it is arranged properly in the code. I provided the data within VarData to illustrate in the best way possible what I was trying to accomplish. I apologize for the wrong syntax but it does seem that everyone did understand the goal here.

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [December 19, 2023, 6:57am UTC](https://discourse.julialang.org/t/anonymous-function-comprehension-coding/107775/9 "2023-12-19T06:57:41Z")

</div>

> [@SergeantMike](#):
>
> but it does seem that everyone did understand the goal here.

There are very smart guys in this forum 😉 😉 😉

---

<div class="post-metadata">

### Author: ![simsurace](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simsurace/32/30216_2.png) [@simsurace](https://discourse.julialang.org/u/simsurace)
#### Post date: [December 19, 2023, 7:33am UTC](https://discourse.julialang.org/t/anonymous-function-comprehension-coding/107775/10 "2023-12-19T07:33:39Z")

</div>

I don‘t think that there is anything wrong and un-idiomatic with writing a loop here, maybe it is even easier to understand.

---

<div class="post-metadata">

### Author: ![vsoler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vsoler/32/30667_2.png) [@vsoler](https://discourse.julialang.org/u/vsoler)
#### Post date: [December 19, 2023, 11:14am UTC](https://discourse.julialang.org/t/anonymous-function-comprehension-coding/107775/11 "2023-12-19T11:14:56Z")

</div>

Thank you, @stevengj, I learnt something new today !!!
