請考慮此數據集:子集JSON數據
type Deck = JsonProvider<"...">
let dt = Deck.GetSamples()
dt
[{"collectible":true,"health":4,"artist":"Zoltan Boros","type":"MINION","cost":1,"attack":2},
{"collectible":true,"health":8,"artist":"James Ryman","type":"MINION","cost":8,"attack":8},
{"collectible":true,"health":3,"artist":"Warren Mahy", "type":"LAND","cost":2,"attack":2}]
我試圖建立能夠提取它的某些信息的功能,並最終將它們存儲在一個較小的數據集。它應該,考慮到類似列表的數據集deck
,只考慮對於key
s等於value
s的卡。
let rec filter deck key value =
let rec aux l1 l2 l3 =
match l1 with
[] -> []
| x::xs when x.l2 = l3 -> x::(aux xs key value)
aux deck key value
例如,
filter dt type minion
應子集的甲板中較小的一個只與第一和第二卡。我覺得我做向前走了幾步中獲得的概念,但它仍然無法正常工作,扔我應該如何定義key
類型的那種
FS0072: Lookup on object of indeterminate type based on information prior to
this program point. A type annotation may be needed prior to this program point to
constrain the type of the object. This may allow the lookup to be resolved.
錯誤?我試着用key : string
和key : string list
,但沒有成功。
爲什麼?我不認爲你可以將代碼解釋爲一個沒有''''操作符的引號的字符串,但是無論如何,它首先會違背類型提供者的觀點。使用過濾器函數有什麼問題,比如'fun(c:Deck.Root) - > c.Type =「MINION」'? – Vandroiy
@Vandroiy,過濾函數沒有錯。我根本不知道他們。感謝您向我展示它。 – Worice
@Worice在任何語言中,它都有助於熟悉它的集合和對它們進行操作的函數(在功能語言中可能更多)。這是一個很好的概述:[集合和函數](http://fsharpforfunandprofit.com/posts/list-module-functions/)。 – s952163