Tuesday, October 2, 2007

High Order Functions must be tested before use

From my previous article comments it is a small and efficient method of filtering datas:

Cpu = fun({cpu, _, _}) -> true; (_) false end.

But whenever we want to transposing it into a HOF (high order function):

Filter = fun(Elem) ->
fun({Elem, _, _}) -> true; (_) false end

This naive approach doesn't work:

1> Filter = fun(Elem) -> fun({Elem, _, _}) -> true; (_) -> false end end.
2> C = Filter(cpu).
3> C({test, t, t}).
true % this should have been false ...

As explained in this document, we need to use guards to make our high order function effective:

The rules for importing variables into a fun has the consequence that certain pattern matching
operations have to be moved into guard expressions and cannot be written in the head of the fun.

The correct way is:

Filter = fun(Elem) ->
fun({X, _, _}) when X == Elem -> true; (_) false end

So we must keep in mind that sometimes we should really check that our HOF is working as expected !

No comments: