This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** an efficient mutable sequence. all operations are | |
* constant time or amortized constant time */ | |
trait Buffer[E] { | |
/** adds a new element to the end */ | |
def append(e: E): Unit | |
/** removes and returns an element from the end */ | |
@throws[NoSuchElementException]("if the buffer is empty") | |
def unappend(): E |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NaiveArrayBuffer[E] extends Buffer[E] { | |
private val cc = 16 | |
private var size = 0 | |
private var buff = new Array[Any](StartSize) | |
def append(e: E): Unit = { | |
buff(size) = e | |
size += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExpandingArrayBuffer[E] extends Buffer[E] { | |
// other operations are the same as in NaiveArrayBuffer | |
private val ExpansionRatio = 2 | |
def append(e: E): Unit = { | |
expandBufferIfFull() | |
buff.update(size, e) | |
size += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RetractingArrayBuffer[E] extends Buffer[E] { | |
// other operations are the same as in ExpandingArrayBuffer | |
private val RetractionTriggerRatio = 2 | |
private val RetractionRatio = 1 | |
def unappend(): E = { | |
checkNonEmpty() | |
size = size - 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Post( | |
uri: Uri, | |
title: String, | |
slug: Markdown, | |
contents: Markdown) | |
case class Blog( | |
uri: Uri, | |
title: String, | |
description: Markdown) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait PostRepo { | |
def create(post: Post): PostId | |
def retrieve(id: PostId): Post | |
def update(post: Post): Unit | |
def delete(post: Post): Unit | |
def blog(post: Post): Blog | |
def authors(post: Post): Set[Author] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait AuthRepoComponent { | |
val authRepo: AuthRepo | |
trait AuthRepo { | |
def create(auth: Author): AuthId | |
def retrieve(id: AuthId): Author | |
def update(auth: Author): Unit | |
def delete(auth: Author): Unit | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait AuthRepoComponentImpl extends AuthRepoComponent { | |
val authRepo: AuthRepo = new AuthRepoImpl | |
class AuthRepoImpl extends AuthRepo { | |
def create(auth: Author) = println(s"create $auth") | |
def retrieve(id: AuthId) = { | |
println(s"retrieve $id") | |
new Author | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait AuthServComponent { | |
val authServ: AuthServ | |
trait AuthServ { | |
def create(auth: Author): AuthId | |
def retrieve(id: AuthId): Author | |
def update(auth: Author): Unit | |
def delete(auth: Author): Unit | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait AuthServComponentImpl extends AuthServComponent { | |
self: AuthRepoComponent => // this is the self-type | |
val authServ: AuthServ = new AuthServ | |
class AuthServImpl extends AuthServ { | |
def create(auth: Author) = authRepo.create(auth) | |
def retrieve(id: AuthId) = authRepo.retrieve(id) | |
def update(auth: Author) = authRepo.update(auth) | |
def delete(auth: Author) = authRepo.delete(auth) | |
} | |
} |
OlderNewer