Pular para o conteúdo

Operador Tap

Nesta página

O operador &. chama f(valor) mas descarta o retorno e propaga o valor original para o próximo passo. É o ponto certo para logs, métricas e debugs no meio de um pipeline sem quebrar o fluxo.

A forma mais comum combina |> com &. no mesmo passo: o pipe avança a transformação, o tap apenas observa:

Tap entre etapas de pipe; tap standalone em array — observa sem alterar o valor.

02-tap-side-effects.zolo
Playground
// Feature: Tap operator `&.`

// Syntax: `value &. f()` calls `f(value)` but RETURNS the original `value`.

// When to use: side effects (log, debug, metrics) in the middle of a

// pipeline without breaking the flow. It is the "spy" between `|>` pipes.


fn double(x: int) -> int {
  return x * 2
}

fn add_one(x: int) -> int {
  return x + 1
}

// Function used as tap — print is the effect, return is discarded.

fn log_step(x: int) -> int {
  print("  [tap] x = {x}")
  return x
}

// Pipeline with tap between steps — observes without changing.

let r = 5
  |> double() &. log_step()
  // 10

  // prints 10, propagates 10

  |> add_one() &. log_step()
  // 11

  // prints 11, propagates 11

  |> double()
// 22

print("final = {r}")

// expected:

//   [tap] x = 10

//   [tap] x = 11

//   final = 22


// Tap is useful to inspect array length in the middle of the pipeline.

fn show_len(arr: [int]) -> [int] {
  print("  [len] {arr.len()}")
  return arr
}

let nums = [1, 2, 3, 4, 5] &. show_len()
print(nums.len())
// expected:

//   [len] 5

//   5

Gotcha: ao usar tap em arrays dentro de um pipeline de |>, prefira quebrar a etapa intermediária numa variável e chamar print(arr) diretamente. Interpolar um array dentro de uma string ("{arr}") exibe o handle interno, não os elementos.

Veja também

Buscar no Zolo

9 resultados

enespt-br