我有一个复杂的示例,但是我使用简单的对象简化了案例。
给定2个功能:
let add (a:int) b = a + b
let aee (a:int[]) b = a.[0] + b
Run Code Online (Sandbox Code Playgroud)
它们可以按以下方式使用:
let c = add 1 5
let a = [|1; 2|]
let d = aee a 5
Run Code Online (Sandbox Code Playgroud)
所以,为什么这是有效的:
let c = 1 |> add 5
Run Code Online (Sandbox Code Playgroud)
这不是吗?
let d = [|1;2|] |> aee 5 // Type mismatch, expecting int[] -> `a but given int -> int
Run Code Online (Sandbox Code Playgroud)
如果很重要,我的实际情况如下:
open FSUnit
type Tick (symbol:string, ask:decimal, bid:decimal) =
member __.Symbol = symbol
member __.Ask = ask
member __.Bid = bid
let containSymbol (ticks:Tick[]) symbol =
( ticks |> Array.tryFind (fun t -> t.Symbol = symbol) ).IsSome
let ticks = [| Tick("aaa", 1m, 2m) |]
// I'm able to do these
should be True <| containSymbol ticks "aaa"
containSymbol ticks "aaa" |> should be True
// but not this
ticks |> containSymbol CurrencyPair.XRP_BTC |> should be True // does not work as described in the small example
// or (desiderable) this
ticks |> should contain (fun t -> t.symbol = "aaa") // don't know how to create/pass a ContainwsConstraint with a function
Run Code Online (Sandbox Code Playgroud)
是的,最终目标是能够使用,ticks |> should contain (fun t -> t.symbol = "aaa")
但我正在迈出第一步。
管道运算符使您可以将参数传递给可能已被部分评估的函数,也可以不将其部分传递给函数。将被传递的值作为该函数的下一个参数传递。
在此示例中:
let d = [|1;2|] |> aee 5
Run Code Online (Sandbox Code Playgroud)
您正在aee
使用参数一部分进行求值5
,然后[|1;2|]
使用管道运算符将其传递给参数,但这不起作用,因为的第一个参数aee
是数组,而不是整数。
为了使它按您尝试的方式工作,您需要更改参数的顺序:
let aee b (a:int[]) = a.[0] + b
let d = [|1;2|] |> aee 5
Run Code Online (Sandbox Code Playgroud)