Dictionary en Smalltalk
Claves a valores, con at:ifAbsent: leyendo con red.
"Dictionary maps keys to values; at:ifAbsent: reads safely."
| prices |
prices := Dictionary new.
prices at: 'broadside' put: 120.
prices at: 'chapbook' put: 340.
prices at: 'bookmark' put: 15.
Transcript show: (prices at: 'chapbook') printString; cr.
Transcript show: (prices at: 'poster' ifAbsent: [0]) printString; cr.
prices keysAndValuesDo: [:job :cents |
Transcript show: job, ' -> ', cents printString; cr].
Transcript show: prices keys asSortedCollection printString; cr.
Cómo funciona
at:put:escribe;at:lee y lanza si falta.at:ifAbsent:da un bloque para el caso ausente.keysAndValuesDo:recorre ambas mitades a la vez.
Palabras clave y builtins usados aquí
new
El intento, en números
- Líneas
- 14
- Caracteres a escribir
- 482
- Tokens
- 82
- Ritmo de tres estrellas
- 60 tpm
Al ritmo de tres estrellas de 60 tokens por minuto, este intento toma unos 82 segundos.
Paso 2 de 4 en Colecciones; paso 11 de 31 en Fundamentos del lenguaje.