Re: named array
- Posted by jimcbrown (admin) Sep 04, 2013
- 2025 views
I really convenient to think in terms of "state of the system." I send to a function the state of system, and get the data. At first it may seem that we should return the new state of the system, but it was uncomfortable. It is much better to return the data. What to do with them will decide the function which data were requested. Maps are not formed me that it is necessary to take care of data backup. These two pieces of code equivalents.
Actually, these two are code equivalents.
function willAccident_Map(map State, atom Time) atom Velocity = map:get(State, "velocity") -- get velocity of system atom Acceleration = map:get(State, "acceleration") -- get acceleration of system Velocity += Acceleration * Time -- no need to keep the old velocity! map NewState = map:clone(State) -- get new state map:put(NewState, "velocity", Velocity) -- put new velocity to new state return isAccident(NewState) -- no need to save answer end function function willAccident_Narr(narr State, atom Time) atom Velocity = narr:get(State, "velocity") -- get velocity of system atom Acceleration = narr:get(State, "acceleration") -- get acceleration of system Velocity += Acceleration * Time -- no need to keep the old velocity! narr NewState = narr:put(State, "velocity", Velocity) -- because we can get new state return isAccident(NewState) -- no need to save answer end function
I could go further, and envision a new function to map that'd go even further.
function willAccident_Map(map State, atom Time) atom Velocity = map:get(State, "velocity") -- get velocity of system atom Acceleration = map:get(State, "acceleration") -- get acceleration of system Velocity += Acceleration * Time -- no need to keep the old velocity! map NewState = map:clone_and_put(State, "velocity", Velocity) -- because we can get new state return isAccident(NewState) -- no need to save answer end function
Shorter, cleaner, safer.
It seems to me what you want (in lieu of direct support of Erlang atom-like functionality in Eu) is an immutable version of maps - one in which every function that could modify a map would return the new map to you while leaving the original (and immutable) map unchanged.