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
(use '[clojure.core.match :only [match]]) | |
(defn evaluate [env [sym x y]] | |
(match [sym] | |
['Number] x | |
['Add] (+ (evaluate env x) (evaluate env y)) | |
['Multiply] (* (evaluate env x) (evaluate env y)) | |
['Variable] (env x))) | |
(def environment {"a" 3, "b" 4, "c" 5}) |
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
object test { | |
sealed trait Limited | |
sealed trait Unlimited | |
sealed trait Lim extends Limited with Unlimited | |
sealed trait Skipped | |
sealed trait Unskipped | |
sealed trait Sk extends Skipped with Unskipped |
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
/* | |
Copyright 2012-2021 Viktor Klang | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software |
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
case class Config(host: String, port: Int) { | |
def prettyPrint(prefix: String, msg: String): String = | |
List(prefix, ": ", msg, " on ", host, ":", port.toString).mkString | |
} | |
/** | |
* Passing a configuration with implicits is like | |
* working in the state monad. You can "put" a | |
* new configuration even though we don't want that | |
* to happen in this particular example. We don't |
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 scalaz.example | |
object Reader extends App { | |
/** | |
* Manual propagation of the environment (in the example, `contextRoot`.) | |
*/ | |
object Config0 { | |
def fragment1(contextRoot: String) = <a href={contextRoot + "/foo"}>foo</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
class RichOrdering[A](oa: Ordering[A]) { | |
def thenBy[B: Ordering](f: A => B): Ordering[A] = new Ordering[A] { | |
def compare(a1: A, a2: A) = oa.compare(a1, a2) match { | |
case 0 => Ordering[B].compare(f(a1), f(a2)) | |
case x => x | |
} | |
} | |
} | |
implicit def ToRichOrdering[A](oa: Ordering[A]): RichOrdering[A] = new RichOrdering(oa) |
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
case class Var[T](i: T) extends AtomicReference[T](i) { | |
def mutate(m: T => T): T = { | |
@tailrec def apply(): T = { | |
val c = get | |
val n = m(c) | |
if (compareAndSet(c, n)) n else apply() | |
} | |
apply() | |
} | |
} |
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
/** | |
* A silly example using Kleisli composition of DB operations | |
* Based on an idea from Runar Bjarnason found here: | |
* https://groups.google.com/d/msg/scala-debate/xYlUlQAnkmE/FteqYKgo2zUJ | |
* | |
* Uses Scalaz7 | |
* | |
* @author <a href="mailto:[email protected]">Jim Powers</a> | |
*/ | |
object Monadic { |
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 FK[A] extends StaticAnnotation | |
class User | |
class Checkin | |
object Main { | |
def fetch[A](id: Int @ FK[A]): A = null.asInstanceOf[A] | |
def main(args: Array[String]): Unit = { | |
val id: Int @ FK[Checkin] = 10 |
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
trait Monad[M[_]] { | |
def pure[A](a: A): M[A] | |
def flatMap[A, B](f: A => M[B]): M[A] => M[B] | |
def map[A, B](f: A => B): M[A] => M[B] = flatMap(a => pure(f(a))) | |
} | |
class Reader[I] extends Monad[({type M[O] = Function1[I, O]})#M] { | |
def pure[A](a: A): Function1[I, A] = error("todo") | |
def flatMap[A, B](f: A => Function1[I, B]): Function1[I, A] => Function1[I, B] = error("todo") | |
} |