Skip to content

Instantly share code, notes, and snippets.

View btrofimov's full-sized avatar

Boris Trofimov btrofimov

View GitHub Profile
@btrofimov
btrofimov / build.sbt
Created December 15, 2016 11:00 — forked from seratch/build.sbt
Scala School - Testing with specs2 examples
organization := "net.seratch"
name := "sandbox"
version := "0.1"
scalaVersion := "2.9.1"
libraryDependencies ++= Seq(
"junit" % "junit" % "4.9" withSources(),
@btrofimov
btrofimov / left.scala
Created October 24, 2016 08:07 — forked from HerringtonDarkholme/left.scala
foldLeft and foldRight
def retry(noTimes: Int)(block: ⇒Future[T]): Future[T] = {
val ns: Iterator[Int] = (1 to noTimes).iterator
val attempts: Iterator[Future[T]] = ns.map(_⇒ ()⇒block)
val failed = Future.failed(new Exception)
attempts.foldLeft(failed)
((a,block) ⇒ a recoverWith { block() })
}
retry(3) { block }
= unfolds to
@btrofimov
btrofimov / ConfigInjector.scala
Last active May 16, 2016 07:09
Generic version of Config enrichment by case class without reflection #shapeless #config
import com.typesafe.config.{ConfigValueFactory, Config}
import shapeless._
import shapeless.ops.hlist.LeftFolder
import shapeless.ops.record._
/*
* case class Book(author: String, title: String, age: Option[Int] = None)
*
* val caseClass = Book("Little John","Shapeless in depth")
*
import scala.collection.mutable.{ListBuffer, ArrayBuffer}
import MatrixTraveller._
object MatrixTraveller {
type Point = (Int,Int)
type MaxScore = Int
}
@btrofimov
btrofimov / generalized version
Last active August 29, 2015 14:24
implementation of algorithm (finding 2nd max element in array)
import scala.util.{Try, Success, Failure}
import Ordering.Implicits._
object HelloWorld extends App{
def find2MaxElem[T : Ordering ](items : Seq[T]) : T = {
if(items.size<2)
throw new RuntimeException("not enough elems in collection")
var max = items(0);
var pmax = items(0)
trait MyIterator[T]{
def hasNext : Boolean
def next: T
}
class Node[T](
value1 : T,
var parent:Option[Node[T]],
var left1:Option[Node[T]],
#include <iostream>
using namespace std;
class Integer
{
private:
int value;
public:
Integer(int i): value(i)
@btrofimov
btrofimov / gist:9467430
Last active August 29, 2015 13:57
Customized BuffereInputStream without available() usage.
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* CustomImplementation of BuffereInputStream which does not use available() of nested stream at all.
* It might be reasonable and helpful for those InputStreams that does not impement or implement available with errors [yes, it is truth, one very famous IT company privodes API with error inside available(). ].
* this implementation uses O(1) RAM consuming.
*/
public class BufferedInputStream extends FilterInputStream {
@btrofimov
btrofimov / Scala_DI_Framework_Example
Last active January 2, 2016 05:59
Example of possible DI framework for Scala
class DIScope extends Enumeration{
type Value = DIScope
val PROTOTYPE,SINGLETON = Value
}
import DIScope._
object Container{
def inject[A,B](self : B, clazz: Class[A],id: Symbol) : A {}
def inject[A,B](self : B, clazz: Class[A])(implicit manifest: Manifest[A]) : A{}
package com.vast.example
import java.net.InetSocketAddress
import java.util.UUID
import java.util.concurrent.{Executors, TimeUnit}
import com.google.common.base.Splitter
import com.twitter.finagle.http.Http
import com.twitter.finagle.builder.{Server, ServerBuilder}
import com.twitter.finagle.service.TimeoutFilter
import com.twitter.finagle.{Service, SimpleFilter, GlobalRequestTimeoutException}