This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data Exp r = Const r | Add (Exp r) (Exp r) | Subtract (Exp r) (Exp r) | |
eval :: (Num r) => Exp r -> r | |
eval (Const v) = v | |
eval (Add e1 e2) = eval e1 + eval e2 | |
eval (Subtract e1 e2) = eval e1 - eval e2 | |
reify:: (Show r) => Exp r -> String | |
reify (Const v) = show v | |
reify (Add e1 e2) = concat ["(", reify e1, " + ", reify e2, ")"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.class public Calculator | |
.super java/lang/Object | |
.method public <init>()V | |
aload_0 | |
invokespecial java/lang/Object/<init>()V | |
return | |
.end method | |
.method public add(II)I |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shapeless._ | |
import HList._ | |
object TestEquiv { | |
case class Example[A, B](a: A, b: Seq[B], c: Int) | |
trait Equiv[T] { | |
def equiv(a: T, b: T): Boolean | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def takeWhile[T](l: List[T])(p: T => Boolean) = { | |
class AccException(val acc: List[T]) extends RuntimeException {} | |
try { | |
l.foldLeft(List.empty[T]) { (acc, x) => | |
if(p(x)) { | |
println(x) | |
x :: acc | |
} | |
else throw new AccException(acc) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shapeless._ | |
import HList._ | |
object TestEquiv { | |
case class Example[A, B](a: A, b: Seq[B], c: Int) | |
trait Equiv[T] { | |
def equiv(a: T, b: T): Boolean | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 自然数の比較 | |
import Nat._ | |
// expression of A < B | |
trait LT[A <: Nat, B <: Nat] | |
object LT { | |
type <[A <: Nat, B <: Nat] = LT[A, B] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Natで構成されたHListから、あるA以下の値でできたHListを抽出 | |
import LT._ | |
import LTEq._ | |
import HListImpl._ | |
/** | |
* HとAを受け取る。 フィルターされたA以下の型はOutに入る | |
* @tparam H フィルターの対象となるHList | |
* @tparam A フィルター基準値 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad | |
-- list | |
:{ | |
do | |
x <- [1,2,3] `mplus` [4,5,6] | |
return x -- [1, 2, 3, 4, 5, 6] | |
:} | |
:{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Deriving | |
// (だいたい)任意のcase classをマクロでtupleに変換 | |
object DerivingTest extends App { | |
// テスト用ケースクラス | |
case class Bar() | |
case class Foo(i: Int, s: String, d: Double) | |
case class Baz(i: Int, s: String, d: Double, f: Foo) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package TypeDynamic | |
import scala.language.experimental.macros | |
import scala.reflect.macros.whitebox | |
import scala.reflect.macros.whitebox.Context | |
object TypeDynamic { | |
class Example() extends Dynamic { | |
val value: Int = 2 | |
def selectDynamic(name: String): Int = macro selectDynamicImpl |