Enumerations: Difference between revisions

added Ol
(added Ol)
Line 1,124:
 
<lang Oforth>[ $apple, $banana, $cherry ] const: Fruits</lang>
 
=={{header|Ol}}==
Ol enumerations is an builtin "ff"s, that is a simple fast dictionaries with number, constant or symbol keys and any values.
 
<lang scheme>
(define fruits '{
(apple . 0)
(banana . 1)
(cherry . 2)})
; or
(define fruits {
('apple . 0)
('banana . 1)
('cherry . 2)})
 
; getting enumeration value:
(get fruits 'apple -1) ; ==> 0
; or simply
(fruits 'apple) ; ==> 0
; or simply with default (for non existent enumeration key) value
(fruits 'carrot -1) ; ==> -1
</lang>
<lang scheme>
; simple function to create enumeration with autoassigning values
(define (make-enumeration . args)
(fold (lambda (ff arg i)
(put ff arg i))
#empty
args
(iota (length args))))
 
(make-enumeration 'apple 'banana 'cherry)
; ==> '#ff((apple . 0) (banana . 1) (cherry . 2))
</lang>
 
No automatic values defining
enum Fruits {
Apple,
Banana,
Cherry
}
 
=={{header|Oz}}==