Skip to content

Instantly share code, notes, and snippets.

View JoolsF's full-sized avatar

Julian Fenner JoolsF

  • London
View GitHub Profile
@JoolsF
JoolsF / cache_example1.scala
Created May 12, 2017 16:08
Scala cache using Guava. Simple example.
import scalacache._
import guava._
import memoization._
import com.google.common.cache.CacheBuilder
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
@JoolsF
JoolsF / db_concat_script1.scala
Last active April 27, 2017 10:48
Simple concat script to create an SQL update statement
/**
* Creates an update statement for a SQL db table from a type where some or all of the values might be None
* If all the values are None will return empty string else it will create the appropriate strings for the Some values
*
* This could be much improved and generalised but its just a simple example. Useful when using db libraries
* where pure SQL is used.
*/
case class Person(name: Option[String],
age: Option[Int])
@JoolsF
JoolsF / sortBy_examples.scala
Created March 9, 2017 11:12
Sorting / sortBy examples
/********************
* sortBy Example 1
********************/
case class Person(name: String, age: Int)
val people = List(Person("Julian", 35), Person("David", 25), Person("Linda", 40))
//ascending
people.sortBy(_.age) //List(Person("David", 25), Person("Julian", 35), Person("Linda", 40))
@JoolsF
JoolsF / listCount.scala
Created January 27, 2017 15:09
List count - counts items in a list
/**
* Takes a list of A and returns a map of A -> Int where Int represents occurences of A
*/
def listCount[A](list: List[A]): Map[A, Int] =
list.foldLeft(Map[A,Int] ()) { (a,b) =>
a.get(b).fold(a + (b -> 1)) (count => a + (b -> (count + 1)))
}
@JoolsF
JoolsF / path_dependent_types_example1.scala
Created November 26, 2016 15:59
Path dependent types example 1
/**
* One way to help the compiler prevent you from introducing bugs. It places
* log that is usually only available at runtime into bugs.
*
* In scala a nested type is bound to the specific instance of the outer type
* and not the type itself
*
*/
class MusicGenre {
@JoolsF
JoolsF / type_class_example1.scala
Last active November 26, 2016 15:30
Type class example 1
/**
* Any developer can declare that a type is a member of a type class simply by providing implementations of the
* operations the type must support. Now, once T is made a member of the type class C, functions that have constrained
* one or more of their parameters to be members of C can be called with arguments of type T.
*
* As such, type classes allow ad-hoc and retroactive polymorphism. Code that relies on type classes is open to extension
* without the need to create adapter objects.
*
* They also allow program design that is open for extension without giving up important information about concrete types
*/
@JoolsF
JoolsF / cats_xor_example1.scala
Created November 24, 2016 16:07
Cat xor example 1
import cats.data.Xor
class Boom(msg:String) extends Error(msg)
def boomXorUnit(blowUp: Boolean): Boom Xor Unit =
if(blowUp) Xor.Left(new Boom("kaboom"))
else new Xor.Right(())
val left = boomXorUnit(true)
val right = boomXorUnit(false)
@JoolsF
JoolsF / closure_example1.scala
Created November 24, 2016 10:54
Scala closure basics
/**
* A closure is a function, whose return value depends on the value of one or more variables
* declared outside this function.
*/
/*
* Example 1
* closureIdentity has a reference to the variable i outside the function but in the enclosing scope.
* The function references factor and reads its current value each time.
*/
var i = 1
@JoolsF
JoolsF / methodToFunction1.scala
Created November 19, 2016 17:09
Method to function example
/**
A Scala method, as in Java, is a part of a class. It has a name, a signature,
optionally some annotations, and some bytecode.
A function in Scala is a complete object.
*/
def sizeConstraint(pred: IntPairPred, n: Int, text: String): Boolean =
pred(text.size, n)
@JoolsF
JoolsF / either_basics.scala
Created November 19, 2016 15:53
Either basics
import scala.util.{Failure, Success, Try}
/**
* Basics
*/
class OddNumberException extends Exception
def doubleEvenNumbers(x: Int): Either[OddNumberException, Int] =
if (x % 2 != 0) Left(new OddNumberException)
else Right(x * 2)