Thinking Functional
Posted: Thu Jul 10, 2008 3:22 pm
I can really see and feel the benefits of pure functional programming. I don't yet have my head around it enough to use it everywhere I want to though. Maybe I am off track early somewhere.
As a toy example, pretend I am implementing a chat server. Somewhere, there will be a list of who is currently logged in. Say, for sake of argument that this list should be kept in alphabetical order by nickname. When someone logs in, the function thing to do is create a new list with this person in it. Yet, I still have to (setf ...) something if I am going to have the list the next time I need it. {Technically, if I could count on optimized tail recursion, I could fake it... but...}.
The situation gets worse if someone can update their nickname in real time. Now, I need to construct a whole new user struct to hold the new name and a whole new list that includes this new struct and excludes the old one. The situation is worse if I am tracking attendance in chat rooms within the system or buddy lists or what-have-you.
The problem, I guess is where to draw the line. I have to mutate something or copy whole hierarchies on every change.
Help me think.
As a toy example, pretend I am implementing a chat server. Somewhere, there will be a list of who is currently logged in. Say, for sake of argument that this list should be kept in alphabetical order by nickname. When someone logs in, the function thing to do is create a new list with this person in it. Yet, I still have to (setf ...) something if I am going to have the list the next time I need it. {Technically, if I could count on optimized tail recursion, I could fake it... but...}.
The situation gets worse if someone can update their nickname in real time. Now, I need to construct a whole new user struct to hold the new name and a whole new list that includes this new struct and excludes the old one. The situation is worse if I am tracking attendance in chat rooms within the system or buddy lists or what-have-you.
The problem, I guess is where to draw the line. I have to mutate something or copy whole hierarchies on every change.
Help me think.