Skip to content

Instantly share code, notes, and snippets.

@bblfish
Last active July 10, 2021 17:23
Show Gist options
  • Save bblfish/e759acba8794b53462c362935a80e471 to your computer and use it in GitHub Desktop.
Save bblfish/e759acba8794b53462c362935a80e471 to your computer and use it in GitHub Desktop.
package monocle
import monocle.Lens
import monocle.syntax.all.*
import java.time.{Clock, Instant}
import scala.collection.immutable.*
//for an intro to Monocle see the video https://twitter.com/bblfish/status/1413758476896153601
// and the thread of references above it.
// and the documentation https://www.optics.dev/Monocle/
// note this is Scala 3 code
sealed trait Resource[T] {
def content: T
def created: Instant
//def modified: Instant
}
def now: Instant = Clock.systemUTC().instant()
case class Container(
content: Map[String, Resource[_]] = Map(),
created: Instant = now
) extends Resource[Map[String, Resource[_]]]
// of course there could be many more Resources (eg. RDF Graphs, Pictures, etc)
case class TextResource(
content: String,
created: Instant = now
) extends Resource[String]
object Server {
val root = Container()
val contents = Lens[Container, Map[String, Resource[_]]](_.content){ newmap =>
cont => cont.copy(content = newmap)
}
def POST(slug: String, text: String): Container => Container = contents.modify(oldMap =>
oldMap + (slug -> TextResource(text))
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment