Skip to content

Instantly share code, notes, and snippets.

View knutwalker's full-sized avatar
🦀

Paul Horn knutwalker

🦀
View GitHub Profile
@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)
import shapeless._
import shapeless.tag._
import shapeless.labelled._
import scala.annotation.implicitNotFound
object Pluck {
trait FindByName[L <: HList, K] extends DepFn1[L] {
type Out
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
}
@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
*/
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)
λ> 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
@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