Lazy Mapped Array
Problem
Design LazyMappedArray, an integer array that remembers mapping functions and evaluates them only when a search needs transformed values.
The object keeps the source positions fixed. A search walks those positions from left to right, applies the current transformation chain to each examined value, and stops at the first match.
Requirements
- Deferred mapping.
maprecords the supplied integer-to-integer callable without invoking it, returns the sameLazyMappedArray, and includes it in later searches. - Mapping order. For each examined source value, apply every registered callable exactly once in
mapcall order, passing each result to the next callable. - First match.
getIndexOfreturns the smallest zero-based source index whose fully transformed value equals the target, or-1when no value matches. An empty source array also returns-1. - Early stop. After finding the first match,
getIndexOfreturns without invoking any callable for a later source index. A missing search examines every source index.
API
| Signature | Returns | Behavior |
|---|---|---|
LazyMappedArray(values) | A new LazyMappedArray | Stores the ordered signed 64-bit source values with no registered transformations. |
map(transformation) | This same LazyMappedArray instance | Registers a callable from one signed 64-bit integer to one signed 64-bit integer without invoking it. |
getIndexOf(value) | The first matching zero-based index, or -1 | Lazily applies the current transformation chain while searching for the signed 64-bit target. |
Examples
Start with [1, 2, 3, 2] and call map(x -> x * 2). The callable is not invoked by map. A later getIndexOf(4) transforms the values at indices 0 and 1, obtains 2 and 4, and returns 1. It does not transform indices 2 or 3.
For [1, 2], the chain map(x -> x + 3).map(x -> x * 2) produces logical values 8 and 10. Therefore, getIndexOf(10) returns 1.
Constraints
0 <= values.length <= 1000- At most
50calls tomapper object - At most
500calls togetIndexOfper object - Source values, targets, and every intermediate callable result fit a signed 64-bit integer
- Every supplied callable is valid and returns normally for the tested inputs
- Calls are sequential
Notes
The custom-test JSON represents a callable with integer multiply and add fields. The driver converts that object to x -> multiply * x + add before calling map. This transport representation is not part of the learner API; map receives a callable.
No behavior is required for caching results across searches.