How to determine the correct data structure for a parameter set

I need a data structure for the following:

In a device having memory slots, each of the slots has a set of parameters. These parameters are of different types. The list of possible parameters is fixed, so there is no need for general flexibility à la "Support for arbitrary parameters without changes". In addition, a content structure is known for each parameter. Typical examples of use are the extraction and modification of one particular parameter, as well as the conversion of a complete set of parameters to another (but already defined) data structure.

A natural choice for an F # data structure would be a type of sum like this:

type SomeParameterContentType = { Field1 : string, Field2 : int } type SomeOtherParameterContentType = { Other1 : bool option, Other2 : double } type Parameter = | SomeParameter of SomeParameterContentType | SomeOtherParameter of SomeOtherParameterContentType 

Thus, I could create a set and save the parameters there with a very good data structure. The question here is: given this idea, what would a particular parameter look like? I do not know how to specify a predicate for the find function for sets. One could define another type of sum, listing only the types of parameters without their contents, using this as a key for the Dictionary, but I do not really like this idea. Using strings instead of the second type of sum does not improve the quality, since this requires a further two times to provide a list of possible parameters.

Does anyone have a better idea?

thanks --Mathias.

+5
source share
1 answer

It looks like all you need is tryFind for Set:

 module Set = let tryFind p = Set.toList >> List.tryFind p 

Using:

 let mySet = Set.ofList [1;2;3;4;5] let m = mySet |> Set.tryFind (fun t -> t = 2) 

val m: int option = Some 2

Use with your types:

 let yourSet = Set.ofList [SomeParameter {Field1="hello";Field2=3}] let mYours = yourSet |> Set.tryFind (fun t -> match t with |SomeParameter p -> true |SomeOtherParameter p -> false) 

val mYours: Parameter option = Some (SomeParameter {Field1 = "hello"; Field2 = 3;})

0
source

Source: https://habr.com/ru/post/1216261/


All Articles