Skip to content

Instantly share code, notes, and snippets.

Paul Horn knutwalker

Block or report user

Report or block knutwalker

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@knutwalker
knutwalker / Main.hs
Created May 9, 2017
Play around with Haskell
View Main.hs
{-# 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
View enum.hs
λ> 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
View Blippy.scala
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 Apr 10, 2019
Converting Map[String,Any] to a case class using Shapeless - http://stackoverflow.com/q/31640565/2996265
View FromMap.scala
/*
* 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
*/
View basic.scala
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
}
View Pluck.scala
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 Oct 21, 2015
Demonstrate how to break actor encapsulation with akka typed
View typed-breakage.scala
#!/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 Oct 15, 2015
Benchmark several stream like implementations
View Results.md
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 Aug 25, 2015
Demonstrates various possibilities to declare a val and the resulting bytecode
View Main.scala
/**
* 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 {
View HasId.scala
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
}
You can’t perform that action at this time.