Do eager pipes create multiple lists in F#?

Viewed 64

When using a list an pipes does it create mutliple intermediate lists? If so is this not very bad for garbage collection?

let mylist = [...]
let filterByPipes ls = 
   somefilter ls //is a list created here
   |> otherFiler  // and then again here
   |> filtersForDays // and finally returned here

1 Answers

Yes, F# uses applicative evaluation order, which means that every value has to be fully evaluated before it can be passed as parameter to a function, which means that yes, at every step a new list is created.

As to whether it's bad for garbage collection... "First measure then optimize" - the first rule of optimization.

In most circumstances, the amount of data is so miniscule, it doesn't really make any difference, definitely not enough to make up for the massive gain in code readability.

But if your measurements do determine a bottleneck in this particular place, and it turns out to be significant for the end goal, - then yes, you'll have to manually fuse the processing chain. Or perhaps employ a more efficient data structure. It all would depend on what the optimization goal is.

Related