Saltar al contenido

27 · Biblioteca Estándar

Arreglos (std::array)

Mutación, transformación, filtrado, agregación, búsqueda y aplanado de arrays.

En esta página

Los arrays en Zolo son secuencias de longitud variable. Declare con let mut para obtener un array mutable. Los métodos están disponibles en forma de método (arr.map(...)) y en forma de módulo (Array::map(arr, ...)). Importe con use std::Array cuando use la forma de módulo.

Apilar y desapilar

push agrega al final; pop elimina y devuelve el último elemento — el patrón clásico de pila LIFO.

Construcción incremental y pila LIFO con push/pop.

01-push-pop.zolo
Playground
// Feature: Array push / pop — mutation at the end of the array

// When to use: building arrays incrementally, using as a stack.


use std::Array

// `let mut` allows mutating the array.

let mut nums = [1, 2, 3]

// Push appends at the end.

nums.push(4)
nums.push(5)
print(nums)
// expected: [1, 2, 3, 4, 5]

print(nums.len())  // expected: 5


// Pop removes and returns the last element.

let last = nums.pop()
print(last)  // expected: 5

print(nums)  // expected: [1, 2, 3, 4]


// Classic LIFO stack.

let mut stack = []
stack.push("a")
stack.push("b")
stack.push("c")
print(stack.pop())  // expected: c

print(stack.pop())  // expected: b

print(stack.len())  // expected: 1


// Equivalent module form.

let mut xs = [10]
xs.push(20)
xs.push(30)
print(xs)
// expected: [10, 20, 30]

Transformación con map

map aplica una función a cada elemento y devuelve un nuevo array de la misma longitud. El array original no se modifica.

Doblar, elevar al cuadrado, convertir tipo, transformar cadenas — map es siempre no destructivo.

02-map.zolo
Playground
// Feature: Array.map — transforms each element while preserving length

// When to use: applying the same transformation to a whole collection.


use std::Array

let nums = [1, 2, 3, 4, 5]

// Double each number.

let doubled = nums.map(|x| x * 2)
print(doubled)

// expected: [2, 4, 6, 8, 10]


// Square.

let squares = nums.map(|x| x * x)
print(squares)

// expected: [1, 4, 9, 16, 25]


// Type conversion: int -> str.

let labels = Array::map([1, 2, 3], |n| "item-{n}")
print(labels[0])  // expected: item-1

print(labels[2])  // expected: item-3


// Map over strings.

let names = ["alice", "bob", "carol"]
let upper = names.map(|s| s.upper())
print(upper)

// expected: [ALICE, BOB, CAROL]


// Original untouched.

print(nums)
// expected: [1, 2, 3, 4, 5]

Filtrado con filter

filter conserva solo los elementos que satisfacen el predicado.

Pares, mayores que 5, cadenas no vacías; composición filter + map.

03-filter.zolo
Playground
// Feature: Array.filter — keeps only elements that satisfy a predicate

// When to use: selecting a subset, removing invalid items.


use std::Array

let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

// Even numbers.

let evens = nums.filter(|x| x % 2 == 0)
print(evens)

// expected: [2, 4, 6, 8, 10]


// Greater than 5.

let big = nums.filter(|x| x > 5)
print(big)

// expected: [6, 7, 8, 9, 10]


// Filter non-empty strings.

let words = ["foo", "", "bar", "", "baz"]
let nonempty = words.filter(|s| s.len() > 0)
print(nonempty)

// expected: [foo, bar, baz]


// Compose with map — double only the evens.

let just_evens = nums.filter(|x| x % 2 == 0)
let evens_doubled = just_evens.map(|x| x * 2)
print(evens_doubled)
// expected: [4, 8, 12, 16, 20]

Agregación con reduce

reduce(fn, init) acumula los elementos en un único valor. El segundo argumento es el valor inicial del acumulador.

Suma, producto, máximo manual y concatenación de cadenas via reduce.

04-reduce.zolo
Playground
// Feature: Array.reduce — aggregates elements into a single value

// When to use: summing, counting, finding max, building a final string.


use std::Array

let nums = [1, 2, 3, 4, 5]

// Sum — accumulator starts at 0.

let sum = nums.reduce(|acc: int, x: int| acc + x, 0)
print(sum)  // expected: 15


// Product — accumulator starts at 1.

let prod = nums.reduce(|acc: int, x: int| acc * x, 1)
print(prod)  // expected: 120


// Manual maximum.

let max = nums.reduce(|acc: int, x: int| if x > acc { x } else { acc }, 0)
print(max)  // expected: 5


// Concatenate strings.

let words = ["hello", " ", "zolo", "!"]
let joined = words.reduce(|acc: str, w: str| acc + w, "")
print(joined)

// expected: hello zolo!


// Count even numbers in the list.

let inputs = [1, 2, 3, 4, 5, 6]
let evens_count = inputs.reduce(
  |acc: int, x: int| if x % 2 == 0 { acc + 1 } else { acc },
  0,
)
print(evens_count)  // expected: 3

Búsqueda con find y contains

contains prueba pertenencia por valor; find devuelve el primer elemento que satisface el predicado, o nil si ninguno coincide.

contains simple; find con predicado; patrón if candidate != nil.

05-find-and-contains.zolo
Playground
// Feature: Array.find / Array.contains — searching in arrays

// When to use: getting the first item matching a criterion, testing membership.


use std::Array

let nums = [1, 2, 3, 4, 5]

// contains — simple membership by value.

print(nums.contains(3))  // expected: true

print(nums.contains(99))  // expected: false


// find — first element satisfying the predicate, or nil.

let first_even = nums.find(|x| x % 2 == 0)
print(first_even)  // expected: 2


let big = nums.find(|x| x > 100)
print(big)  // expected: nil


// find on a string array.

let names = ["alice", "bob", "carol", "dave"]
let starts_c = names.find(|n| n.starts_with("c"))
print(starts_c)  // expected: carol


// Combine with if-let for safe usage.

let candidate = nums.find(|x| x > 3)
if candidate != nil {
  print("found: {candidate}")
} else {
  print("nothing")
}
// expected: found: 4

Aplanar e iterar con efecto secundario

flat() aplana un nivel de anidamiento. each ejecuta un bloque por elemento y devuelve nil — adecuado para efectos secundarios como print.

flat para unir sub-arrays; each para imprimir; composición map + flat como flatMap.

06-flat-and-each.zolo
Playground
// Feature: Array.flat / Array.each — flattening and iteration with side effects

// When to use: combining nested arrays, performing an action per item without a return.


use std::Array

// flat — flattens one level of nesting.

let nested = [[1, 2], [3, 4], [5]]
let flat = nested.flat()
print(flat)

// expected: [1, 2, 3, 4, 5]


// flat with arrays of varying sizes — all become items.

let mixed = [[1], [], [2, 3, 4], [5]]
print(mixed.flat())

// expected: [1, 2, 3, 4, 5]


// each — side effect per item, returns nil.

print("--- each ---")
Array::each([10, 20, 30], |x| print("item: {x}"))

// expected: --- each ---

// expected: item: 10

// expected: item: 20

// expected: item: 30


// Classic combination: flat + map for "flatMap".

let phrases = ["zolo lang", "rust roxo"]
let tokens = phrases.map(|s| s.split(" ")).flat()
print(tokens)
// expected: [zolo, lang, rust, roxo]

Desafío

Dado un array de palabras, use filter + map para obtener solo las palabras con más de 3 letras, en mayúsculas. Luego sume las longitudes con reduce.

Consulta también

Índice global

Encuentra tu camino por Zolo

Prueba una idea

Empieza aquí

9 resultados

9 resultados

enespt-br