I think anonymous function usage and list comprehensions are separate issues.
Anonymous functions are not type-specialized, so they’re basically always slow. I avoid them in any code that’s not one-off. List comprehensions are type-specialized, but the inference for them isn’t super robust, especially in the global scope. There’s a couple of points of uncertainty: (1) how long will the result be?, (2) what’s the return type of the expression being evaluated in the comprehension, (3) does anything in the comprehension mutate anything? If you use typed list comprehensions, you can work around (2), but (1) and (3) are still potentially issues. That said, array comprehensions aren’t a lot slower than explicit loops, so many people like them — especially people coming from Python, where they’re very popular. — John On Jul 6, 2014, at 6:22 AM, Michiaki ARIGA <che...@gmail.com> wrote: > Thank you for your correction, Jim. > > In Apprentice.2, same approach with numpy version, I fixed as following. > > ``` > Z = rand(100,2) > X, Y = Z[:,1], Z[:,2] > R = sqrt(X.^2 + Y.^2) > T = atan2(Y,X) > ``` > > Also in Apprentice. 7, I fixed . position. > > I understand Julia works well without anonymous function, so when should I > use array comprehension? > Do I have to make function to use array comprehension? > > Many people advised to use array comprehension in this thread, but I don't > understand when to use it... > > > > che...@gmail.com > > > 2014-07-06 10:58 GMT+09:00 Michael Prentiss <mcprent...@gmail.com>: > Julia is not as performant with anonymous functions, and list comprehension. > > The compiler has a harder time with the optimization step. This is not a > surprise and is > known to the language designers. This is not a surprise. > > > On Saturday, July 5, 2014 8:01:46 PM UTC-5, james.dill...@gmail.com wrote: > > In Apprentice.6, I don't think you want to use the sqrtm(). sqrt() is > already vectorized over the matrix. Also a couple of '.'s are misplaced, so > perhaps instead: > > Z =rand(10,2) > > D = sqrt((Z[:,1].-Z[:,1]').^2+(Z[:,2].-Z[:,2]').^2) > > As a newbie myself, what surprised me is that this is faster and allocates > less memory than the comprehension: > > D = [norm(Z[i,:]-Z[j,:],2) for i = 1:10, j = 1:10] > > I am sure someone else here can explain why. > > Jim > On Sunday, June 22, 2014 10:43:32 AM UTC-4, Michiaki Ariga wrote: > Hi all, > > I'm a Julia newbee, and I'm trying to learn Julia and wrote Julia version of > rougier's 100 numpy > exercises(http://www.loria.fr/~rougier/teaching/numpy.100/index.html). > > https://github.com/chezou/julia-100-exercises > > I'd like you to tell me more "julia way" or something wrong with. > > Best regards, > Michiaki >