java streams (or any other functional library for other languages) are very nice.
For example, you can have (js sudo code
).
Stream.of([1, 2, 3]).filter(x => x > 2).map(x => x * 5).result(); // [15]
Ignore the syntax or the specific implementation it's just an example.
Now my problems are when the flow is a little bit complicated.
For example, if I need different data on each step like that:
Stream.of([1,2, 3])
.map(x => x * 3)
.zip([4, 5, 6])
.map(..//here i need the initial array)
.map(..//here i need the zipped array)
.total(..//
As you see in some methods i need the last calculated value, in some i need the initial value.
Also, there are situations where I need the intermediate values but after they are calculated.
map(x => x * 1).map(x => x * 2).map(x => x * 4).map(..//i need the result from 2nd map (x*2)
This is a silly example but illustrates the problem.
Is there a good solution to this problem.
I thought I can save all data in the object but this leads to more verbose code because on each step I have to set and get the properties from the object.
Another example:
Sum the numbers: [1, 2, 3, 4] -> 10
Filter numbers above 2: [1, 2, 3, 4] -> [3, 4]
Multiple each number with the sum: [30, 40]
Stream.of([1,2,3, 4])
.sum()
.filter(// here will be the sum, but i want the initial array and later the sum)
.map(// here i want the filtered array and the calculated sum)
Thanks