Skip to content

Instantly share code, notes, and snippets.

@fancellu
Created November 23, 2015 09:19
Show Gist options
  • Save fancellu/2ade41a7f92e8424b3ee to your computer and use it in GitHub Desktop.
Save fancellu/2ade41a7f92e8424b3ee to your computer and use it in GitHub Desktop.
Simple example of document store, one mutable the other immutable.
object stuff {
class Catalog(documentsIn:List[Document]=List.empty){
def isEmpty=_documents.isEmpty
def add(document:Document)= _documents::=document
private[this] var _documents=documentsIn
def documents= _documents
}
class ImCatalog(val documents:List[Document]=List.empty){
def isEmpty=documents.isEmpty
def +(document:Document)= new ImCatalog(document::documents)
}
case class Document(title:String)
val cat=new Catalog
assert(cat.isEmpty==true)
val helloWorld=Document("Hello world")
cat.add(helloWorld)
assert(cat.isEmpty==false)
assert(cat.documents.head==helloWorld)
var imcat=new ImCatalog
assert(imcat.isEmpty==true)
imcat+=helloWorld
assert(imcat.isEmpty==false)
assert(imcat.documents.head==helloWorld)
val docs=List(helloWorld,helloWorld)
val cat2=new Catalog(docs)
assert(cat2.documents.size==2)
imcat=new ImCatalog(docs)
assert(imcat.documents.size==2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment