FSharp Functional Composition

故事扮演 提交于 2019-12-24 11:51:25

问题


I have three functions like this:

let functionA (i:int) =
    "functionA" + string i

let functionB (i:int) =
    "functionB" + string i

let functionC (i:int) =
    "functionC" + string i

I want to chain these functions together such that the result of executing all three is an array of each of the return values, kind of like Seq.Collect arrayOfFunctions

Is there a way to do this declaratively? If I change functionB's parameter from an int to a float, does the answer change?

Thanks


回答1:


let farr = [| functionA; functionB; functionC |] 
let applyfarr farr i = Array.map (fun f -> f i) farr  

How to apply:

applyfarr farr 2 
> val it : string [] = [|"functionA2"; "functionB2"; "functionC2"|]


来源:https://stackoverflow.com/questions/28236710/fsharp-functional-composition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!