design patterns - Linked objects with functional style programming? -


i new scala experienced programmer trying shed old habits , on board functional style. simple program i'm designing involves questions / answer objects , retains basic facts success. alone easy enough using immutable objects - part of objective link associated facts, program might ask them once in awhile next each other. (example: "the knight rider car was?" , "what tv show features impala? )

in java might have linked associated objects i've written below, immutable laws prevent (i can't link a-> b , instantiate b -> unless in 1 shot?)

and must admit, thing - maybe functional style designed prevent kind of circular referencing. i'm @ loss how correct problem. design fail once question asked - returns copy object new values of "right , asked" object linking seems pitfall again. best guess build relation sets in helper object, based on q/a data only, i'd avoid juggling separate data pattern object.

thank - tim in columbus ohio

case class factitem (      q:string, a:string,     right: double = 0.0,     asked: double = 0.0,     linked: list[factitem] = list[factitem]()      ) {     def rp = right / asked //right per    def sublink( l:list[factitem] ) : factitem =  {     this.copy( linked = l-this)    }  

see question how immutably: scala: how model basic parent-child relation

tip: don't in case class unless override tostring not include list of items (you'll overflow stack if there circular references, since tostring of item includes tostring of others in list).

you may save lot of headaches making list private var , updating instances factory method in companion object creates items, once have references list items. no public setters it's still immutable.

a similar , perhaps theoretically more sound approach make list stream gets values map in companion object. map has mutable while items being created, @ least items immutable.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -