How to calculate the "sliding sliced" fold of an array of int arrays in F#? -


i have function called calcarr_arrofarr in f# signature int [] -> int [][] -> int, i.e. calcarr_arrofarr takes 2 arguments, int array , array of int arrays, , returns int.

i want create function calcarrofarr signature int [][] -> int, following:

let calcarrofarr (arrofarr : int [][]) =     array.fold (fun acc e -> acc + (calcarr_arrofarr e arrofarr.[?..])) 0 arrofarr 

where ? index of e + 1.
in other words, in calcarrofarr want apply calcarr_arrofarr every element e of arrofarr plus "remaining portion" of arrofarr, i.e. slice of arrofarr starting after element e. of course, last element of arrofarr, nothing added accumulator, nor exception thrown.
there way create calcarrofarr in functional way? array.foldi function come handy...

if feel need array.foldi, write one! following snippet extend built-in array module foldi:

module array =     let foldi f z =          |> array.fold (fun (i,a) x -> i+1, f x) (0,z) |> snd 

slicing past-the-end gives empty array (i.e., [|0;1|].[2..] = [||]), original suggestion works:

let calcarrofarr (arrofarr : int [][]) =      array.foldi (fun acc e -> acc + (calcarr_arrofarr e arrofarr.[i+1..])) 0 arrofarr  

however, slice arrofarr.[i+1..] copies array slice; might unfortunate efficiency.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -