3

I am new to some of the advanced functional programming ideas, in particular: how to work with immutable data. Data structures are often composites, composed of smaller data structures. For instance, if we have a Family collection which is made up all of the Members of a household. We might construct the family:

var flintstones = new Family().
    add(new Member({name: 'Fred'})).    //returns new instance of family 
    add(new Member({name: 'Wilma'}).    // "
    add(new Member({name: 'Pebbles'})); // "
var fred = flintstones.get({name: 'Fred'}).set({lname: 'Flintstone'});
flintstones = fred.family(); //new instance of family with latest fred.

Notice how changing fred did not actually change flintstones. I can grab a fresh reference to flintstones but for what purpose? As all objects are mere snapshots I can't see the point of retaining a reference. State changes have been abstracted away, so we're not going to use the Observer pattern. Thus, how are dependent things like GUIs that care about state changes handled? What is the functional alternative to observation for keeping things in sync? I don't see the objects themselves as having any business in rendering themselves. How might a functional program handle keeping the state of a single-page webapp GUI in sync?

For what it's worth Rich Hickey's talks inspired me to explore functional programming. I get his concepts, but I struggle with leaping to a practical implementation (in JavaScript).

Mario
  • 6,572
  • 3
  • 42
  • 74
  • Not sure I got your question: this is an OOP approach, not a functional programming one. Are you asking how to convert that in a more functional way? – ZER0 Apr 12 '13 at 20:23
  • From what I've been reading (especially the links provided by Matthew Strawbridge on his comments), this is more about *reactive* than *functional* programming. – bfavaretto Apr 13 '13 at 20:17
  • I understand that persistent data structures are immutable. What I think I was not understanding is that state management -- knowing that Wilma changed her maiden name when she married Fred -- still requires the programmer to manage references (somewhere, somehow) to the current state of Wilma. In memory somewhere there is still a Wilma Slaghoople, but our current reference is no longer pointing to it. – Mario Jun 25 '14 at 17:24

1 Answers1

2

In your example, Family is supposed to be immutable. So each call to add must return a whole new object that is based on the contents of the existing object (themselves immutable, so copying them isn't a problem) plus the new thing. Similarly, the set you call on Fred must return a whole new family member based on Fred but with a different last name. (Therefore the original Fred inside Flintstones is not changed at all.)

The advantage of this functional style is that once you've got a reference to an object, you know it's not going to change. If it was valid when it was constructed, you don't have to keep checking whether it's still valid. Any you can pass the object out of your code without having to clone it first to protect your internal copy from being changed.

If you want to learn more about functional programming you might be better off trying out a purely functional language such as Haskell or F#; trying to do functional programming in JavaScript is just likely to be confusing.

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
  • I understanding that new instances of things are being created (lambda calculus, I think it's called), but what I'm missing is how to have changes reflected in a GUI. – Mario Apr 12 '13 at 20:57
  • It's fine to do `var flintstones = fred.family();` (i.e. updating a variable that isn't itself member data of some object to point at a new object). You don't need a new variable. So, for a GUI, you'd just tell the root of the new component tree to update itself. – Matthew Strawbridge Apr 12 '13 at 21:03
  • Let me rephrase. Can you show hypothetical code for reflecting the changes to the data in the GUI? Just a few lines (edit your answer if need be). – Mario Apr 12 '13 at 21:37
  • Examples that I've seen are like mine; they fail to show how the changes fit into the program's main loop. I understand things in the small. I'm trying to see how they fit together in the large. – Mario Apr 12 '13 at 22:01
  • I think you're essentially asking *Is functional GUI programming possible?* (http://stackoverflow.com/questions/2672791), which leads on to *What is (functional) reactive programming?* (http://stackoverflow.com/questions/1028250). – Matthew Strawbridge Apr 12 '13 at 22:02