6 people like it.
Like the snippet!
Fix the parameter order of Array.foldBack
The built in foldBack has parameters in an odd order: you can't pipe the array into it, and the folder signature is different from Array.fold's. Here's how to fix it.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
|
let letters = [|"a"; "b"; "c"; "d"|]
let forward =
letters
|> Array.fold (fun acc elem -> acc + elem) "->"
// "->abcd"
// The built in foldBack has parameters in an odd order:
// you can't pipe the array into it, and the folder
// signature is different from Array.fold's:
let nasty =
Array.foldBack (fun elem acc -> acc + elem) letters "->"
// "->dcba"
// Let's fix that:
module Array =
let foldBack' folder state array =
let folder' x y = folder y x
Array.foldBack folder' array state
// Now a foldBack call looks like a fold call but
// still goes backwards through the array:
let backward =
letters
|> Array.foldBack' (fun acc elem -> acc + elem) "->"
// "->dcba"
|
val letters : string []
Full name: Script.letters
val forward : string
Full name: Script.forward
module Array
from Microsoft.FSharp.Collections
val fold : folder:('State -> 'T -> 'State) -> state:'State -> array:'T [] -> 'State
Full name: Microsoft.FSharp.Collections.Array.fold
val acc : string
val elem : string
val nasty : string
Full name: Script.nasty
val foldBack : folder:('T -> 'State -> 'State) -> array:'T [] -> state:'State -> 'State
Full name: Microsoft.FSharp.Collections.Array.foldBack
val foldBack' : folder:('a -> 'b -> 'a) -> state:'a -> array:'b [] -> 'a
Full name: Script.Array.foldBack'
val folder : ('a -> 'b -> 'a)
val state : 'a
Multiple items
val array : 'b []
--------------------
type 'T array = 'T []
Full name: Microsoft.FSharp.Core.array<_>
val folder' : ('b -> 'a -> 'a)
val x : 'b
val y : 'a
val backward : string
Full name: Script.backward
Multiple items
module Array
from Script
--------------------
module Array
from Microsoft.FSharp.Collections
More information