discriminated-union

How to enumerate a discriminated union in F#?

夙愿已清 提交于 2019-12-17 18:13:43
问题 How can I enumerate through the possible "values" of a discriminated union in F#? I want to know if is there something like Enum.GetValues(Type) for discriminated unions, tough I am not sure over what kind of data I would enumerate. I would like to generate a list or array of a discriminated union with one item for each option. 回答1: Yes, F# has it's own reflection layer build on top of .NET's reflection to help you make sense of types that are specific to F#, like discriminating unions. Here

In F#, How can I attach metadata to discriminated union values?

自闭症网瘾萝莉.ら 提交于 2019-12-13 18:44:38
问题 I want to create something that's kind of like an enum with an F# record type for a value instead of an int. For example, if I've got the union: type BologneseIngredients = | Spaghetti | Tomatoes | MincedBeef | GrandmasSecretIngredient I know that spaghetti is always 30cm long and tomatoes are always red. What I could do is have a 'get metadata' function: let getMetadata = function | Spaghetti -> { length: 30.0<cm> } | Tomatoes -> { colour: Color.Red } | _ -> { } but I'd really like to keep

C# types for empty F# discriminated union cases

删除回忆录丶 提交于 2019-12-13 14:27:11
问题 I'm accessing an F# discriminated union using C# and trying to use a switch statement over the union's cases. This works fine for values that have at least one field but not for empty values as these don't have a corresponding class generated, only a property. Consider the following F# discriminated union. type Letter = A of value:int | B of value:string | C | D In C# I have the following switch statement inside a function that has an argument letter of type Letter: switch (letter) { case A a

Print case-identifier of discriminated union

陌路散爱 提交于 2019-12-12 21:06:34
问题 I have an Option type: type Option<'a> = | Some of 'a | None override x.ToString() = sprintf "%A" x printf "%A" None // "None" printf "%A" (Some 1) // "Some 1" Supposedly, in a function I want to print Some 1 , but in another function I want to print its case-identifier, i.e. Some (discard the " 1" value). How can I do it? 回答1: If you want a generic way of doing it rather than implement a member for each of your types, you can use reflection for that: open Microsoft.FSharp.Reflection let

Operating on an F# List of Union Types

孤街醉人 提交于 2019-12-12 11:34:17
问题 This is a continuation of my question at F# List of Union Types. Thanks to the helpful feedback, I was able to create a list of Report s, with Report being either Detail or Summary . Here's the data definition once more: module Data type Section = { Header: string; Lines: string list; Total: string } type Detail = { State: string; Divisions: string list; Sections: Section list } type Summary = { State: string; Office: string; Sections: Section list } type Report = Detail of Detail | Summary

Discriminated union structural/custom equality

点点圈 提交于 2019-12-12 10:01:01
问题 I have the following discriminated union: type ActCard = Cellar of card list | Chapel of (card option * card option* card option* card option) | Smithy | Spy of (card -> bool * card -> bool) It had structural equality until I added the card -> bool to Spy . This question is helpful for how to do custom equality for records. However, I'm not sure how best to implement it in this situation. I would prefer to not have to enumerate each case in ActCard : override x.Equals(yobj) = match x, yobj

How to get the type of each union case for a given union type in F#

僤鯓⒐⒋嵵緔 提交于 2019-12-11 07:46:54
问题 I am wondering in the F# code below how to fetch the type associated with each union case via reflection type AccountCreatedArgs = { Owner: string AccountId: Guid CreatedAt: DateTimeOffset StartingBalance: decimal } type Transaction = { To: Guid From: Guid Description: string Time: DateTimeOffset Amount: decimal } type AccountEvents = | AccountCreated of AccountCreatedArgs | AccountCredited of Transaction | AccountDebited of Transaction I tried using FSharpType.GetUnionCases(typeof

F#: type matching on DU cases, make this slightly more generic

蓝咒 提交于 2019-12-11 06:55:01
问题 In this previous question, there is a lovely solution to asking if an object is a particular union case: let isUnionCase (c : Expr<_ -> 'T>) = match c with | Lambdas (_, NewUnionCase(uci, _)) -> let tagReader = Microsoft.FSharp.Reflection.FSharpValue.PreComputeUnionTagReader(uci.DeclaringType) fun (v : 'T) -> (tagReader v) = uci.Tag | _ -> failwith "Invalid expression" which is great. If I have: type Dog = | Spaniel | Shepherd type Cat = | Tabby | Manx type Animal | Dog of Dog | Cat of Cat I

Applying recursion on 1d-list with parentheses

怎甘沉沦 提交于 2019-12-11 03:51:37
问题 I am kinda new to F# and I'm trying to write a simple program that reads a mathematical expression and calculates it. I was successfully able to calculate expressions like: "5+3 *3 - 1/2" , "10 + 50 /50" etc. As this is being fairly simple and done by straightforward recursion, I wanted to take it to the next level using other mathematical functions (cos,sin etc.) but . . . I stumbled upon parentheses as i can't get an idea of how to recurse on patterns such as "((5+3) * 5) - (4-5)" because

C++11 class with union of string and shared_ptr

天大地大妈咪最大 提交于 2019-12-11 02:52:17
问题 (this is a bit similar to this question, and inspired by the C++11 FAQ on union but not exactly so...) In the context of coding a Scheme-like interpreter in idiomatic C++11 Let's suppose I want a tagged union of a string, an int, and some closure. So I would probably code: #include <string> #include <new> #include <memory> #include <functional> enum kind { nothing, string, integer, closure }; class Value { enum kind k; typedef std::string string_t; union { std::string str; int num; std: