This file contains 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
;; | |
;; NS CHEATSHEET | |
;; | |
;; * :require makes functions available with a namespace prefix | |
;; and optionally can refer functions to the current ns. | |
;; | |
;; * :import refers Java classes to the current namespace. | |
;; | |
;; * :refer-clojure affects availability of built-in (clojure.core) | |
;; functions. |
This file contains 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
(defmacro letrec [bindings & body] | |
(let [bcnt (quot (count bindings) 2) | |
arrs (gensym "bindings_array") | |
arrv `(make-array Object ~bcnt) | |
bprs (partition 2 bindings) | |
bssl (map first bprs) | |
bsss (set bssl) | |
bexs (map second bprs) | |
arrm (zipmap bssl (range bcnt)) | |
btes (map #(walk/prewalk (fn [f] |
This file contains 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 math | |
import random | |
class Qubit: | |
def __init__(self, a = 1, b = 0): | |
'''Initialize a single qubit''' | |
self.zero = complex(a) | |
self.one = complex(b) | |
def xgate(self): |
This file contains 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 Enum { //DIY enum type | |
import java.util.concurrent.atomic.AtomicReference //Concurrency paranoia | |
type EnumVal <: Value //This is a type that needs to be found in the implementing class | |
private val _values = new AtomicReference(Vector[EnumVal]()) //Stores our enum values | |
//Adds an EnumVal to our storage, uses CCAS to make sure it's thread safe, returns the ordinal | |
private final def addEnumVal(newVal: EnumVal): Int = { import _values.{get, compareAndSet => CAS} | |
val oldVec = get |
This file contains 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 MyOption[+A] { | |
// single abstract method | |
def cata[X](some: A => X, none: => X): X | |
import MyOption._ | |
def map[B](f: A => B): MyOption[B] = cata(f andThen some, none) | |
// Also | |
// def map[B](f: A => B): MyOption[B] = flatMap(f andThen some) |
This file contains 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 scalaz._ | |
import Scalaz._ | |
object MonadTransformerExamples { | |
def main(args: Array[String]) = run | |
def run { | |
// ------------------------------------------------------ | |
// Combined Option/Option | |
// ------------------------------------------------------ |
This file contains 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 demo | |
class Coder(words: List[String]) { | |
private val mnemonics = Map( | |
'2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL", | |
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ") | |
/** Invert the mnemonics map to give a map from chars 'A' ... 'Z' to '2' ... '9' */ | |
private val charCode: Map[Char, Char] = |
This file contains 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 scalaz._ | |
import Scalaz._ | |
import effects._ | |
object ExperimentingWithIo { | |
def main(args: Array[String]): Unit = { | |
val i1: IO[Unit] = for { | |
_ <- putStrLn("What's your name?") |
This file contains 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 unexpected : Nothing = sys.error("Unexpected invocation") | |
// Encoding for "A is not a subtype of B" | |
trait <:!<[A, B] | |
// Uses ambiguity to rule out the cases we're trying to exclude | |
implicit def nsub[A, B] : A <:!< B = null | |
implicit def nsubAmbig1[A, B >: A] : A <:!< B = unexpected | |
implicit def nsubAmbig2[A, B >: A] : A <:!< B = unexpected |
This file contains 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> |
OlderNewer