Skip to content

Instantly share code, notes, and snippets.

View holograph's full-sized avatar

Tomer Gabel holograph

View GitHub Profile
@holograph
holograph / Stateful.scala
Created March 4, 2020 17:56
Accord example showing stateful validators
package com.wix.accord.examples
class Stateful(allowedNames: Set[String]) {
import com.wix.accord.dsl._
import com.wix.accord._
private implicit val classroomValidator = validator[Classroom] { c =>
c.teacher.name is in(allowedNames)
c.students.map(_.name).each is in(allowedNames)
@holograph
holograph / A.java
Created October 14, 2018 08:48
Showcasing Java method references
import java.util.function.Supplier;
class A {
static int staticSupplier() { return 5; }
int instanceSupplier() { return 10; }
static void printOut(Supplier<Integer> supplier) {
System.out.println(supplier.get());
}
@holograph
holograph / ClassWithLazyLogs.java
Last active June 8, 2020 19:29
Java 8 vs Scala
public class ClassWithLazyLogs implements LazyLogging {
public String getNormalizedName(Person person) {
info(() -> "getNormalizedName called");
debug(() -> "Normalizing " + person.toString());
String normalizedName =
person.getName().toUpperCase().trim();
debug(() -> "Normalized name is: " + normalizedName);
return normalizedName;
}
}
@holograph
holograph / repl.out
Created December 17, 2014 09:09
Games with path-dependent types, singletons and type projections
arilou:accord tomer$ scala
Welcome to Scala version 2.11.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_05).
Type in expressions to have them evaluated.
Type :help for more information.
scala> trait A { case object B }
defined trait A
scala> def m[T <: A](x: T): T#B = x.B
<console>:8: error: type B is not a member of type parameter T
@holograph
holograph / ValidatorExecutionSample.scala
Created December 11, 2013 15:33
A sample Accord validator execution (https://github.com/wix/accord).
scala> val validPerson = Person( "Wernher", "von Braun" )
validPerson: Person = Person(Wernher,von Braun)
scala> validate( validPerson )
res0: com.wix.accord.Result = Success
scala> val invalidPerson = Person( "", "No First Name" )
invalidPerson: Person = Person(,No First Name)
scala> validate( invalidPerson )
@holograph
holograph / ValidatorDefinitionSample.scala
Last active December 31, 2015 00:59
A sample validator definition for Accord (https://github.com/wix/accord).
import com.wix.accord.dsl._ // Import the validator DSL
case class Person( firstName: String, lastName: String )
case class Classroom( teacher: Person, students: Seq[ Person ] )
implicit val personValidator = validator[ Person ] { p =>
p.firstName is notEmpty // The expression being validated is resolved automatically, see below
p.lastName as "last name" is notEmpty // You can also explicitly describe the expression being validated
}
@holograph
holograph / gist:5945358
Created July 7, 2013 23:22
Quick-and-dirty Git log parser. This specific example parses logs with stats output (i.e. git log --numstat) and was used to generate the codebase evolution graph in http://www.slideshare.net/holograph/scala-in-practice-12803578
package com.tomergabel
import scala.util.parsing.combinator.RegexParsers
import java.text.SimpleDateFormat
import java.util.Date
import scala.util.parsing.input.Reader
import java.io.{InputStreamReader, BufferedReader, FileInputStream}
object StatLogProcessor {
case class Commit( hash: String, author: String, timestamp: Date, message: String, locs: Map[ String, Int ] )
@holograph
holograph / Test.scala
Created February 12, 2013 14:00
Reduced repro case for Scala 2.10 compiler issue, issue link to be to added.
/*
Reproduction scenario for Scala 2.10 compiler issue.
To compile: scalac Test.scala
To execute:
wanamingo:tmp tomer$ scala com.tomergabel.examples.Test
java.lang.ClassFormatError: Duplicate method name&signature in class file com/tomergabel/examples/Derived$$anonfun$defect$1
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
@holograph
holograph / ChainedEnumSerializer.scala
Created December 18, 2012 15:22
A workaround for the Lift-JSON bug documented here: https://github.com/lift/framework/issues/1080 Basically works by providing a single serializer for multiple enumerations, the order of which determines behavior in case of value collisions. Forked from a gist I accidentally created anonymously.
package com.tomergabel.examples
import net.liftweb.json._
import net.liftweb.json.JsonDSL._
/**
* A chained object which provides JSON serialization/deserialization facilities for multiple enumerations.
* Intended as a workaround for the following issue: https://github.com/lift/framework/issues/1080
*
* To use simply add to your formats: `implicit val formats = DefaultFormats + new ChainedEnumSerializer( enum1, enum2 )`
@holograph
holograph / AnotherSquerylIssue.scala
Created August 19, 2012 08:53
Followup sample code for SCL-4559
package com.tomergabel.examples
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.{Session, SessionFactory, KeyedEntity, Schema}
import java.sql.Timestamp
/**
* Created by tomer on 7/2/12.
*/