Skip to content

Instantly share code, notes, and snippets.

View OlegIlyenko's full-sized avatar

ΘLΞG OlegIlyenko

View GitHub Profile
@OlegIlyenko
OlegIlyenko / gist:4235329
Created December 7, 2012 18:33
Type classes
package org.am.incident
object Test extends App {
def ??? = throw new IllegalStateException()
trait Conv[A, B] {
def conv(a: A): B
}
@OlegIlyenko
OlegIlyenko / SecureModule.scala
Created May 17, 2013 20:39
Fixed version of SecureModule
import scaldi.{Injector, Module, Injectable}
trait Security {
type User
def authenticate(username: String, password: String): Boolean
}
class SecuritySample extends Security {
def authenticate(username: String, password: String): Boolean = false
import scaldi.{Injectable, Module, Injector}
class UserService
class UserController(implicit inj: Injector) extends Injectable {
val userService = inject [UserService]
}
class OrderService(implicit inj: Injector) extends Injectable {
val userService = inject [UserService]
@OlegIlyenko
OlegIlyenko / same_xml.rb
Created August 14, 2013 20:19
Ruby script that checks whether 2 XML files are structurally same
#!/usr/bin/env ruby
require 'nokogiri'
require 'equivalent-xml'
xml = ARGV.map {|file_name| File.open(file_name) {|f| Nokogiri::XML(f)}}
print "Same: ", EquivalentXml.equivalent?(xml[0], xml[1])
@OlegIlyenko
OlegIlyenko / table-helper.js
Created September 24, 2013 14:25
Highlights rows and cells as well as sums clicked cells
var src = 'https:' == location.protocol ? 'https':'http', script = document.createElement('script');
script.src = src+'://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
script.onload = function () {
var $ = jQuery;
var elem = $('<div>').text("0").css({position: 'fixed', top: 0, left: 0, width: 250, height: 40, backgroundColor: '#006699', borderBottomRightRadius: 7, color: 'white', padding: 9, fontWeight: 'bold', fontSize: '30px', boxShadow: "0 0px 20px 2px black"}).appendTo(document.body);
var sum = 0;
var count = 0;
$("table td").click(function () {
var cell = $(this);
@OlegIlyenko
OlegIlyenko / NotNothing.scala
Created January 19, 2014 18:35
Advanced Type Constraints with Type Classes.
import scala.reflect.runtime.universe.TypeTag
import scala.annotation.implicitNotFound
@implicitNotFound("Sorry, type inference was unable to figure out the type. You need to provide it explicitly.")
trait NotNothing[T]
object NotNothing {
private val evidence: NotNothing[Any] = new Object with NotNothing[Any]
implicit def notNothingEvidence[T](implicit n: T =:= T): NotNothing[T] =
// First you need to create module with actor system and actor bindings in it:
// IMPORTANT: `Actor` bindings should always be providers (bound with `toProvider` method,
// IMPORTANT: which will create new instances each time it gets injected)
implicit val module = new Module {
bind [ActorSystem] to ActorSystem("MySystem")
bind [GreetPrinter] toProvider new GreetPrinter
binding toProvider new GenericPrinter
bind [PrintStream] to Console.out
@OlegIlyenko
OlegIlyenko / Parboiled2PredicateParser.scala
Created June 25, 2014 23:43
Parboiled2 Predicate Parser
import scala.annotation.switch
import org.parboiled2._
import scala.io.Source
import scala.util.{Failure, Success}
class PredicateParser(val input: ParserInput) extends Parser with CommonRules {
def And = Keyword("and")
def Or = Keyword("or")
def Not = Keyword("not")
@OlegIlyenko
OlegIlyenko / OptionsExample.scala
Created June 21, 2015 12:42
Example of akka-http server with generic OPTIONS method handling
import akka.actor.ActorSystem
import akka.stream.ActorFlowMaterializer
import akka.http.scaladsl.model.headers._
import akka.http.scaladsl.Http
import akka.http.scaladsl.server._
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.server.Directives._
object OptionsMethod extends App {
@OlegIlyenko
OlegIlyenko / DateType.scala
Created August 19, 2015 08:23
GraphQL scalar DateType implemented with sangria
case object DateCoercionViolation extends ValueCoercionViolation("Date value expected")
def parseDate(s: String) = Try(new DateTime(s, DateTimeZone.UTC)) match {
case Success(date) => Right(date)
case Failure(_) => Left(DateCoercionViolation)
}
val DateTimeType = ScalarType[DateTime]("DateTime",
coerceOutput = date => ast.StringValue(ISODateTimeFormat.dateTime().print(date)),
coerceUserInput = {