Re: named array
- Posted by TheresNoTime Sep 03, 2013
- 2199 views
BRyan said...
You can not just say Euphoria's maps don't suit me because of side effects and not explain what you mean by that statement.
A side effect is that the function is changing something outside of themselves. Functions without side effects named "pure functions". Sometimes side effects are necessary, because the display and the hard drive is also part of the outside world. However, it should by all means avoid impurity, if possible. When I'm going to use the maps, I can not write a pure function. Example:
include std/map.e include std/math.e map JohnSmith = map:new() map:put(JohnSmith, "Expenses", {1000, 2000, 3000, 4000}) function getSumExpenses(map Client) sequence Expenses = map:get(Client, "Expenses") return sum(Expenses) end function function getHistory(map Client, integer Months) sequence Expenses = map:get(Client, "Expenses") return head(Expenses, Months) end function function getSumHistoryExpenses(map Client, integer Months) map:put(Client, "Expenses", getHistory(Client, Months)) return getSumExpenses(Client) end function puts(1, "Sum of Expenses from January to March = ") print(1, getSumHistoryExpenses(JohnSmith, 3)) puts(1, "\nAll expenses, must be present up to April = ") print(1, map:get(JohnSmith, "Expenses")) puts(1, "\nFAIL!!! Map has changed forever, even though we changed it inside the function.")