I have a problem, where I have a record (called Probablities in my case) that only consists of a certain type, and I want to create array with the record values.
type Probability = Probability of float
type Probabilities = {
Pp: Probability;
Pf: Probability;
Pd: Probability;
Pg: Probability;
}
let calculatingSum (probablities : Probabilities) =
probablities
// I need something, that can transform probablities to an array of Probability
// such that I can do something like
[|1.0;0.5|]
|> Array.sum
I know it is possible to access all the elements individually, but I really wanna avoid that.
asked Sep 3, 2021 at 12:45
2 Answers 2
If I had reason to treat the data both as a record and as a collection, I would make a conversion to a map (and transform that to array or list when needed) Like so:
module Probabilities =
let asMap (p : Probabilities) =
Map.ofList [
"Pp", p.Pp
"Pf", p.Pf
"Pd", p.Pd
"Pg", p.Pg
]
Brian Berns
17.3k2 gold badges33 silver badges45 bronze badges
answered Sep 3, 2021 at 13:54
You could add a method to your Probabilities
type that converts its contents into an array:
type Probabilities = {
Pp: Probability;
Pf: Probability;
Pd: Probability;
Pg: Probability;
}
with
member this.ToArray =
[|
this.Pp
this.Pf
this.Pd
this.Pg
|]
And then use it like this:
let calculatingSum (probablities : Probabilities) =
probablities.ToArray
|> Array.sumBy (fun (Probability p) -> p)
|> Probability
answered Sep 3, 2021 at 14:49
lang-ml
Probabilities
type (say,AsList
that would return a list of probabilities) - not sure this is any better than explicit destructuring, but maybe less verbose. Another thing that comes to mind is reflection (github.com/dotnet/fsharp/blob/main/src/fsharp/FSharp.Core/…), but looks like quite a dirty hack that should be avoided whenever it is possible...