Anonymous function/comprehension coding

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.

    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

Maybe something like

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

?

Haven’t tested it though…

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.

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 !!!

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.

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

3 Likes

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.

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.

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.

There are very smart guys in this forum :wink: :wink: :wink:

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

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