Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying Julia function to nested array of arrays

Is there a simpler way to do apply a function in Julia to nested array than defining a new function? - e.g. for this simple example:

a = collect(1:10)
b = [ a*i for i in 100:100:400]

arraylog(x) = log.(x) ## Need to define an extra function to do the inner array?
arraylog.(b)
like image 557
MR_MPI-BGC Avatar asked Dec 08 '25 01:12

MR_MPI-BGC


1 Answers

I would use a comprehension just like you used it to define b: [log.(x) for x in b].

The benefit of this approach is that such code should be easy to read later.

EDIT

Referring to the answer by Tasos actually a comprehension implicitly defines an anonymous function that is passed to Base.Generator. In this use case a comprehension and map should be largely equivalent.

I assumed that MR_MPI-BGC wanted to avoid defining an anonymous function. If it were allowed one could also use a double broadcast like this:

(x->log.(x)).(b)

which is even shorer but I thought that it would not be very readable in comparison to a comprehension.

like image 80
Bogumił Kamiński Avatar answered Dec 13 '25 01:12

Bogumił Kamiński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!