views:

126

answers:

3

This may be an oxymoron, but how would one update a data entity in the functional programming style? From all I've read, functional-style programming uses transformations to return an output on immutable entities. The only thing I can think of would be to completely replace the original entity, but that seems almost the same as a classic update approach.

+2  A: 

The short answer is that in the functional style, each data entity would be immutable, so an update is really a new data entity with the updated value, kind of like how strings work in .Net.

The real interesting challenges come when dealing with IO, it becomes tough to model I/O in a purely functional manner which leads to workarounds like Monads.

Sijin
+7  A: 

Are you talking about on disk database entities or data-structures in memory.

For the latter, functional languages use persistent data-structures, which are implemented such that the new version and the old version are both available after the update, but they share common parts (so that it is efficient). So you appear to be returning a totally new datastructure, but in fact, it shares most of its implementation with the one it was modifying.

There are some really good implementations to look at in the clojure source (written in Java) -- I took two of them apart on my blog

http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-7.html

http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-8.html

Lou Franco
+1  A: 

Lou Franco has it. Data structures in functional languages are implemented such that to modifying them, you "completely replace" the original entity. Behind the scenes, they still use most of the old one: they just replace the changed bits. The old version still exists, too, but garbage collection will destroy it eventually as long as nobody references it.

Jacob B