Skip to content

Instantly share code, notes, and snippets.

View t3hnar's full-sized avatar
🎯
Focusing

Yaroslav Klymko t3hnar

🎯
Focusing
View GitHub Profile
@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 / 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 / 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
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 / codeStyleSettings.xml
Last active August 29, 2015 13:56
Intellij Idea codeStyleSettings.xml for Scala
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectCodeStyleSettingsManager">
<option name="PER_PROJECT_SETTINGS">
<value>
<option name="USE_SAME_INDENTS" value="true" />
<option name="IGNORE_SAME_INDENTS_FOR_LANGUAGES" value="true" />
<option name="OTHER_INDENT_OPTIONS">
<value>
<option name="INDENT_SIZE" value="2" />
lazy val flyway = new Flyway {
setDataSource(dataSource)
}
def migrateToLatestDbVersion() {
try flyway.migrate() catch {
case e: FlywayException if e.getMessage.startsWith("Unable to check whether schema") =>
flyway.init()
flyway.migrate()
}
case class Coordinates(x: Double, y: Double)
object HaversineDistance {
def apply(a: Coordinates, b: Coordinates): Double = {
val deltaLat = math.toRadians(b.x - a.x)
val deltaLong = math.toRadians(b.y - a.y)
val x = math.pow(math.sin(deltaLat / 2), 2) + math.cos(math.toRadians(a.x)) * math.cos(math.toRadians(b.x)) * math.pow(math.sin(deltaLong / 2), 2)
val greatCircleDistance = 2 * math.atan2(math.sqrt(x), math.sqrt(1 - x))
3958.761 * greatCircleDistance
}
lazy val dbCache: Option[Cache] = ???
lazy val realCache: scala.concurrent.Future[Cache] = ???
def cache: Option[Cache] = {
realCache.value match {
case Some(Success(x)) => Some(x)
case None => dbCache
case Some(Failure(error)) =>
log.error(error)
dbCache
# Maven
target
# Idea
.idea/
*.iml
*.ipr
#JRebel
rebel.xml
package com.evolutiongaming.serialization
import java.nio.charset.Charset
import akka.serialization.SerializerWithStringManifest
import scala.util.control.NoStackTrace
class BrokenSerializer extends SerializerWithStringManifest {