这是内联魔法。
让我们采用 kvb 的代码并定义一个gmap
处理所有情况的函数:
let inline gmap f (x, y) = f $ x, f $ y
type One = One with static member ($) (One, x) = 1 // Example1 ConvertAll
type Id = Id with static member ($) (Id , x) = x // Example2 PassThrough
type SeqSingleton = SeqSingleton with static member ($) (SeqSingleton , x) = seq [x]
type ListSingleton = ListSingleton with static member ($) (ListSingleton, x) = [x]
type ListHead = ListHead with static member ($) (ListHead, x) = List.head x
// Usage
let pair1 = gmap One ("test", true)
let pair2 = gmap Id ("test", true)
let pair3 = gmap SeqSingleton ("test", true)
let pair4 = gmap ListSingleton ("test", true)
let pair5 = gmap ListHead (["test";"test2"], [true;false])
let pair6 = ("test", true) |> gmap ListSingleton |> gmap ListHead
(* results
val pair1 : int * int = (1, 1)
val pair2 : string * bool = ("test", true)
val pair3 : seq<string> * seq<bool> = (["test"], [true])
val pair4 : string list * bool list = (["test"], [true])
val pair5 : string * bool = ("test", true)
val pair6 : string * bool = ("test", true)
*)
UPDATE
也可以使用更通用的gmap
函数定义here https://gist.github.com/gusty/0cc6d0c379d3845f0e334a84e3b13a37那么它也适用于 n 组 (n