开发者

Tupled function composition

开发者 https://www.devze.com 2023-04-04 04:29 出处:网络
I\'m curious why this let f开发者_开发技巧 = (fun a b -> a, b) >> obj.Equals gives the error

I'm curious why this

let f开发者_开发技巧 = (fun a b -> a, b) >> obj.Equals

gives the error

No accessible member or object constructor named 'Equals' takes 1 arguments

but this works

let f = (fun a -> a, a) >> obj.Equals


Without defining a new combinator operator:

let f = (fun a b -> a, b) >> (<<) obj.Equals

>> (<<) is a nice trick, and can also be extended for more arguments:

let compose3 f g = f >> (<<) ((<<) g)
val compose3 : ('a -> 'b -> 'c -> 'd) -> ('d -> 'e) -> ('a -> 'b -> 'c -> 'e)


Consider the types. (>>) has type ('a -> 'b) ->('b -> 'c) -> ('a -> 'c), but you're trying to call it with arguments of type 'a -> ('b -> 'a*'b) and obj * obj -> bool, which can't be made to fit together like that.

You could of course define a new combinator for composing binary and unary functions:

let ( >>* ) f g a b = f a b |> g

in which case you can use it in your example instead of (>>).

0

精彩评论

暂无评论...
验证码 换一张
取 消