FSharp功能组合 [英] FSharp Functional Composition

查看:70
本文介绍了FSharp功能组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个这样的功能:

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

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

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

我想将这些函数链接在一起,以便执行所有三个函数的结果是每个返回值的数组,有点像 Seq.Collect arrayOfFunctions

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

有没有办法声明性地做到这一点?如果我将functionB的参数从int更改为float,答案是否会改变?

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

谢谢

推荐答案

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

如何申请:

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

这篇关于FSharp功能组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆