6

Suppose I do f(x,y) = 2x + ythen map(f, [2,4,6],[1,1,1]) I will get:

3-element Array{Int64,1}:
  5
  9
 13

If I do map(f, [2,4,6],1), i.e., I want to suppose that the second input is always the same, I will get:

1-element Array{Any,1}:
5

So this does not work. Is there a way to do this without coding a Vector [1,1,1] ?

1

2 Answers 2

7

Broadcast "acts like" it changes the arrays to be a size which works, and maps:

broadcast(f, [2,4,6],1)

outputs:

Int64[3]
5
9
13
3
  • Is there a parallel equivalent to broadcast, as pmap() is to map() ? Aug 10, 2016 at 16:43
  • There can be if you let the developers know that you want one here. There are some implementation details that would need to get worked out (map doesn't need to share data between the independent runs, whereas broadcast needs to share the small vector in some sense), but if my proposal gets enough backing, there should be a pbroadcast(), and also things like tbroadcast() for threading. See the issue. Aug 10, 2016 at 16:47
  • Awesome. This would be great. I'll put my plug in. Aug 10, 2016 at 16:50
2

You can use map(x->f(x,1), [2,4,6]) to accomplish this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.