# Is there diff() method for other operations?

**URL:** <https://discourse.julialang.org/t/is-there-diff-method-for-other-operations/20527>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 7, 2019, 12:45am UTC](https://discourse.julialang.org/t/is-there-diff-method-for-other-operations/20527 "2019-02-07T00:45:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [February 7, 2019, 12:45am UTC](https://discourse.julialang.org/t/is-there-diff-method-for-other-operations/20527/1 "2019-02-07T00:45:27Z")

</div>

For example:

```julia
a = [2 , 4 , 8 , 16 , 32]

diff(a) 
=> [2 , 4 , 8 , 16]

```

How do you do this for other operations? This might include something like:

```julia
a = [2 , 4 , 8 , 16 , 32]

average(a)
=> [3 , 6 , 12 , 24]

```

// for vectors this always reduces the length by one

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [February 7, 2019, 2:38am UTC](https://discourse.julialang.org/t/is-there-diff-method-for-other-operations/20527/2 "2019-02-07T02:38:20Z")

</div>

To evaluate `f(x,y)` for each two consecutive elements of `a` you can do

```julia
f.(@view(a[1:end-1]), @view(a[2:end]))

```

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [February 7, 2019, 3:35am UTC](https://discourse.julialang.org/t/is-there-diff-method-for-other-operations/20527/3 "2019-02-07T03:35:53Z")

</div>

Alternatively, you could define a generic `consecutive` function

```julia
function consecutive(f, A::AbstractVector)
    [f(A[i+1], A[i]) for i = 1:length(A)-1 ]
end

a = [2 , 4 , 8 , 16 , 32]
consecutive(-, a) # diff
consecutive(+, a)
consecutive(max, a)
consecutive((x,y)->(x+y)/2, a) #average

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 7, 2019, 4:28am UTC](https://discourse.julialang.org/t/is-there-diff-method-for-other-operations/20527/4 "2019-02-07T04:28:14Z")

</div>

We’ve talked about this some on the GitHub issue tracker: check out [naming: setdiff, diff, gradient · Issue #26361 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/26361). There wasn’t enough of a consensus or drive to actually implement any of the proposed options.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [February 7, 2019, 7:40am UTC](https://discourse.julialang.org/t/is-there-diff-method-for-other-operations/20527/5 "2019-02-07T07:40:29Z")

</div>

This package was created to solve that, and similar, problems

> **[GitHub - JeffreySarnoff/RollingFunctions.jl: Roll a window over data; apply a...](https://github.com/JeffreySarnoff/RollingFunctions.jl)**
>
> Roll a window over data; apply a function over the window. - GitHub - JeffreySarnoff/RollingFunctions.jl: Roll a window over data; apply a function over the window.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 7, 2019, 8:21am UTC](https://discourse.julialang.org/t/is-there-diff-method-for-other-operations/20527/6 "2019-02-07T08:21:29Z")

</div>

```julia
using IterTools, Statistics
a = 2 .^ (1:5)
map(xs -> middle(xs...), partition(a, 2, 1))

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 7, 2019, 12:41pm UTC](https://discourse.julialang.org/t/is-there-diff-method-for-other-operations/20527/7 "2019-02-07T12:41:22Z")

</div>

> [@mbauman](#):
>
> There wasn’t enough of a consensus or drive to actually implement any of the proposed options.

Note that the impetus for having such a function “built-in” in Julia is pretty low because writing an explicit comprehension or loop is easy and fast, and the impulse to use a `consecutive(+, a)` largely arises in Matlab-style vectorized code that is unnecessary (and usually awkward and suboptimal) in Julia.

Another generalization of this concept would be a convolution of a long sequence with a short sequence (in this case convolving `A` with `[1,1]`, while `diff` corresponds to convolving with `[1,-1]`). This is nontrivial to implement extremely efficient code for in the general case of a length-`m` filter, so a mature library is helpful. Packages for this sort of thing include `LocalFilters.jl` and `ImageFiltering.jl`.
