Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 从字典序列中提取主键数据值_F#_Dictionary_Set_Sequence - Fatal编程技术网

F# 从字典序列中提取主键数据值

F# 从字典序列中提取主键数据值,f#,dictionary,set,sequence,F#,Dictionary,Set,Sequence,我编写了以下代码来提取一系列字典中ID字段的值 并将它们作为一个集合返回-基本上,我要查找数据库表中 我曾经填充过一个字典(每个表行1个dict)。代码下面是一些示例数据 加载到字典序列中的 虽然下面的代码可以工作,但这种风格可能更具功能性——我不得不求助于 使用可变列表构建结果集。所以我觉得这不对 任何人都想在这里提供一个改进的、更实用的解决方案 // Extract the PK values from a dictionary and create a key set from thes

我编写了以下代码来提取一系列字典中ID字段的值 并将它们作为一个集合返回-基本上,我要查找数据库表中 我曾经填充过一个字典(每个表行1个dict)。代码下面是一些示例数据 加载到字典序列中的

虽然下面的代码可以工作,但这种风格可能更具功能性——我不得不求助于 使用可变列表构建结果集。所以我觉得这不对

任何人都想在这里提供一个改进的、更实用的解决方案

// Extract the PK values from a dictionary and create a key set  from these data values
// The expected result here is: set ["PK1"; "PK2"; "PK3"; "PK4"]
let get_keyset (tseq:seq<Dictionary<string,string>>) =  
    let mutable setres =[]  
    for x in tseq do  
        for KeyValue(k,v) in x do  
            // Extract ID values/keys from each dict  
            setres <- x.Item("ID")::setres  
    setres |> List.rev |> Set.ofList   

// Sample Data  
// First Tuple is PK/ID value  
let tabledata = [  
                [("ID", "PK1"); ("a2","aa"); ("a3", "aaa"); ("a4", "aaaa") ]  
                [("ID", "PK2"); ("b2","bb"); ("b3", "bbb"); ("b4", "bbbb") ]  
                [("ID", "PK3"); ("c2","cc"); ("c3", "ccc"); ("c4", "cccc") ]  
                [("ID", "PK4"); ("d2","dd"); ("d3", "ddd"); ("d4", "dddd") ]  
                ]  

//generate dict sequence from datasets  
let gendictseq tabledata =  
    seq {  
        for tl in tabledata do  
            let ddict = new Dictionary<string,string>()    
            for (k,v) in tl do  
                    ddict.Add(k,v)  
            yield ddict  
    }  
//从字典中提取PK值,并从这些数据值创建密钥集
//这里的预期结果是:set[“PK1”;“PK2”;“PK3”;“PK4”]
让我们获取_键集(tseq:seq)=
设可变集合=[]
对于tseq do中的x
对于x do中的键值(k,v)
//从每个dict中提取ID值/键
setres List.rev |>Set.of列表
//样本数据
//第一个元组是PK/ID值
设tabledata=[
[(“ID”、“PK1”);(“a2”、“aa”);(“a3”、“aaa”);(“a4”、“aaaa”)]
[(“ID”,“PK2”);(“b2”,“bb”);(“b3”,“bbb”);(“b4”,“bbbb”)]
[(“ID”、“PK3”);(“c2”、“cc”);(“c3”、“ccc”);(“c4”、“cccc”)]
[(“ID”、“PK4”);(“d2”、“dd”);(“d3”、“ddd”);(“d4”、“dddd”)]
]  
//从数据集生成dict序列
让gendictseq tabledata=
序号{
对于tabledata中的tl,请执行以下操作:
设ddict=newdictionary()
对于tl-do中的(k,v)
D.添加(k,v)
收益率
}  

在C#中使用
ResizeArray
List
)比可变列表变量更好:

let getKeyset (tseq:seq<Dictionary<string,string>>) =  
    let setres = new ResizeArray<string>()
    for x in tseq do 
        for KeyValue(k,v) in x do 
            setres.Add(x.["ID"])
    setres |> Set.ofSeq
让getKeyset(tseq:seq)=
设setres=new ResizeArray()
对于tseq do中的x
对于x do中的键值(k,v)
setres.Add(x.[“ID”])
setres |>Set.ofSeq
或者使用功能更强的序列计算:

let getKeyset2 (tseq:seq<Dictionary<string,string>>) =  
    seq {
        for x in tseq do 
            for KeyValue(k,v) in x do 
                yield x.["ID"]
        }
    |> Set.ofSeq
让getKeyset2(tseq:seq)=
序号{
对于tseq do中的x
对于x do中的键值(k,v)
收益率x.[“ID”]
}
|>方程组

在C#中使用
ResizeArray
List
)比可变列表变量更好:

let getKeyset (tseq:seq<Dictionary<string,string>>) =  
    let setres = new ResizeArray<string>()
    for x in tseq do 
        for KeyValue(k,v) in x do 
            setres.Add(x.["ID"])
    setres |> Set.ofSeq
让getKeyset(tseq:seq)=
设setres=new ResizeArray()
对于tseq do中的x
对于x do中的键值(k,v)
setres.Add(x.[“ID”])
setres |>Set.ofSeq
或者使用功能更强的序列计算:

let getKeyset2 (tseq:seq<Dictionary<string,string>>) =  
    seq {
        for x in tseq do 
            for KeyValue(k,v) in x do 
                yield x.["ID"]
        }
    |> Set.ofSeq
让getKeyset2(tseq:seq)=
序号{
对于tseq do中的x
对于x do中的键值(k,v)
收益率x.[“ID”]
}
|>方程组

从功能上看,此操作如下图所示:

let get_keyset_new (tseq:seq<Dictionary<string,string>>) = 
  let s = tseq |> Seq.map (fun i -> i |> Seq.map (fun e -> i.Item("ID") ) )
  seq {
      for i in s do
          yield! i
  } |> Set.ofSeq
get_keyset_new(tseq:seq)=
设s=tseq |>Seq.map(fun i->i |>Seq.map(fun e->i.Item(“ID”))
序号{
因为我在做什么
屈服!我
}|>方程组

从功能上看,此操作如下图所示:

let get_keyset_new (tseq:seq<Dictionary<string,string>>) = 
  let s = tseq |> Seq.map (fun i -> i |> Seq.map (fun e -> i.Item("ID") ) )
  seq {
      for i in s do
          yield! i
  } |> Set.ofSeq
get_keyset_new(tseq:seq)=
设s=tseq |>Seq.map(fun i->i |>Seq.map(fun e->i.Item(“ID”))
序号{
因为我在做什么
屈服!我
}|>方程组

你的
获取密钥集
在我看来相当复杂。这要简洁得多:

let get_keyset tseq =
    tseq |> Seq.map (fun (x:Dictionary<_,_>) -> x.["ID"]) |> set

你的
get_键集
在我看来很复杂。这要简洁得多:

let get_keyset tseq =
    tseq |> Seq.map (fun (x:Dictionary<_,_>) -> x.["ID"]) |> set

如果使用Set.ofList,为什么需要反转列表?如果使用Set.ofList,为什么需要反转列表?同意我的代码中的卷积-这就是我发布的原因。您的解决方案说明了fp方法的真正威力,在fp方法中,代码可以真正浓缩为几个简洁的表达式。(我还在学习这个东西-你的例子真的很有帮助-Thx)。@BrendanC:另一个有趣的方法是
get\u keyset
可以写:
let get\u keyset:Dictionary seq->Set=seq.map(fun x->x.[ID])>>Set
:-]同意我的代码中的卷积-这就是我发布的原因。您的解决方案说明了fp方法的真正威力,在fp方法中,代码可以真正浓缩为几个简洁的表达式。(我还在学习这些东西-你的例子真的很有帮助-Thx)。@BrendanC:另一个有趣的方法是
get\u keyset
可以编写:
let get\u keyset:Dictionary seq->Set=seq.map(fun x->x.[ID])>>设置
:-]我认为序列表达式的功能更少,更具命令性…我认为序列表达式的功能更少,更具命令性。。。