Skip to content

Instantly share code, notes, and snippets.

View swannodette's full-sized avatar

David Nolen swannodette

View GitHub Profile
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(set! *warn-on-reflection* true)
(ns fj
(:import [java.util.concurrent RecursiveTask
ForkJoinPool]))
(set! *warn-on-reflection* true)
;; -----------------------------------------------
;; Helpers to provide an idiomatic interface to FJ
(defprotocol IFJTask
@swannodette
swannodette / dcg.clj
Created May 18, 2011 21:28 — forked from raek/dcg.clj
Definite Clause Grammars with core.logic (aka Logos) in Clojure http://en.wikipedia.org/wiki/Definite_clause_grammar
(ns dcg
(:refer-clojure :exclude [reify == inc])
(:use (clojure.core.logic minikanren prelude)))
(declare sentence noun-phrase verb-phrase det noun verb)
(defn sentence [s1 s3]
(exist [s2]
(noun-phrase s1 s2)
(verb-phrase s2 s3)))
@swannodette
swannodette / gist:997140
Created May 28, 2011 19:26
type-inf.clj
(ns logic.y
(:refer-clojure :exclude [== reify inc])
(:use [clojure.core.logic minikanren prelude
nonrel match]))
(defna findo [x l o]
([_ [[?y :- o] . _] _]
(project [x ?y] (== (= x ?y) true)))
([_ [_ . ?c] _] (findo x ?c o)))
@swannodette
swannodette / gist:1074818
Created July 10, 2011 18:38
change.clj
(def denoms [2000 1000 500 100 25 10 5 1])
(defn make-change
([amt] (make-change amt denoms {}))
([amt denoms r]
(if (zero? amt)
r
(let [[f :as denoms] (drop-while #(> % amt) denoms)
[n amt] ((juxt quot rem) amt f)]
(recur amt denoms (assoc-in r [f] n))))))
;; (match [x y z]
;; [_ f# t#] 1
;; [f# t# _ ] 2
;; [_ _ f#] 3
;; [_ _ t#] 4)
(def pm2 (pattern-matrix [(pattern-row [wildcard (pattern false) (pattern true)] :a1)
(pattern-row [(pattern false) (pattern true) wildcard] :a2)
(pattern-row [wildcard wildcard (pattern false)] :a3)
(pattern-row [wildcard wildcard (pattern true)] :a4)]
(defne assoco [m k v o]
([[] _ _ [[k v]]])
([[[k v] . _] _ _ m])
([[[k ?v] . ?r] _ _ [[k v] . ?r]]
(!= m o))
([[[?j v]] _ _ [[?j v] [k v]]]
(!= ?j k))
([[[?j ?u] . ?r] _ _ [[?j ?u] . ?o]]
(!= ?j k)
(!= m o)
@swannodette
swannodette / gist:1102865
Created July 24, 2011 17:38
assoco.clj
(ns clojure.core.logic.assoco
(:refer-clojure :exclude [== reify inc not])
(:use [clojure.core.logic minikanren prelude match disequality]))
;; defining assoc as a pure relation
;; a good example of the non-overlapping
;; clauses property. A given map can match
;; only one of the these clauses.
(defne assoco [m k v o]
@swannodette
swannodette / Coder.scala
Created July 26, 2011 19:09
phone_code.clj
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")
private val charCode: Map[Char, Char] =
for ((digit, str) <- mnemonics; letter <- str) yield letter -> digit
(deftyped
addInteger
[Integer :> [Integer :> Integer]]
[x y]
(+ x y))
(deftyped
addDouble
[Double :> [Double :> Double]]