typestar

WriteStream en Smalltalk

Arma un string sin concatenación repetida.

"A WriteStream builds a string without repeated concatenation."
| report |
report := WriteStream on: String new.
report
    nextPutAll: 'DAILY DOCKET';
    cr;
    nextPutAll: '3 jobs, ';
    print: 545;
    nextPutAll: ' sheets'.

Transcript show: report contents; cr.

"streamContents: wraps the same pattern in one expression."
Transcript show: (String streamContents: [:s |
    #('set' 'ink' 'pull') do: [:step | s nextPutAll: step]
        separatedBy: [s nextPutAll: ' / ']]); cr.

Cómo funciona

  1. WriteStream on: String new abre el constructor.
  2. nextPutAll: agrega; print: formatea números adentro.
  3. streamContents: envuelve el patrón en una expresión.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
16
Caracteres a escribir
454
Tokens
78
Ritmo de tres estrellas
55 tpm

Al ritmo de tres estrellas de 55 tokens por minuto, este intento toma unos 85 segundos.

Escribe este fragmento

Paso 1 de 2 en Streams; paso 24 de 31 en Fundamentos del lenguaje.

← Anterior Siguiente →