具有多个输入参数的F#函数组合 [英] F# Function Composition with multiple input parameters

查看:81
本文介绍了具有多个输入参数的F#函数组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是F#的新手,最近发现了函数组合运算符>>

I am new to F# and I recently discovered the function composition operator >>

我了解基本原理,所以类似这样的事情是可能的....

I understand the basic principle so that something like this is possible....

let Add1ToNum x = x +1
let Mul2ToNum y = y * 2
let FuncComp = Add1ToNum >> Mul2ToNum

但是,当您具有多个具有不同数量输入参数的函数时,如何处理构图……例如,我希望能够执行以下操作……

However, how would one handle composition when you have several functions that have a varying number of input parameters... for instance I would like to be able to do the following...

let AddNums (x,y) = x+y
let MulNums (x,y) = x*y
let FuncComp = Add1 >> Mul2

这显然是行不通的,因为AddNums返回一个int,而MulNums正在期待一个元组.

Which obviously doesn't work because AddNums is returning an int, and MulNums is expecting a tuple.

是否有某种形式的语法可以实现此目的?或者,如果我想使用功能组合,是否必须始终执行某种中间函数来转换值?

Is there some form of syntax that allows me to accomplish this, or if I am wanting to use Function Composition do I have to always perform some sort of intermediary function to transform the values?

任何对此的建议将不胜感激.

Anyone suggestions on this would be much appreciated.

推荐答案

正如Yin和codekaizen指出的那样,您无法编写这两个函数来创建一个将输入传递给第一个函数,然后传递此调用的输出的函数.到第二个函数(即使用>>运算符).使用图表,您不能执行以下操作:

As Yin and codekaizen pointed out, you cannot compose the two functions to create a function that passes the input to the first one and then passes the output of this call to the second function (that is, using th >> operator). Using a diagram, you cannot do:

     +---------+    +---------+
 --->| AddNums |--->| MulNums |--->
     +---------+    +---------+

一个选项是更改功能并指定参数之一,以便可以组合功能. codekaizen的示例使用了这一点,也可以这样编写(如果您使用currying而不是tupled参数):

One option is to change the function and specify one of the parameters, so that the functions can be composed. The example by codekaizen uses this and could be also written like this (if you used currying instead of tupled parameters):

let AddNums x y = x + y  
let MulNums x y = x * y  
let FuncComp = (AddNums 1) >> (MulNums 2)

组成函数的另一个选项是创建一个函数,该函数接受多个输入,将两个数字传递给第一个函数,然后使用结果和原始输入的另一个数字调用第二个函数.使用图表:

Another option for composing the functions is to create a function that takes several inputs, passes two numbers to the first function and then calls the second function with the result and another number from the original inputs. Using a diagram:

 -----------------\
 --->+---------+   \+---------+
 --->| AddNums |--->| MulNums |--->
     +---------+    +---------+

如果您需要类似的内容,那么最好的选择是直接编写,因为这可能不是经常重复的模式.直接地,这很容易(使用curried变体):

If you need something like that, then the best option is to write that directly, because this probably won't be a frequently repeated pattern. Directly, this is easy (using the curried variant):

let AddNums x y = x + y  
let MulNums x y = x * y  
let FuncComp x y z = AddNums z y |> (MulNums z)

如果您想更一般地编写这样的内容(或者只是出于好奇),则可以编写这样的内容(这次使用函数的元组版本). &&&运算符的灵感来自箭头:

If you wanted to write something like that more generally (or just for curiosity), you could write something like this (using the tupled version of functions this time). The &&& operator is inspired by Arrows:

let AddNums (x,y) = x + y 
let MulNums (x,y) = x * y  

let (&&&) f g (a, b) = (f a, g b)
let FuncComp = (AddNums &&& id) >> MulNums

// First two numbers are added, result is multiplied by the third one
FuncComp ((9, 12), 2) // Gives '42'

这篇关于具有多个输入参数的F#函数组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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