Le Minaw
Le Minaw

Reputation: 885

Using Julia's pmap() with comprehensions

With a simple function such as

function fun(x::Real, y::Real)
    x, y
end

that i want to call using pmap() by doing

pmap(fun, [x for x=0:.1:2], [y for y=4:-.1:2])

Julia gives this error

ERROR: LoadError: MethodError: Cannot `convert` an object of type Tuple{Float64,Float64} to an object of type AbstractFloat
This may have arisen from a call to the constructor AbstractFloat(...),
since type constructors fall back to convert methods.

I don't really get what's happening here.

According to some research I've done:

It's well-established that to call map on an N-argument function, you pass N lists (or whatever collection) to map:

julia> map(+, (1,2), (3,4))
(4,6)

What's wrong then?

Upvotes: 1

Views: 238

Answers (1)

HarmonicaMuse
HarmonicaMuse

Reputation: 7893

Which version of Julia are you using? Please update to the latest stable release (0.6.x), as this work fine with the current release, this esample was ran at JuliaBox:


jrun@notebook-0hnhf:/home/jrun$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _' |  |
  | | |_| | | | (_| |  |  Version 0.6.2 (2017-12-13 18:08 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu'

julia> function fun(x::Real, y::Real)
           x, y
       end
fun (generic function with 1 method)

julia> pmap(fun, [x for x = 0:.1:2], [y for y = 4:-.1:2])
21-element Array{Tuple{Float64,Float64},1}:
 (0.0, 4.0)
 (0.1, 3.9)
 (0.2, 3.8)
 (0.3, 3.7)
 (0.4, 3.6)
 (0.5, 3.5)
 (0.6, 3.4)
 (0.7, 3.3)
 (0.8, 3.2)
 (0.9, 3.1)
 (1.0, 3.0)
 (1.1, 2.9)
 (1.2, 2.8)
 (1.3, 2.7)
 (1.4, 2.6)
 (1.5, 2.5)
 (1.6, 2.4)
 (1.7, 2.3)
 (1.8, 2.2)
 (1.9, 2.1)
 (2.0, 2.0)

If you are not going to transform or filters the collected elements of the range, then you could also simply call collect(4:-.1:2) instead of [y for y = 4:-.1:2].

If you just need to iterate over the values of the range, then you don't even need to collect the values, just use the ranges as is instead, ie:

pmap(fun, 0:.1:2, 4:-.1:2)

Upvotes: 1

Related Questions