[julia-users] pair-wise operation on an array?

2015-02-07 Thread Evan Pu
say I want to compute a pair-wise diff for all the elements in the array.
input:[1, 2, 4, 7, 8]
output:[1, 2, 3, 1]

is there some kind of "beautiful" way of doing it? i.e. w/o using a for 
loop nor using explicit indecies


Re: [julia-users] pair-wise operation on an array?

2015-02-07 Thread Tamas Papp
diff([1, 2, 4, 7, 8])

I don't think it gets more beautiful than this :D

Best,

Tamas

On Sat, Feb 07 2015, Evan Pu  wrote:

> say I want to compute a pair-wise diff for all the elements in the array.
> input:[1, 2, 4, 7, 8]
> output:[1, 2, 3, 1]
>
> is there some kind of "beautiful" way of doing it? i.e. w/o using a for
> loop nor using explicit indecies


Re: [julia-users] pair-wise operation on an array?

2015-02-07 Thread Evan Pu
i see, maybe my example is too special.
What i meant is I have an array of elements and I have a binary operator 
which I want to apply to every adjacent elements.

On Saturday, February 7, 2015 at 7:26:55 AM UTC-5, Tamas Papp wrote:
>
> diff([1, 2, 4, 7, 8]) 
>
> I don't think it gets more beautiful than this :D 
>
> Best, 
>
> Tamas 
>
> On Sat, Feb 07 2015, Evan Pu > wrote: 
>
> > say I want to compute a pair-wise diff for all the elements in the 
> array. 
> > input:[1, 2, 4, 7, 8] 
> > output:[1, 2, 3, 1] 
> > 
> > is there some kind of "beautiful" way of doing it? i.e. w/o using a for 
> > loop nor using explicit indecies 
>


Re: [julia-users] pair-wise operation on an array?

2015-02-07 Thread Steven G. Johnson


On Saturday, February 7, 2015 at 6:45:40 AM UTC-8, Evan Pu wrote:
>
> i see, maybe my example is too special.
> What i meant is I have an array of elements and I have a binary operator 
> which I want to apply to every adjacent elements.
>

No, I think you have to use a loop or comprehension, e.g. [op(a[i],a[i+1]) 
for i in 1:length(a)-1].   This is pretty compact and efficient, so I doubt 
it's worth trying to condense further.