相关疑难解决方法(0)

从工会案例中提取价值

在Fsharp应用程序中,我已将几个联合案例类型定义为

type A = A of String
type B = B of String
type C = C of String
Run Code Online (Sandbox Code Playgroud)

我想定义一个函数来从union case实例中提取值.

let getValue( ctor: String-> 'a) = 
   ...implementation here
Run Code Online (Sandbox Code Playgroud)

反正有没有完成这样的任务?谢谢.

f#

7
推荐指数
1
解决办法
2068
查看次数

F#比较区分联合的案例标识符

有没有办法通过F#中的案例标识符比较受歧视的联盟?

type MyUnion =
| MyString of string
| MyInt of int

let x = MyString("hello")
let y = MyString("bye")
let z = MyInt(25)

let compareCases a b =
// compareCases x y = true
// compareCases x z = false
// compareCases y z = false
Run Code Online (Sandbox Code Playgroud)

如何compareCases以通用方式实现功能?

即如下所示,但更通用(反射是可以的):

let compareCases a b =
  match a with
  | MyString(_) -> match b with | MyString(_) -> true | _ -> false
  | MyInt(_) -> match b with | …
Run Code Online (Sandbox Code Playgroud)

.net reflection f# discriminated-union

4
推荐指数
1
解决办法
415
查看次数

匹配区别联合时的通配符

在下面的真实世界的例子中我做了一个匹配:

type Style = Nice | Cool | Ugly
type Color = Blue | Yellow | Orange | Grey | Cyan
type ClothingProperties = Style * Color

type Clothes =
| Jeans of ClothingProperties
| Pullover of ClothingProperties
| Shirt of ClothingProperties

type Person =
| Person of string * Clothes

let team = [Person("Jan", Jeans (Cool, Blue)); Person("Pete", Shirt (Nice, Cyan)); Person("Harry", Pullover (Ugly, Grey))]

let matchPerson person=
    match person with
    | Person(name,  Jeans(Ugly,_) ) -> printfn "%s wears …
Run Code Online (Sandbox Code Playgroud)

f# functional-programming pattern-matching discriminated-union

3
推荐指数
1
解决办法
242
查看次数