F# Lists except-筛选不等于其他列表的任何项的字符串序列
我试图创建一个过滤器函数,接受两个列表参数,并在排除第二个列表中的现有项(等于F# Lists except-筛选不等于其他列表的任何项的字符串序列,f#,f#-3.0,F#,F# 3.0,我试图创建一个过滤器函数,接受两个列表参数,并在排除第二个列表中的现有项(等于a)后返回第一个序列中的所有项 type R = { A: string; B: int; ...} let filter (xxx: seq<string) (except: list<R>) = xxx |> Seq.filter (fun i -> // returns all the items in xxx which not equal to a
a
)后返回第一个序列中的所有项
type R = { A: string; B: int; ...}
let filter (xxx: seq<string) (except: list<R>) =
xxx
|> Seq.filter (fun i ->
// returns all the items in xxx which not equal to any except.A
type R={A:string;B:int;…}
let过滤器(xxx:seq-seq.filter(乐趣i->
//返回xxx中除.A之外不等于任何值的所有项
我想一件事是
let filter (xxx: seq<string>) (except: list<R>) =
xxx
|> Seq.filter (fun i -> except |> List.exists (fun t -> t.A = i) |> not)
let过滤器(xxx:seq)(除:列表)=
xxx
|>Seq.filter(fun i->except |>List.exists(fun t->t.A=i)|>not)
最简单的代码是:
type R = { A: string; B: int; }
let filter where except =
let except' = except |> List.map (fun x -> x.A) |> Set.ofList
where
|> Seq.filter (not << except'.Contains)
type R={A:string;B:int;}
让我们在除此之外的任何地方过滤=
让except'=except |>List.map(funx->x.A)|>Set.of列表
哪里
|>Seq.filter(非Fluent LINQ实现:
let filter (where: seq<string>) except =
let contains = set (where.Except(List.map (fun x -> x.A) except)) in
where.Where contains.Contains
let过滤器(其中:seq)除=
let contains=set(where.Except(List.map(fun x->x.A)Except))在
where.where contains.contains
现在有序列。除了
:
xs
|> Seq.except ys
// All xs that are not in ys
这是一个集合操作。请参阅。@Daniel-可能不是-如果原始列表包含重复项,则使用集合可能会丢失这些项。在这种情况下,只有第二个列表应视为集合。