User loginNavigation |
Filter-Reduce OptimizationThis must be well-know amongst functional programming researchers, but I kind of stumbled upon it by accident when studying Joy. Consider the following program to count even numbers: define count_evens = [2 mod 0 neq] filter 0 [inc] reduce So for those new to Joy, every program/function takes it parameter on the stack, and pushes its results onto the stack. Here is a brief explanation of the operators:
The program I showed takes a list on the stack, filters the list for only even numbers, and then counts them using a reduce function. Assuming you are following me so far, this program can be rewritten as follows (caveat: I'm doing this off the top of my head so I could have made a mistake in syntax): def count_evens_1 = 0 [2 mod 0 eq [inc] [] if] reduce What I've done here is eliminate the unneccessary filter operation and "folded" it (pun intended) into the reduce operation. My observation when working with Joy is that this optimization is trivial to automate. So my question to the group is: is this a well known optimization, and if so, is it as easily applied in other functional languages? For those whose curiosity has been piqued, I'm researching the viability of using a Joy dialect as an intermediate language. By cdiggins at 2006-05-02 02:09 | LtU Forum | previous forum topic | next forum topic | other blogs | 5666 reads
|
Browse archives
Active forum topics |
Recent comments
22 weeks 6 days ago
22 weeks 6 days ago
22 weeks 6 days ago
45 weeks 19 hours ago
49 weeks 2 days ago
50 weeks 6 days ago
50 weeks 6 days ago
1 year 1 week ago
1 year 6 weeks ago
1 year 6 weeks ago