If I want to move away from imperative programming language, let us say it is C#, I have to find the equivalence in F#. I begin with getting rid of For-loop.
I already know I can use Seq.sum, Seq.map, etc to replace most of the for-loop in my program. How about I need apply transform functions to a single data? Can I chain these functions?
Fortunately the answer is yes. Let us say you have a list of functions f0, f1...fn to apply to a data. In a good math format: f0(f1(...fn(data))). I take functions as an input parameter and compositeFunction will give me the f0(f1..fn())).
let compositeFunctions functions = Seq.fold (>>) (fun c->c) functions
you chain the functions by >> and (fun c->c) as starting point.
The data will go into (fun c->c) first, which does nothing. Then the data go through each function in the function list.
No comments:
Post a Comment