Enumerations

From Rosetta Code
Revision as of 01:28, 20 February 2008 by rosettacode>Mwn3d (Changed over to works with template)
Task
Enumerations
You are encouraged to solve this task according to the task description, using any language you may know.

Create an enumeration of types with and without values.

Ada

Ada enumeration types have three distinct attributes, the enumeration literal, the enumeration position, and the representation value. The position value is implied from the order of specification of the enumeration literals in the type declaration. The position values provide the ordering for the enumeration values. In the example below apple is less than banana which is less than cherry due to their positions, not due to their enumeration literal. There is no necessary relationship between the enumeration position and the enumeration representation.

type Fruit is (apple, banana, cherry); - No specification of the representation value;
for Fruit use (apple => 1, banana => 2, cherry => 3); - specification of the representation values

Ada enumeration types are non-numeric discrete types. They can be used to index arrays, but there are no arithmetic operators for enumeration types. Characters are implemented as an enumeration type in Ada.

BASIC

Works with: QuickBasic version 4.5
Works with: PB version 7.1
 REM Impossible. Can only be faked with arrays of strings.
 OPTION BASE 1
 DIM SHARED fruitsName$(1 to 3)
 DIM SHARED fruitsVal%( 1 to 3)
 fruitsName$[1] = "apple"
 fruitsName$[2] = "banana"
 fruitsName$[3] = "cherry"
 fruitsVal%[1] = 1
 fruitsVal%[2] = 2
 fruitsVal%[3] = 3
 REM OR GLOBAL CONSTANTS
 DIM SHARED apple%, banana%, cherry%
 apple%  = 1
 banana% = 2
 cherry% = 3

C

Works with: gcc
Works with: MSVC
Works with: BCC
Works with: Watcom
 enum fruits { apple, banana, cherry };
 enum fruits { apple = 0, banana = 1, cherry = 2 };

C++

Works with: g++
Works with: Visual C++
Works with: BCC
Works with: Watcom
 enum fruits { apple, banana, cherry };
 enum fruits { apple = 0, banana = 1, cherry = 2 };

C#

 enum fruits { apple, banana, cherry }
 enum fruits { apple = 0, banana = 1, cherry = 2 }
 enum fruits : int { apple = 0, banana = 1, cherry = 2 }


E

Simple group of object definitions (value methods could be left out if appropriate):

def apple  { to value() { return 0 } }
def banana { to value() { return 1 } }
def cherry { to value() { return 2 } }

With a guard for type checks:

interface Fruit guards FruitStamp {}
def apple  implements FruitStamp {}
def banana implements FruitStamp {}
def cherry implements FruitStamp {}

def eat(fruit :Fruit) { ... }

With and without values, using a hypothetical enumeration library:

def [Fruit, [=> apple, => banana, => cherry]] := makeEnumeration()
def [Fruit, [=> apple, => banana, => cherry]] :=
  makeEnumeration(0, ["apple", "banana", "cherry"])

Forth

Forth has no types, and therefore no enumeration type. To define sequential constants, a programmer might write code like this:

0 CONSTANT apple
1 CONSTANT banana
2 CONSTANT cherry
...

However, a common idiom in forth is to define a defining word, such as:

: ENUM ( n -<name>- n+1 )   DUP CONSTANT 1+ ;

This word defines a new constant of the value specified and returns the next value in sequence. It would be used like this:

0 ENUM APPLE  ENUM BANANA  ENUM CHERRY  DROP

Or you can use CONSTANT to capture the "end" value instead of dropping it:

0 ENUM FIRST ENUM SECOND ...  CONSTANT LAST

A variation of this idea is the "stepped enumeration" that increases the value by more than 1, such as:

: SIZED-ENUM ( n s -<name>- n+s )   OVER CONSTANT + ;
: CELL-ENUM ( n -<name>- n+cell )   CELL SIZED-ENUM ;

A programmer could combine these enum definers in any way desired:

0 ENUM       FIRST   \ value = 0
CELL-ENUM    SECOND  \ value = 1
ENUM         THIRD   \ value = 5
3 SIZED-ENUM FOURTH  \ value = 6
ENUM         FIFTH   \ value = 9
CONSTANT     SIXTH   \ value = 10

Note that a similar technique is often used to implement structures in Forth.

For a simple zero-based sequence of constants, one could use a loop in the defining word:

: CONSTANTS ( n -- ) 0 DO I CONSTANT LOOP ;
\ resistor digit colors
10 CONSTANTS black brown red orange yellow green blue violet gray white

Haskell

data Fruit = Apple | Banana | Cherry deriving Enum

Java

Works with: Java version 1.5+
 enum fruits { apple, banana, cherry }
 enum fruits
 {
   apple(0), banana(1), cherry(2)
   private final int value;
   fruits(int value) { this.value = value; }
   public int value() { return value; }
 }

JavaScript

 var fruits { apple, banana, cherry };
 var fruits { apple : 0, banana : 1, cherry : 2 };

JSON

 fruits { apple, banana, cherry };
 fruits { apple : 0, banana : 1, cherry : 2 };

JScript.NET

 enum fruits { apple, banana, cherry }
 enum fruits { apple = 0, banana = 1, cherry = 2 }

Lisp

In most Lisps, it is natural to use symbols for enumerations, often without any sort of pre-declaration. If values are desired, they can be assigned to the symbols.

Common Lisp

Values:

;; symbol to number
(defconstant apple 0)
(defconstant banana 1)
(defconstant cherry 2)
;; number to symbol
(defun index-fruit (i)
  (aref #(apple banana cherry) i))

Of course, the two definitions above can be produced by a single macro, if desired.

Defining a type for documentation or checking purposes:

(deftype fruit ()
  '(member apple banana cherry))

Scheme

(define apple 0)
(define banana 1)
(define cherry 2)

(This section needs attention from someone familiar with Scheme idioms.)

OCaml

type fruit =
  Apple
| Banana
| Cherry

Perl

 # Using an array
 my @fruits = qw(apple banana cherry);
 # Using a hash
 my %fruits = ( apple => 0, banana => 1, cherry => 2 );

PHP

 // Using an array/hash
 $fruits = array( "apple", "banana", "cherry" );
 $fruits = array( "apple" => 0, "banana" => 1, "cherry" => 2 );
 // If you are inside a class scope
 class Fruit {
   const APPLE = 0;
   const BANANA = 1;
   const CHERRY = 2;
 }
 // Then you can access them as such
 $value = Fruit::APPLE;
 // Or, you can do it using define()
 define("FRUIT_APPLE", 0);
 define("FRUIT_BANANA", 1);
 define("FRUIT_CHERRY", 2);

Python

Works with: Python version 2.5

There is no special syntax, typically global variables are used with range:

FIRST_NAME, LAST_NAME, PHONE = range(3)

Raven

{ 'apple' 0 'banana' 1 'cherry' 2 } as fruits

Seed7

 const type: fruits is new enum
     apple, banana, cherry
   end enum;

Tcl

The following elegant example comes straight from the [Tcl wiki:]

 proc enumerate {name values} {
   interp alias {} $name: {} lsearch $values
   interp alias {} $name@ {} lindex $values
 }

it would be used like this:

enumerate fruit {apple blueberry cherry date elderberry}

fruit: date
  ==> prints "3"
fruit@ 2
  ==> prints "cherry"

Toka

Toka has no data types, and therefore no actual enumeration type. There is an optional library function which does provide a way to create enumerated values easily though.

This library function takes a starting value and a list of names as shown in the example below.

needs enum
0 enum| apple banana carrot |
10 enum| foo bar baz |

Visual Basic .NET

 ' Is this valid?!
 Enum fruits
 apple
 banana
 cherry
 End Enum
 ' This is correct
 Enum fruits
 apple = 0
 banana = 1
 cherry = 2
 End Enum