Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -0,0 +1,3 @@
enum class fruits { apple, banana, cherry };
enum class fruits { apple = 0, banana = 1, cherry = 2 };

View file

@ -0,0 +1 @@
enum class fruits : unsigned int { apple, banana, cherry };

View file

@ -0,0 +1 @@
enum fruits : unsigned int { apple, banana, cherry };

View file

@ -0,0 +1,3 @@
typedef enum { apple, banana, cherry } fruits;
typedef enum { apple = 0, banana = 1, cherry = 2 } fruits;

View file

@ -1,17 +1,50 @@
// Named (commonly used enum in D) (int).
enum Fruits1 { apple, banana, cherry }
// Anonymous, as in C.
enum { APPLE, BANANA, CHERRY }
// Named with specified values (int).
enum Fruits2 { apple = 0, banana = 10, cherry = 20 }
// Named, typed and with specified values.
enum Fruits3 : ubyte { apple = 0, banana = 100, cherry = 200 }
void main() {
// Named enumeration (commonly used enum in D).
// The underlying type is a 32 bit int.
enum Fruits1 { apple, banana, cherry }
// You can assign an enum to the general type, but not the opposite:
int f1 = Fruits1.banana; // No error.
// Fruits1 f2 = 1; // Error: cannot implicitly convert.
// Anonymous enumeration, as in C, of type 32 bit int.
enum { APPLE, BANANA, CHERRY }
static assert(CHERRY == 2);
int f1 = Fruits2.banana; // No error.
// Fruits2 f2 = 1; // Error: cannot implicitly convert.
// Named enumeration with specified values (int).
enum Fruits2 { apple = 0, banana = 10, cherry = 20 }
// Named enumeration, typed and with specified values.
enum Fruits3 : ubyte { apple = 0, banana = 100, cherry = 200 }
// Named enumeration, typed and with partially specified values.
enum Test : ubyte { A = 2, B, C = 3 }
static assert(Test.B == 3); // Uses the next ubyte, duplicated value.
// This raises a compile-time error for overflow.
// enum Fruits5 : ubyte { apple = 254, banana = 255, cherry }
enum Component {
none,
red = 2 ^^ 0,
green = 2 ^^ 1,
blue = 2 ^^ 2
}
// Phobos BitFlags support all the most common operations on flags.
// Some of the operations are shown below.
import std.typecons: BitFlags;
alias ComponentFlags = BitFlags!Component;
immutable ComponentFlags flagsEmpty;
// Value can be set with the | operator.
immutable flagsRed = flagsEmpty | Component.red;
immutable flagsGreen = ComponentFlags(Component.green);
immutable flagsRedGreen = ComponentFlags(Component.red, Component.green);
immutable flagsBlueGreen = ComponentFlags(Component.blue, Component.green);
// Use the & operator between BitFlags for intersection.
assert (flagsGreen == (flagsRedGreen & flagsBlueGreen));
}

View file

@ -1,5 +1,5 @@
const (
apple = iota
banana
cherry
apple = iota
banana
cherry
)

View file

@ -1,5 +1,5 @@
const (
apple = 0
banana = 1
cherry = 2
apple = 0
banana = 1
cherry = 2
)

View file

@ -0,0 +1,7 @@
type fruit int
const (
apple fruit = iota
banana
cherry
)

View file

@ -0,0 +1,7 @@
type fruit int
const (
apple fruit = 0
banana fruit = 1
cherry fruit = 2
)

View file

@ -0,0 +1,3 @@
typedef NS_ENUM(NSInteger, fruits) { apple, banana, cherry };
typedef NS_ENUM(NSInteger, fruits) { apple = 0, banana = 1, cherry = 2 };

View file

@ -1 +1,15 @@
FIRST_NAME, LAST_NAME, PHONE = range(3)
>>> from enum import Enum
>>> Contact = Enum('Contact', 'FIRST_NAME, LAST_NAME, PHONE')
>>> Contact.__members__
mappingproxy(OrderedDict([('FIRST_NAME', <Contact.FIRST_NAME: 1>), ('LAST_NAME', <Contact.LAST_NAME: 2>), ('PHONE', <Contact.PHONE: 3>)]))
>>>
>>> # Explicit
>>> class Contact2(Enum):
FIRST_NAME = 1
LAST_NAME = 2
PHONE = 3
>>> Contact2.__members__
mappingproxy(OrderedDict([('FIRST_NAME', <Contact2.FIRST_NAME: 1>), ('LAST_NAME', <Contact2.LAST_NAME: 2>), ('PHONE', <Contact2.PHONE: 3>)]))
>>>

View file

@ -1 +1 @@
vars().update((key,val) for val,key in enumerate(("FIRST_NAME","LAST_NAME","PHONE")))
FIRST_NAME, LAST_NAME, PHONE = range(3)

View file

@ -0,0 +1 @@
vars().update((key,val) for val,key in enumerate(("FIRST_NAME","LAST_NAME","PHONE")))

View file

@ -0,0 +1,11 @@
module Fruits
APPLE = 0
BANANA = 1
CHERRY = 2
end
# It is possible to use a symbol if the value is unrelated.
FRUITS = [:apple, :banana, :cherry]
val = :banana
FRUITS.include?(val) #=> true

View file

@ -0,0 +1,12 @@
module Card
# constants
SUITS = %i(Clubs Hearts Spades Diamonds)
SUIT_VALUE = SUITS.each_with_index.to_h # version 2.1+
# SUIT_VALUE = Hash[ SUITS.each_with_index.to_a ] # before it
#=> {:Clubs=>0, :Hearts=>1, :Spades=>2, :Diamonds=>3}
PIPS = %i(2 3 4 5 6 7 8 9 10 Jack Queen King Ace)
PIP_VALUE = PIPS.each.with_index(2).to_h # version 2.1+
# PIP_VALUE = Hash[ PIPS.each.with_index(2).to_a ] # before it
#=> {:"2"=>2, :"3"=>3, :"4"=>4, :"5"=>5, :"6"=>6, :"7"=>7, :"8"=>8, :"9"=>9, :"10"=>10, :Jack=>11, :Queen=>12, :King=>13, :Ace=>14}
end

View file

@ -1,5 +0,0 @@
module Fruits
APPLE = 0
BANANA = 1
CHERRY = 2
end