scala - change state in functional programming -
i want write piece of code (in functional programming style) should keep track of whether user logged in or not. suppose have things in immutable way.
following pseudo code seem functional. takes state , returns reverse value. hasn't got side effects
changestate(boolean state){ return !state } somewhere in logic, once user logs in (or logs out), i'll call above function passing current value of logged in status. unable think of how store logged in status in function way. wrong because currentloggedstate val
val currentloggedstate = false; //user entered login details correctly, change state currentloggedstate = changestate(currentloggedstate) how can write such logic in functional way?
state cannot avoided. point of functional programming enable better reasoning, not make purely mathematical model program.
for example, database literally 1 giant state storage. when you're creating, updating, deleting etc. manipulating state.
there environments (such akka actors model) state unavoidable well. try implementing non-trivial system actors , guarantee you'll have actors full of lists , hashmaps. @ point becomes inevitable. example, coursera course held under courtesy of epfl, called "reactive programming" (it's been renamed fp design in scala or that), had section held roland kuhn himself , involved working actors , throughout course assignments there shitload of state. i'm saying let know there authoritative people in scala community saying state cannot avoided.
in situation, it's best if can push redis or similar storage, state not present in code (only mutability persistence layer / storage).
Comments
Post a Comment