Как можно оптимизировать по времени работы:
let c = [1..1000]
let eachWithEachAndCoollect f l =
let _, result = l |> List.fold
(fun (acc, result) el ->
let extended =
acc |> List.fold
(fun acc2 ael ->
match f el ael with
| Some(t) -> t :: acc2
| None -> acc2)
[]
(el :: acc), result @ extended)
([],[])
result
let eachWithEachAndCoollect2 f l =
let rec iter l acc =
match l with
| h :: t ->
iter t acc @ (t |> List.choose (fun el -> f h el))
| [] -> acc
iter l []
c |> eachWithEachAndCoollect (fun a b -> Some(a,b))
(*Real: 00:00:33.223, CPU: 00:00:31.109, GC gen0: 470, gen1: 233, gen2: 28*)
c |> eachWithEachAndCoollect2 (fun a b -> Some(a,b))
(*Real: 00:00:32.130, CPU: 00:00:32.015, GC gen0: 482, gen1: 288, gen2: 20*)