1

I have a collection of key/value pairs contained inside an F# Map

type node = {myFloat:float, data1:int; data2:int;}
type nodesCollection = {metaData:int nodes:Map<float,node>}

let results = processData (nodes:Map<float,node>)

The function processData returns a list of tuples with the following signature

val results : (node * int * int * int * int) List

I would like to:

  1. Only work with the first item of the tuple (i.e. node)
  2. Iterate through either the key/value pairs in the Map or the list of nodes
  3. Replace the node values in the Map with those if the list in the key exist in both
  4. If the key doesn't exist in the Map added the node from the list to the Map
  5. Return the updated Map

Ignoring the fact that the returned tuple needs to be parsed, I would do something like this in C# if I was using a Dictionary<float,node>

foreach newNode in nodesInList
{
  if (nodesCollection.nodes.Contains(newNode.myfloat))
     nodesCollection.nodes[newNode.myfloat] = node
  else
     nodesCollection.nodes.Add(newNode.myfloat, node); 
}

return nodesCollection.nodes

How would I approach this using a Map and functional style?

4

1 回答 1

3

折叠是一种以不可变的迭代/递归方式操纵状态的通用技术。如果你想在 C# 中试验它,那就是 Aggregate LINQ 扩展方法。

在 F# Map.add 实际上是添加/替换,因此您不需要根据是否包含键进行分支。

type node = {myFloat:float; data1:int; data2:int;}
type nodesCollection = {metaData:int; nodes:Map<float,node>}

let nodeColl = { metaData = 5; nodes = Map.empty }
let results : (node * int * int * int * int) List = []

let nodes = List.map (fun (n, _, _, _, _) -> n) results

let res = List.fold (fun map e -> Map.add e.myFloat e map) nodeColl.nodes nodes
于 2014-02-01T05:43:43.820 回答