From d4ea952032b099a4674c27e964ab6ec421c690f4 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Fri, 26 Apr 2019 23:21:22 +0200 Subject: Dubious way to have lazy orddict updates. I want to keep the benefits of orddict:fetch vs orddict:find, but still be able to re-use the existing code when adding a new element. --- src/ataxic.erl | 6 +++++- src/ataxic_sugar.erl | 32 +++++++++++++++++++------------- 2 files changed, 24 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/ataxic.erl b/src/ataxic.erl index 195d77f..4d829f3 100644 --- a/src/ataxic.erl +++ b/src/ataxic.erl @@ -126,7 +126,7 @@ -export([apply_to/2, matches/2]). --export([optimize/1]). +-export([optimize/1, is_constant/1]). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -378,3 +378,7 @@ matches (OP, Entry) -> true -> true; _ -> false end. + +-spec is_constant (basic()) -> boolean(). +is_constant (#const{}) -> true; +is_constant(_) -> false. diff --git a/src/ataxic_sugar.erl b/src/ataxic_sugar.erl index 3098196..7f6e441 100644 --- a/src/ataxic_sugar.erl +++ b/src/ataxic_sugar.erl @@ -64,20 +64,26 @@ update_orddict_element (IX, OP) -> store, [ ataxic:constant(IX), - ataxic:sequence ( - [ - ataxic:apply_function - ( - orddict, - fetch, - [ - ataxic:constant(IX), - ataxic:current_value() - ] - ), - OP - ] + case ataxic:is_constant(OP) of + true -> OP; + false -> + ataxic:sequence + ( + [ + ataxic:apply_function + ( + orddict, + fetch, + [ + ataxic:constant(IX), + ataxic:current_value() + ] + ), + OP + ] + ) + end ), ataxic:current_value() ] -- cgit v1.2.3-70-g09d2