Skip to content

Instantly share code, notes, and snippets.

View sullivan-'s full-sized avatar

John Sullivan sullivan-

View GitHub Profile
/** 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
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
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
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
case class Post(
uri: Uri,
title: String,
slug: Markdown,
contents: Markdown)
case class Blog(
uri: Uri,
title: String,
description: Markdown)
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]
}
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
}
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
}
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
}
}
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)
}
}