Reputation: 3885
For example, we need to implement function Set addToSet(Stack, Object)
: it accept a Stack
and some object that can be pushed into the stack; the input object should be pushed into the input stack; we need it to return a Set
, which contains all elements of the input stack with the new object. If we push the new object into the stack, we violate the principle of functional programming. For some reason, we can't push the new object into the stack before or after invoking this function (i.e. do pushing outside the function). How should we do?
My guess solution is to copy the input stack to a new one first, and then modify the new stack and finally return the result Set
with the new stack together. What's your opinion?
Upvotes: 0
Views: 79
Reputation: 2180
How about:
function Stack pushToStack(Stack, Object)
function Set stackToSet(Stack)
stackToSet(pushToStack(Stack,Object))
No more side effect.
Upvotes: 1