Created
April 7, 2019 14:08
-
-
Save matsueushi/658921bbeb21c47ebb6ed028041e7e41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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