Skip to content

Instantly share code, notes, and snippets.

View JoolsF's full-sized avatar

Julian Fenner JoolsF

  • London
View GitHub Profile
@JoolsF
JoolsF / stream_example1.scala
Created August 3, 2016 21:56
Streams example - fibonacci and factorial
lazy val fibs: Stream[BigInt] =
BigInt(0) #::
BigInt(1) #::
fibs.zip(fibs.tail).map { n => n._1 + n._2 }
lazy val N: Stream[BigInt] = BigInt(1) #:: N.map(_ + 1)
lazy val fac: Stream[BigInt] = BigInt(1) #:: fac.zip(N).map(a => a._1 * a._2)
fibs take 10 foreach println
fac take 10 foreach println
@JoolsF
JoolsF / basic_s3_client.scala
Last active August 16, 2016 14:13
Basic s3 client example
package foo
import java.io.ByteArrayInputStream
import com.amazonaws.ClientConfiguration
import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model.{GetObjectRequest, GroupGrantee, ObjectMetadata, Permission}
import com.typesafe.config.ConfigFactory
@JoolsF
JoolsF / random_filename.scala
Created August 18, 2016 13:00
Random filename generator
import scala.util.Random
/**
* Generates a random string
* Takes an optional prefix and includes timestamp with milisecond accuracy
* To ensure uniqueness, if two strings are requested less than one millisecond apart
* a random string is appended to the end of the string of minimum length 8
*/
def getRandomFileName(prefix: String = "", i: Int = 8) = {
require(i <= 8)
@JoolsF
JoolsF / Monad1.scala
Created November 5, 2016 15:02
Very basic monad explanation
/**
* Simple Monad
*
* The key abstraction is the flatMap, which binds the computation through chaining.
* Each invocation of flatMap returns the same data structure type (but of different value),
* that serves as the input to the next command in chain. In the above snippet, flatMap
* takes as input a closure (SomeType) => List[AnotherType] and returns a List[AnotherType].
* The important point to note is that all flatMaps take the same closure type as input and
* return the same type as output. This is what "binds" the computation thread - every item
* of the sequence in the for-comprehension has to honor this same type constraint.
@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)
@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 / 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 / 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 / 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 / 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 {