请考虑此数据集:子集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