Skip to content

Instantly share code, notes, and snippets.

View knutwalker's full-sized avatar
🦀

Paul Horn knutwalker

🦀
View GitHub Profile
@knutwalker
knutwalker / Main.hs
Created May 9, 2017 11:21
Play around with Haskell
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Applicative ((<$>), (<*>))
import Control.Monad (mzero)
import Data.Aeson
import Data.Aeson.Types (emptyObject)
import qualified Data.ByteString.Lazy.Char8 as BL
λ> enumFromThenTo 3 9 50
[3,9,15,21,27,33,39,45]
it :: (Enum a, Num a) => [a]
λ> enumFromThenTo 'A' 'C' 'I'
"ACEGI"
it :: [Char]
λ> data Foo = A | B | C | D | E | F | G | H | I deriving (Enum,Show)
λ> enumFromThenTo A C I
sealed trait Thingy[+A] {
def bla(x: String): String
val bla2: String ⇒ String = bla
}
case class Good[A](x: A) extends Thingy[A] { def bla(x: String) = x }
case class Bad(t: String) extends Thingy[Nothing] { def bla(x: String) = t }
object Thingy {
def good1[A](x: A): Thingy[A] = Good(x)
@knutwalker
knutwalker / FromMap.scala
Last active April 12, 2020 15:21
Converting Map[String,Any] to a case class using Shapeless - http://stackoverflow.com/q/31640565/2996265
/*
* Based on http://stackoverflow.com/a/31641779/2996265
*
* Changed:
* - use https://github.com/knutwalker/validation to accumulate errors
* - support lists as well (polymorphic on higher kinds would be better, though)
* - use -Xexperimental for java8 style SAM support
*/
class CreditCard {
def charge(price: Double): Unit =
println(s"side effect ${##}")
}
case class Coffee(name: String = "Venti Tall Latte with Grande Soy Frappucino Whip Cream Extra Beans") {
def price: Double = 13.37
}
import shapeless._
import shapeless.tag._
import shapeless.labelled._
import scala.annotation.implicitNotFound
object Pluck {
trait FindByName[L <: HList, K] extends DepFn1[L] {
type Out
@knutwalker
knutwalker / typed-breakage.scala
Last active October 21, 2015 08:56
Demonstrate how to break actor encapsulation with akka typed
#!/usr/bin/env scalas
// Run with conscript: http://www.scala-sbt.org/0.13/docs/Scripts.html
// Example run:
//
// $ ./typed-breakage.scala
// [state-1]: count = 21776 (should be 10000)
// [state-3]: count = 28473 (should be 10000)
// [state-2]: count = 29454 (should be 10000)
@knutwalker
knutwalker / Results.md
Last active October 15, 2015 09:19
Benchmark several stream like implementations
Benchmark Mode Cnt Score Error Units
StreamPressureBench.bench_01_transducers avgt 5 89.049 ± 5.785 ms/op
StreamPressureBench.bench_01_transducers:·compiler.time.profiled avgt 5 59.000 ms
StreamPressureBench.bench_01_transducers:·compiler.time.total avgt 5 938.000 ms
StreamPressureBench.bench_01_transducers:·gc.alloc.rate avgt 5 3426.787 ± 224.769 MB/sec
StreamPressureBench.bench_01_transducers:·gc.churn.PS_Eden_Space avgt 5 3422.665 ± 523.829 MB/sec
StreamPressureBench.bench_01_transducers:·gc.churn.PS_Survivor_Space avgt 5 0.262 ± 0.485 MB/sec
Stream
@knutwalker
knutwalker / Main.scala
Created August 25, 2015 12:31
Demonstrates various possibilities to declare a val and the resulting bytecode
/**
* Demonstrates various possibilities to declare a val and the resulting bytecode.
*
* Main takeaways are:
* - `private[this]` does not generate a getter and genertes direct field access
* - compile time constants are `final val x = e` with an optional modifier (§4.1)
* - final must be given, even if enclosing class is already final (§ 5.2 - 'final')
* - no type annotation may be present
*/
final class Main {
import shapeless._
import shapeless.labelled._
import shapeless.ops.record._
import scala.annotation.implicitNotFound
@implicitNotFound("Cannot prove that ${A} has an 'id: Int' field.")
trait HasId[A] {
def apply(a: A): Int
}