Skip to content

Instantly share code, notes, and snippets.

@ghoseb
ghoseb / ns-cheatsheet.clj
Last active May 20, 2024 13:01 — forked from alandipert/ns-cheatsheet.clj
Clojure ns syntax cheat-sheet
;;
;; 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.
@michalmarczyk
michalmarczyk / letrec.clj
Created July 23, 2010 01:21
a letrec macro for Clojure
(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]
@limitedmage
limitedmage / qubit.py
Created April 27, 2011 23:34
Python Quantum Computing simulator
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):
@viktorklang
viktorklang / ScalaEnum.scala
Created June 30, 2011 23:12
DIY Scala Enums (with optional exhaustiveness checking)
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
@debasishg
debasishg / gist:1059864
Created July 2, 2011 08:37
Tony Morris' Option using catamorphism
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)
@krasserm
krasserm / MonadTransformerExamples.scala
Created July 14, 2011 10:32
Scalaz 7 monad transformer examples
import scalaz._
import Scalaz._
object MonadTransformerExamples {
def main(args: Array[String]) = run
def run {
// ------------------------------------------------------
// Combined Option/Option
// ------------------------------------------------------
@retronym
retronym / Coder.scala
Created August 20, 2011 07:25
Martin Odersky's Phone Coder example
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] =
@missingfaktor
missingfaktor / ExperimentingWithIo.scala
Created September 10, 2011 13:53
Playing with Scalaz effects
import scalaz._
import Scalaz._
import effects._
object ExperimentingWithIo {
def main(args: Array[String]): Unit = {
val i1: IO[Unit] = for {
_ <- putStrLn("What's your name?")
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
@retronym
retronym / config.scala
Last active May 9, 2018 05:47
Styles of config propagation: Manual, Implicits, DynamicVariable, Reader
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>