Эта страница ещё не переведена, поэтому показана на английском. Помогите с переводом

tap

Calls a function with a value for its side effect, then returns the value unchanged.

T tap<T>(void Function(T a) f, T a)

Lecture

tap is data-first: tap(f, value) runs f(value) — usually a print, a log call, or some other side effect — and then hands value straight back unchanged. It exists so you can peek at a value without breaking the expression it's part of: wrap any single value in tap and the surrounding code doesn't need to change at all.

tap operates on one value at a time. Its cousin peek is the pipeline version — it runs a side effect for every element of an Iterable as it's pulled through. For a curried, reusable "logger" you close over f yourself: (v) => tap(f, v), as shown below.

Demo 1 · Basics

Demo 2 · Logging inside a pipe

Closing over tap gives a reusable "curried" logger you can drop into a pipe chain to inspect intermediate values without altering the flow of data:

Try it yourself

Exercise: use tap to print each value as it flows through the map callback, before it gets multiplied by 10.

Related: peek — the pipeline version of tap · pipe1 / pipe — compose a value through functions · identity — pass through with no side effect at all · apply — invoke a function with a dynamic argument list