Re: named array
- Posted by TheresNoTime Sep 04, 2013
- 2029 views
It might be easier if I explain why I conceived the idea of any structure. I'm tired of writing a lot of function parameters. In theory, I can do without any structure at all, even without the sequence.
function getSomeCalculations(sequence x, sequence y, atom velocity, integer index, sequence etc) return bla bla bla end function
The disadvantage is that I have to remember what parameters to pass and in what order. I have tried a variety of options, including all the ones that you advised. And only then, after much trial and error, I came up with narr. 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.
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 atom NewVelocity = Velocity + Acceleration * Time -- velocity after "Time" passed map:put(State, "velocity", NewVelocity) -- put new velocity to SAME state boolean Accident = isAcccident(State) -- save answer map:put(State, "velocity", Velocity) -- don't forget to restore state of system!!! return Accident 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
Shorter, cleaner, safer.