kongware / scriptum

Functional Programming Unorthodoxly Adjusted to Client-/Server-side Javascript
MIT License
383 stars 21 forks source link

Can arrays be reconciled with lazy evaluation? #376

Closed ivenmarquardt closed 1 year ago

ivenmarquardt commented 1 year ago

Arrays are an inherently strict data type. If a lazy right fold is applied, the Symbol.isConcatSpreadable property makes the [x].concat(ys) operation strict in both its arguments. If the symbol is set to false, the following data structure results:

[a, lambda, lambda, ..] // unevaluated
[a, [a, [a, ..]]] // expanded

Several issues arise:

Fix the nesting

The evaluation function to expand the array can spread the sub arrays into the outermost array. However, this process would be explicit. Many array combinators would need to be modified to implicitly trigger evaluation. An array subclass would be needed.

Fix length

An array subclass could also enforce complete evaluation before accessing length using a smart getter.

Fix index access

This isn't possible with a subtype. A proxy is needed instead, an effort that wouldn't be in proportion with its return.

Conclusion

Reconciliation isn't possible because the result would be quite ineffective.