inject:into: en Smalltalk
El pliegue: enhebra un valor corriente por la colección.
"inject:into: is the fold: thread a running value through."
| sheets total longest |
sheets := #(120 340 85 500 210).
total := sheets inject: 0 into: [:sum :n | sum + n].
Transcript show: 'Edition total: ', total printString; cr.
longest := sheets inject: 0 into: [:best :n | best max: n].
Transcript show: 'Largest run: ', longest printString; cr.
"The accumulator can be anything -- here a string."
Transcript show: (#('set' 'proof' 'print')
inject: 'plan' into: [:acc :step | acc, ' > ', step]); cr.
Cómo funciona
- El primer argumento siembra el acumulador.
- El bloque recibe el valor corriente y cada elemento.
- Cualquier objeto acumula — números o strings igual.
El intento, en números
- Líneas
- 13
- Caracteres a escribir
- 505
- Tokens
- 94
- Ritmo de tres estrellas
- 55 tpm
Al ritmo de tres estrellas de 55 tokens por minuto, este intento toma unos 103 segundos.
Paso 2 de 4 en Enumeración; paso 15 de 31 en Fundamentos del lenguaje.