Skip to content

Instantly share code, notes, and snippets.

View t3hnar's full-sized avatar
🎯
Focusing

Yaroslav Klymko t3hnar

🎯
Focusing
View GitHub Profile
import akka.util.ByteString
import ByteOrder.{BIG_ENDIAN, LITTLE_ENDIAN}
def putLongPart(order: ByteOrder) = ByteString.newBuilder.putLongPart(123, 4)(order).result()
require(putLongPart(BIG_ENDIAN) != putLongPart(LITTLE_ENDIAN))
@t3hnar
t3hnar / api.scala
Last active November 8, 2019 15:11
CQRS example
package cqrs
import scala.reflect.ClassTag
object api {
type Identifier = String
def newIdentifier: Identifier = java.util.UUID.randomUUID().toString
type Revision = Long
val initialRevision: Revision = 0L
@t3hnar
t3hnar / child-schema.json
Created January 21, 2013 09:05
json-schema to scala case class
{
"type": "object",
"$schema": "http://json-schema.org/draft-03/schema",
"id": "#",
"title":"Child",
"properties": {
"name": {
"type": "string",
"id": "name",
"required": true
@t3hnar
t3hnar / EndlessIterator.scala
Created November 21, 2012 14:01
EndlessIterator
class EndlessIterator[T](iterable: Iterable[T]) extends Iterator[T] {
assert(iterable.nonEmpty, "iterable is empty")
private var _iterator = iterable.iterator
private def iterator =
if (_iterator.hasNext) _iterator
else {
_iterator = iterable.iterator
_iterator
@t3hnar
t3hnar / Primes.scala
Created November 2, 2012 17:20
Primes
/**
* @author Yaroslav Klymko
*/
object Primes {
val naturals = {
def loop(n: Long): Stream[Long] = n #:: loop(n + 1)
loop(1)
}
val primes: Stream[Long] = {
@t3hnar
t3hnar / blog.scala
Created September 4, 2012 18:52
Very simple example of event sourcing
object blog {
object events {
sealed trait PostEvent
case class PostCreated(title: String) extends PostEvent
case class PostRenamed(id: Long, name: String) extends PostEvent
class EventStream() {
object ConnectorTypes extends Table[(String, ConnectorType.Value)]("connector_type") {
implicit val ConnectorTypeMapper = new EnumMappedTypeMapper(ConnectorType)
def chargerId = column[String]("charger_id")
def connectorType = column[ConnectorType.Value]("connector_type")
def * = chargerId ~ connectorType
def ux = index("ux_connector_type", chargerId ~ connectorType, unique = true)
def fkCharger = foreignKey("fk_connector_types_charger", chargerId, Charger)(_.id)
@t3hnar
t3hnar / gist:2374796
Created April 13, 2012 07:25
Akka Http processing
val func = (req: HttpServletRequest) => {
// ASYNC SCOPE
// heavy scope, actually this is a place async is needed for
// async request might expire
(res: HttpServletResponse) => {
// RESPONSE SCOPE
// lite scope for putting collected data to response, called if not expired
(result: Boolean) => {
// CALLBACK SCOPE
// called to notify whether response sent successfully
@t3hnar
t3hnar / JavaClass.java
Created February 7, 2012 10:47
Scala types with java classes
public class JavaClass {
public String toString() {
return ""
}
public boolean getBool() {
return false
}
}
package com.thenewmotion.chargenetwork.server.services.proto
import akka.actor._
import akka.actor.Actor._
import akka.routing._
import akka.event.EventHandler
/**
* @author Yaroslav Klymko
*/