apply
Builtin index · Signal processing
result = apply(x, f)
Input: x, f · Output: result
What it is
x: a number, boolean, vector, matrix, or Signal; f: a one-argument function of a number returning a number — the function itself, or a Str naming one you defined.
Overloads
x can be number, boolean, Vec, Mat, or Signal; f can be number, boolean, Vec, Mat, Signal, or List.
3 forms. Each carries its own inputs, outputs and worked example — pick the tab for the one you are calling.
| Code | Input | Output | Description |
result = apply(x, f) | x number or booleanx a bare number (or boolean).f number or booleanf: a one-argument function of a number returning a number — the function itself, or a Str naming one you defined. | x Signal, vector, matrix, number, or booleana Signal(xs, fs) comes back as a new Signal at the same fs, a vector/matrix keeps its own shape, a bare number/boolean applies the function oncemap(xs, f) string[Collections, Strings & Data Frames](collections-strings.md) | Applies fnName exactly once, returning a number. |
Examples
Written for this form and run by the build -- the output below is what the engine actually produced.
double(v) := v * 2
n = apply(5, "double")
print("scalar: {n}") # scalar: 10
scalar: 10
| Code | Input | Output | Description |
result = apply(x, f) | x Vec or Matx a Vec/Mat.f Vec or Matf: a one-argument function of a number returning a number — the function itself, or a Str naming one you defined. | x Signal, vector, matrix, number, or booleana Signal(xs, fs) comes back as a new Signal at the same fs, a vector/matrix keeps its own shape, a bare number/boolean applies the function oncemap(xs, f) string[Collections, Strings & Data Frames](collections-strings.md) | Applies fnName elementwise, returning the same shape. |
Examples
Written for this form and run by the build -- the output below is what the engine actually produced.
double(v) := v * 2
v = apply([1, 2, 3], "double")
print("vector: {v}") # vector: [2, 4, 6]
vector: [2, 4, 6]
| Code | Input | Output | Description |
result = apply(x, f) | x Signalx a Signal.f Signal or Listf: a one-argument function of a number returning a number — the function itself, or a Str naming one you defined. | x Signal, vector, matrix, number, or booleana Signal(xs, fs) comes back as a new Signal at the same fs, a vector/matrix keeps its own shape, a bare number/boolean applies the function oncemap(xs, f) string[Collections, Strings & Data Frames](collections-strings.md) | Applies fnName elementwise and re-wraps the result at the SAME Fs — the one case map can't reproduce, since map always returns a plain List and drops the sample rate. |
Examples
Written for this form and run by the build -- the output below is what the engine actually produced.
double(v) := v * 2
sig = signal([1, 2, 3, 4], 8)
s = sig.apply("double")
print("signal: {s}") # signal: signal(Fs=8) [2, 4, 6, 8]
signal: signal(Fs=8) [2, 4, 6, 8]
apply is the one to reach for when you have your own user-defined transform and need it applied elementwise while KEEPING whatever container you started with — a Signal's sample rate in particular, which map would silently drop. The example below defines a trivial doubling function and applies it to a Signal, printing the result to show the Fs survives the round trip.
double(v) := v * 2
sig = signal([1, 2, 3, 4], 8)
out = sig.apply("double") # same Fs, genuinely transformed
print("apply(double): {out}")
apply(double): signal(Fs=8) [2, 4, 6, 8]