Skip to content

Instantly share code, notes, and snippets.

View kevinmeredith's full-sized avatar

Kevin Meredith kevinmeredith

  • https://twitter.com/Gentmen
View GitHub Profile
@kevinmeredith
kevinmeredith / X.scala
Created January 23, 2017 18:00
Generic Filter?
trait Element[A, B] {
val input: A
val filteredValue: Option[B]
}
def filterFn[A, B](queryFilterValues: List[B])(implicit ev: A => Element[A, B]): List[A] => List[A] =
elements => {
queryFilterValues match {
case _ :: _ => elements.flatMap { e =>
ev(e).filteredValue match {
@kevinmeredith
kevinmeredith / X.scala
Last active January 19, 2017 03:05
Attempt to implement `Init` per shapeless-guide book
package net
import shapeless.{::, DepFn1, HList}
trait Init[L <: HList] extends DepFn1[L]
object Init {
type Aux[L <: HList, Out1] = Init[L] { type Out = Out1}
@kevinmeredith
kevinmeredith / X.scala
Created January 18, 2017 15:41
var args
scala> import scala.collection.immutable.ListMap
import scala.collection.immutable.ListMap
scala> ListMap[Int, String]( (42, "hi"), (66, "world") )
res1: scala.collection.immutable.ListMap[Int,String] = Map(42 -> hi, 66 -> world)
scala> val list: List[(Int, String)] = List( (42, "hi"), (66, "world") )
list: List[(Int, String)] = List((42,hi), (66,world))
scala> ListMap[Int, String]( list: _* )
@kevinmeredith
kevinmeredith / Json.scala
Created January 7, 2017 18:57
Generic Derivation of `JsonValue` from shapeless-book - my attempt
package net
import shapeless.{::, Generic, HList, HNil, Lazy}
sealed trait JsonValue
case class JsonObject(fields: List[(String, JsonValue)]) extends JsonValue
case class JsonArray(items: List[JsonValue]) extends JsonValue
case class JsonString(value: String) extends JsonValue
case class JsonNumber(value: Double) extends JsonValue
case class JsonBoolean(value: Boolean) extends JsonValue
@kevinmeredith
kevinmeredith / A.scala
Last active December 27, 2016 18:24
private versus private [this] in Scala 2.11
scala> :paste
// Entering paste mode (ctrl-D to finish)
class A(private [this] val x: Int)
object A {
def f(a: A) = a.x
}
// Exiting paste mode, now interpreting.
@kevinmeredith
kevinmeredith / X.scala
Created November 18, 2016 17:48
Mutual Recursion odd/even
scala> :paste
// Entering paste mode (ctrl-D to finish)
import scala.annotation.tailrec
@tailrec
def odd1(n: Int): Boolean = {
if (n == 0) false
else even1(n - 1)
}
@kevinmeredith
kevinmeredith / anorm.scala
Created November 8, 2016 14:01 — forked from davegurnell/anorm.scala
A short guide to Anorm
/*
Overview
--------
To run a query using anorm you need to do three things:
1. Connect to the database (with or without a transaction)
2. Create an instance of `anorm.SqlQuery` using the `SQL` string interpolator
3. Call one of the methods on `SqlQuery` to actually run the query
@kevinmeredith
kevinmeredith / X.scala
Created October 27, 2016 15:20
Type-class within case class
trait Reads[A]
case class Service(a: String) {
implicit val xReads = X.reads(this)
}
case class X(y: Int)
object X {
def reads(s: Service): Reads[X] = new Reads[X] {}
}
@kevinmeredith
kevinmeredith / asdfsadf.scala
Created October 19, 2016 20:59
DoorStatus ADT - using String's without `write`
sealed trait DoorStatus
case object Open extends DoorStatus
case object Closed extends DoorStatus
object DoorStatus {
val OpenShowValue = "open"
val ClosedShowValue = "closed"
def show(d: DoorStatus): String = d match {

Advanced Functional Programming with Scala - Notes

Copyright © 2017 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x