Skip to content

Instantly share code, notes, and snippets.

@matsueushi
Created April 7, 2019 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matsueushi/658921bbeb21c47ebb6ed028041e7e41 to your computer and use it in GitHub Desktop.
Save matsueushi/658921bbeb21c47ebb6ed028041e7e41 to your computer and use it in GitHub Desktop.
function rolling(fun::Function, x::AbstractArray{T}, window::Int) where {T <: Real}
[fun(view(x, i:i + window - 1)) for i = 1:length(x) - window + 1]
end
println(rolling(sum, [1, 2, 3, 4, 5], 2)) # [3, 5, 7, 9]
println(rolling(sum, [1, 2, 3, 4, 5], 3)) # [6, 9, 12]
println(rolling(prod, [1, 2, 3, 4, 5], 3)) # [6, 24, 60]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment