Skip to content

Instantly share code, notes, and snippets.

View diversit's full-sized avatar

Joost den Boer diversit

View GitHub Profile
@diversit
diversit / scala-type-helper.scala
Created April 21, 2017 14:34
Scala help to match an object with the argument type of another object
object TypeHelper {
import scala.reflect.runtime.universe.TypeTag
def getTypeTag[T : TypeTag](t: T): TypeTag[T] = implicitly[TypeTag[T]]
/**
* Checks if the object matches the type of the typed object.
*
* @param obj An object instance.
* @param typedObj A type object with a 'val argTypeTypeTag: TypeTag[_]' property.
@diversit
diversit / scala-puzzler-for-expression.sc
Last active March 15, 2017 08:59
Scala puzzler for-expression pitfall
/*
Using a for-expression (not comprehension!!) works different then expected!
When running with lists, see below, the 'println' is run 4 times as expected.
But when using Futures, the second failing Future is not 'run' and the result is still a success!
Only when 1st Future is a failure, the result is a failure.
BUT !!! In case of Future's the 'println' is NEVER run!! Why??
Note: this is tested using Twitter Futures. Don't know about Scala Future.
*/
@diversit
diversit / fix-osx-missing-webcam.sh
Created January 11, 2017 08:45
Fix 'webcam cannot be found in OSX'
# Run this command in terminal to get webcam back in OSX
sudo Killall VDCAssistant
@diversit
diversit / Say joke
Last active November 28, 2016 13:47
#!/bin/bash
sayJoke()
{
while [ true ]
do
sleep $((RANDOM%600+15))
curl -s http://chuck.cach.in/text | say
done
}
@diversit
diversit / TestApp.scala
Created June 14, 2016 13:58
A Finatra test app to try our different ways of handling a Scalactic Or into a proper response
package com.philips.flexc.player_runtime.rest
import com.twitter.finagle.http.{Request, Status}
import com.twitter.finatra.http.response.ResponseBuilder
import com.twitter.finatra.http.routing.HttpRouter
import com.twitter.finatra.http.{Controller, HttpServer}
import com.twitter.util.Future
import org.scalactic.{Bad, Good, Or}
/**
@diversit
diversit / create-dynamic-instance-with-varargs.scala
Created March 21, 2016 07:56
Dynamically create an instance of a class using variable arguments.
/**
* Create an instane of a class of type T with given arguments.
*
* @param args Arguments to create instance with. The order of arguments must match the constructor!
* @tparam T Type of class to create.
* @return Instance of T constructed with given arguments.
*/
def createInstanceOf[T : reflect.runtime.universe.TypeTag](args: Any*): util.Try[T] = {
import reflect.runtime.{universe => ru}
@diversit
diversit / JavaStreamToScala.scala
Created January 15, 2016 13:43
Implicit conversion of a Java8 Stream into a Scala Stream
/**
* Since using a Java8 Stream in Scala is not easy since Scala functions do not map to Java8 functions (yet, is experimental).
* While apparently using lambda's in other parts like Swing callbacks is possible, defining a java.util.function.Function[A,B]
* in Scala code does not work. Compiler keeps complaining:
* {{{
Error:(52, 10) no type parameters for method map: (x$1: java.util.function.Function[_ >: A, _ <: R])java.util.stream.Stream[R] exist so that it can be applied to arguments (java.util.function.Function[A,B])
--- because ---
argument expression's type is not compatible with formal parameter type;
found : java.util.function.Function[A,B]
required: java.util.function.Function[_ >: A, _ <: ?R]
@diversit
diversit / Slick_outout.txt
Created August 1, 2015 07:42
Slick 3 insert failure output
09:27:01.107 [main] DEBUG slick.ast.Node$.debug[32] - Assigned type Option[Int'] to node Path @599984672.id
09:27:01.160 [main] DEBUG slick.ast.Node$.debug[32] - Assigned type Option[Int'] to node Path s3.id
09:27:01.180 [main] DEBUG slick.ast.Node$.debug[32] - Assigned type Int' to node Path s2.user_id
09:27:01.181 [main] DEBUG slick.ast.Node$.debug[32] - Assigned type Int' to node Path s2.company_id
09:27:01.182 [main] DEBUG slick.ast.Node$.debug[32] - Assigned type String' to node Path s2.content_type
09:27:01.184 [main] DEBUG slick.ast.Node$.debug[32] - Assigned type java.sql.Timestamp' to node Path s2.from_date
09:27:01.185 [main] DEBUG slick.ast.Node$.debug[32] - Assigned type Int' to node Path s2.numberofdays
09:27:01.185 [main] DEBUG slick.ast.Node$.debug[32] - Assigned type java.sql.Timestamp' to node Path s2.upto_end_date
09:27:01.188 [main] DEBUG slick.ast.Node$.debug[32] - Assigned type Option[Int'] to node Path s2.created_by
09:27:01.189 [main] DEBUG slick.
@diversit
diversit / DistinctFunctionSeq.scala
Created July 29, 2015 08:26
Distinct elements of a List (or Seq) based on a property of the element
/**
* Extends a List[A] with a 'distinctOn' method to be able to distinct the elements
* of a list based on an element property.
*
* For example:
* In a List of Subscription's, we only want to send a certain Subscription.subscriber
* a message once even though the subscriber has multiple subscriptions.
* {{{
* subscriptions.distinctOn(_.subscriber) // returns List[Subscription] with unique subscribers
* }}}
@diversit
diversit / Generic-Map-Diff
Created July 8, 2015 17:52
Generic 'diff' implementation for Maps
import scalaz._
import Scalaz._
val m1 = Map(1 -> Set(2))
val m2 = Map(2 -> Set(5))
val m3 = Map(1 -> Set(3))
val tot = m1 |+| m2 |+| m3
tot.keySet -- m1.keySet
m1.keySet -- tot.keySet