有区别的联盟初始化 [英] Discriminated Union initialization
问题描述
是否有更简单的方法使用F#来初始化这副牌?
Is there a simpler way to initialize this deck of cards using F#?
let create() = [ 0..12 ]
type Suit =
| Spades of list<int>
| Hearts of list<int>
| Clubs of list<int>
| Diamonds of list<int>
let spades = create() |> Spades
let hearts = create() |> Hearts
let clubs = create() |> Clubs
let diamonds = create() |> Diamonds
具体地说,我想简化这四套衣服的初始化. 有没有更简单的方法可以做到这一点?
Specifically, I would like to simplify the initialization of these four suits. Is there a simpler way to do this?
我可以枚举此已区分联合的类型,并将其分配给某些平台"结构吗?
Could I enumerate the types of on this discriminated union and assign it to some "deck" structure?
注意:
我是F#的新手.因此,如果这个问题看起来很愚昧,请原谅我.
I am new to F#. So please forgive me if this question seems ignorant.
推荐答案
我认为没有更简洁的方法,但是我也不会像这样对纸牌建模,因为它无济于事使非法国家无法代表.例如,我可以这样做:
I don't think that there's a more succinct way, but I also wouldn't model playing cards like this, because it doesn't help making illegal states unrepresentable. As an example, I can do this:
> Spades [-1; 10; 100];;
val it : Suit = Spades [-1; 10; 100]
那甚至意味着什么?
相反,我可能会像这样对卡片进行建模:
Instead, I'd probably model cards like this:
type Suit = Diamonds | Hearts | Clubs | Spades
type Face =
| Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten
| Jack | Queen | King | Ace
type Card = { Suit: Suit; Face: Face }
这将使您能够表达任何有效卡:
This would enable you to express any valid card:
> { Suit = Spades; Face = Ace };;
val it : Card = {Suit = Spades;
Face = Ace;}
另一方面,您不能表达invalid
卡:
On the other hand, you can't express invalid
cards:
> { Suit = Spades; Face = Hundred };;
{ Suit = Spades; Face = Hundred };;
------------------------^^^^^^^
stdin(4,25): error FS0039: The value or constructor 'Hundred' is not defined
在此模型中,初始化看起来有点麻烦,但另一方面,以下deck
是不可变的 value ,因此一旦定义,就可以一次又一次地重用平台
Initialisation looks, in this model, a bit cumbersome, but on the other hand, the following deck
is an immutable value, so once it's defined, you can reuse the deck again and again.
let deck = [
{ Suit = Diamonds; Face = Two };
{ Suit = Diamonds; Face = Three };
{ Suit = Diamonds; Face = Four };
{ Suit = Diamonds; Face = Five };
{ Suit = Diamonds; Face = Six };
{ Suit = Diamonds; Face = Seven };
{ Suit = Diamonds; Face = Eight };
{ Suit = Diamonds; Face = Nine };
{ Suit = Diamonds; Face = Ten };
{ Suit = Diamonds; Face = Jack };
{ Suit = Diamonds; Face = Queen };
{ Suit = Diamonds; Face = King };
{ Suit = Diamonds; Face = Ace };
{ Suit = Hearts; Face = Two };
{ Suit = Hearts; Face = Three };
{ Suit = Hearts; Face = Four };
{ Suit = Hearts; Face = Five };
{ Suit = Hearts; Face = Six };
{ Suit = Hearts; Face = Seven };
{ Suit = Hearts; Face = Eight };
{ Suit = Hearts; Face = Nine };
{ Suit = Hearts; Face = Ten };
{ Suit = Hearts; Face = Jack };
{ Suit = Hearts; Face = Queen };
{ Suit = Hearts; Face = King };
{ Suit = Hearts; Face = Ace };
{ Suit = Clubs; Face = Two };
{ Suit = Clubs; Face = Three };
{ Suit = Clubs; Face = Four };
{ Suit = Clubs; Face = Five };
{ Suit = Clubs; Face = Six };
{ Suit = Clubs; Face = Seven };
{ Suit = Clubs; Face = Eight };
{ Suit = Clubs; Face = Nine };
{ Suit = Clubs; Face = Ten };
{ Suit = Clubs; Face = Jack };
{ Suit = Clubs; Face = Queen };
{ Suit = Clubs; Face = King };
{ Suit = Clubs; Face = Ace };
{ Suit = Spades; Face = Two };
{ Suit = Spades; Face = Three };
{ Suit = Spades; Face = Four };
{ Suit = Spades; Face = Five };
{ Suit = Spades; Face = Six };
{ Suit = Spades; Face = Seven };
{ Suit = Spades; Face = Eight };
{ Suit = Spades; Face = Nine };
{ Suit = Spades; Face = Ten };
{ Suit = Spades; Face = Jack };
{ Suit = Spades; Face = Queen };
{ Suit = Spades; Face = King };
{ Suit = Spades; Face = Ace }; ]
尽管这看起来很乏味,但您只需编写一次即可.由于deck
是一个值,因此可以将其设为属于模块的值.客户可以简单地使用此值,而不必自己初始化卡座.
Although this looks tedious, you only have to write this once. Since deck
is a value, you can make it a value that belongs to a module. Clients can simply use this value, instead of having to initialise a deck by themselves.
这篇关于有区别的联盟初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!