请考虑此数据集:
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
应将甲板分成较小的甲板,只有第一张和第二张卡。我认为我在获得这个概念方面做了很少的步骤,但它仍然没有用,抛出了一种错误
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
的类型?我尝试使用key : string
和key : string list
,但没有成功。
答案 0 :(得分:2)
您是否尝试重新实施filter
?
#if INTERACTIVE
#r @"..\packages\FSharp.Data\lib\net40\FSharp.Data.dll"
#endif
open FSharp.Data
[<Literal>]
let jsonFile = @"C:\tmp\test.json"
type Json = JsonProvider<jsonFile>
let deck = Json.Load(jsonFile)
deck |> Seq.filter (fun c -> c.Type = "MINION")
给我:
val it:seq.Root&gt; = seq
[{“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}]
答案 1 :(得分:0)
您实际上需要注释l1的类型。
设置l1: something list
应该是您想要的。
Key并不提供帮助,因为类型推断是从上到下,x.l2
是在aux
被调用key
作为参数之前