anchor.diff:      lists:foldr(F, <<>>, Parts).
   
   
  +safe_average(Values) ->
  +    safe_average(Values, 1, 0.0).
  +
  +safe_average([], _Cnt, Avg) -> Avg;
  +safe_average([{_TS, null} | Vs], Cnt, Avg) ->
  +    safe_average(Vs, Cnt, Avg);
  +safe_average([{_TS, Val} | Vs], Cnt, Avg) ->
  +    NewAvg = Avg + (Val - Avg) / Cnt,
  +    safe_average(Vs, Cnt + 1, NewAvg);
  +safe_average([null | Vs], Cnt, Avg) ->
  +    safe_average(Vs, Cnt, Avg);
  +safe_average([Val | Vs], Cnt, Avg) ->
  +    NewAvg = Avg + (Val - Avg) / Cnt,
  +    safe_average(Vs, Cnt + 1, NewAvg).
  +
  +
   %%
   %% TESTS
   %%
  
positive.code:                       {alias_target(Target, Aliases), Values}
                end, Series);
  
  evaluate_call(Unknown, _Args, _From, _Until, _Now) ->
      lager:error("unknown function call ~p or invalid arguments", [Unknown]),
      error.
  
negative.code:      ?_assertEqual(<<"foo">>, alias_target(<<"foo.bar.test">>, [0]))
      ].
  
  safe_average_test_() ->
      [?_assertEqual(4.0, safe_average([4])),
       ?_assertEqual(3.0, safe_average([2, 4])),
       ?_assertEqual(3.0, safe_average([2, 4, 4, 2])),
       ?_assertEqual(0.0, safe_average([])),
       ?_assertEqual(0.0, safe_average([null])),
       ?_assertEqual(3.0, safe_average([2, 4, null])),
       ?_assertEqual(4.0, safe_average([null, 4, null])),
       ?_assertEqual(3.0, safe_average([{0, 2}, {0, 4}, {0, null}])),
       ?_assertEqual(4.0, safe_average([{0, null}, {0, 4}, {0, null}]))
      ].
  
  average_above_test_() ->
      Series = [{<<"target">>, [{100, 5.0}, {110, 7.0}]}],
      [?_assertEqual(Series, evaluate_call(<<"averageAbove">>, [Series, 6.0], 0, 0, 0)),
       ?_assertEqual([], evaluate_call(<<"averageAbove">>, [Series, 6.5], 0, 0, 0))
      ].
  
  average_below_test_() ->
      Series = [{<<"target">>, [{100, 5.0}, {110, 7.0}]}],
      [?_assertEqual([], evaluate_call(<<"averageBelow">>, [Series, 5.5], 0, 0, 0)),
       ?_assertEqual(Series, evaluate_call(<<"averageBelow">>, [Series, 6.0], 0, 0, 0)),
       ?_assertEqual(Series, evaluate_call(<<"averageBelow">>, [Series, 10], 0, 0, 0))
      ].
  
  -endif.
  