Skip to content

Instantly share code, notes, and snippets.

View sptz45's full-sized avatar
🏠
Working from home

Spiros Tzavellas sptz45

🏠
Working from home
View GitHub Profile
@cvogt
cvogt / gist:9519186
Created March 12, 2014 23:53
Nested entity mapping with Slick 1.x
case class Part1(i1: Int, i2: String)
case class Part2(i1: String, i2: Int)
case class Whole(p1: Part1, p2: Part2)
class Tuple2Mapper[Entity,Component1,Tuple1,Component2,Tuple2](
entityFactory: (Component1,Component2) => Entity,
entityExtractor: Entity => Option[(Component1,Component2)],
component1factory: Tuple1 => Component1,
component1extractor: Component1 => Option[Tuple1],
component2factory: Tuple2 => Component2,
component2extractor: Component2 => Option[Tuple2]
@kabutz
kabutz / gist:13ee4e22fc796a469d06
Created May 5, 2014 08:03
How to validate a Greek Tax Number (AFM)
// From http://www.gsis.gr/gsis/info/gsis_site/Services/Polites/sources.html
// gr.gsis.e7.E7File class
// Apache 2.0 license
public static boolean afmIsValid(String afm) {
int sum = 0;
if (afm.length() != 9)
return false;
for (int i = 0; i < 8; i++)
@propensive
propensive / outcomes.scala
Last active August 29, 2015 14:15
Using TMaps for error handling
Welcome to Scala version 2.10.4 (OpenJDK 64-Bit Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.
// Import the Rapture modules we need
scala> import rapture._, uri._, codec._, io._, fs._, core._, csv._
import rapture._
import uri._
import codec._
import io._
@etorreborre
etorreborre / gist:730097
Created December 6, 2010 10:17
An implicit pearl in Scala
/**
* An answer from Jason Zaugg on the scala-internals mailing list
*
* Implicit parameters can have defaults.
*/
scala> implicit def OptionalImplicit[A <: AnyRef](implicit a: A = null) = Option(a)
OptionalImplicit: [A <: AnyRef](implicit a: A)Option[A]
scala> implicitly[Option[Ordering[Int]]]
@jorgeortiz85
jorgeortiz85 / DynamicImpl.scala
Created January 17, 2011 20:16
Method calls & XML traversal with Scala's new Dynamic type
class DynamicImpl(x: AnyRef) extends Dynamic {
def _select_(name: String): DynamicImpl = {
new DynamicImpl(x.getClass.getMethod(name).invoke(x))
}
def _invoke_(name: String)(args: Any*) = {
new DynamicImpl(x.getClass.getMethod(name, args.map(_.asInstanceOf[AnyRef].getClass) : _*).invoke(x, args.map(_.asInstanceOf[AnyRef]) : _*))
}
override def typed[T] = x.asInstanceOf[T]
override def toString = "Dynamic(" + x.toString + ")"
}
scala> def isNumeric[N](implicit ev: Numeric[N] = null): Boolean = ev != null
isNumeric: [N](implicit ev: Numeric[N])Boolean
scala> isNumeric[String]
res2: Boolean = false
scala> isNumeric[Int]
res3: Boolean = true
@milessabin
milessabin / gist:2473113
Created April 23, 2012 19:03
Default argument values only need to typecheck if they're needed.
scala> implicit val i = 23
i: Int = 23
scala> def foo[T](t : T)(implicit ev : T = "foo") = ev
foo: [T](t: T)(implicit ev: T)T
scala> foo(0)
res6: Int = 23
scala> foo("bar")
@sadache
sadache / funky.scala
Created May 16, 2012 19:47 — forked from jto/funky.scala
Funky enumerator usage
package controllers
import play.api._
import play.api.mvc._
import play.api.libs.ws._
import play.api.libs.iteratee._
import play.api.libs.concurrent._
object Application extends Controller {
@sadache
sadache / Application.scala
Created May 17, 2012 08:26
Play2: Stream results of parallel jobs as comet to the client
package controllers
import play.api._
import play.api.mvc._
object Application extends Controller {
def index = Action {
@sadache
sadache / gist:2939230
Created June 15, 2012 23:37
Parsing progressively a csv like file with Play2 and Iteratees

If your csv doesn't contain escaped newlines then it is pretty easy to do a progressive parsing without putting the whole file into memory. The iteratee library comes with a method search inside play.api.libs.iteratee.Parsing :

def search (needle: Array[Byte]): Enumeratee[Array[Byte], MatchInfo[Array[Byte]]]

which will partition your stream into Matched[Array[Byte]] and Unmatched[Array[Byte]]

Then you can combine a first iteratee that takes a header and another that will fold into the umatched results. This should look like the following code:

// break at each match and concat unmatches and drop the last received element (the match)