Skip to content

Instantly share code, notes, and snippets.

View swannodette's full-sized avatar

David Nolen swannodette

View GitHub Profile
@swannodette
swannodette / notes.clj
Last active July 5, 2022 13:32
Generates lists of size M containing natural numbers which add up to N
;; list comprehensions are often used as a poor man's Prolog
;; consider the following, it has only one solution
;; [1 1 1 1 1 1 1 1 1 1] yet we actually consider 10^10 possibilities
(for [a (range 1 11)
b (range 1 11)
c (range 1 11)
d (range 1 11)
e (range 1 11)
f (range 1 11)
(unifier/unify
{:as '{?x (?y ?z)}
:constraints {'?z (fnc [n] (number? n))}
'(?x ?a) '(["foo" 9] [1 2 3]))
;; previously you couldn't name '(?y ?z)
(unifier/unify
{:constraints {'?z (fnc [n] (number? n)}}
'((?y ?z) ?a) '(["foo" 9] [1 2 3]))
(ns net.cgrand.decay
"Exponentially decaying lists. See http://awelonblue.wordpress.com/2013/01/24/exponential-decay-of-history-improved/
for background and http://clj-me.cgrand.net/2013/02/12/decaying-lists-log-scale-for-lists/ for documentation")
;; PRNG, formulas straight from java.util.Random javadoc
(defn- seed ^long [^long n]
(bit-and (unchecked-multiply n 0x5DEECE66D)
(unchecked-dec (bit-shift-left 1 48))))
(defn- next-seed ^long [^long seed]
(ns logic-play.puzzle
(:refer-clojure :exclude [==])
(:use [clojure.core.logic])
(:require [clojure.tools.macro :as mu]
[clojure.set :as set]
[clojure.core.logic.fd :as fd]))
;; -----
;; CLP(Set) Boilerplate
(defn index [xs] (->> xs (map-indexed (fn [i x] [x (inc i)])) (into {})))
(ns puzzle
(:refer-clojure :exclude [==])
(:use [clojure.core.logic])
(:require [clojure.core.logic.set :as set]))
(defn existso [q ps] (fresh [x] (featurec x ps) (membero x q)))
(defn ruleo [q p v tp tv] (existso q {p v tp tv}))
(defn neg-ruleo [q p v tp tv] (fresh [x] (!= x tv)) (existso q {p v tp x}))
(defn earliero [q p v op ov]
@zmaril
zmaril / core.clj
Last active December 16, 2015 14:19
Simple rejection sampling based probabilistic programming library. An approximation of church.
(ns hacklheber.core)
(defn flip
"A function which returns true or false randomly. Can optionally be
supplied a number for a bias."
([] (> 0.5 (rand)))
([p] (> p (rand))))
(defn- memo-bangs
"If a variable is bound with a bang, then it will be memoized."
@austinhaas
austinhaas / findall.clj
Created May 9, 2013 04:54
core.logic findall sketch
(ns pettomato.findall
(:refer-clojure :exclude [==])
(:use
[clojure.core.logic.protocols])
(:require
[clojure.core.logic :refer :all]))
(defn findall
"A goal that unifies l with a lazy sequence containing all possible
instantiations of v that could be generated by applying the goal g
anonymous
anonymous / gist:5600633
Created May 17, 2013 17:33
  • remove -snapshot from version number in both package.json and project.clj
  • probably run the tests one more time to make sure all is well
  • add and commit changes with git
  • create a git tag for to-be-released version
  • push the commits and tag out to github
  • run npm publish . (may need to re/register with npmjs.org)
  • deploy to clojars (maybe?)
  • assuming all goes well, bump the version number and append -snapshot in both package.json and project.clj
@lynaghk
lynaghk / gist:5694320
Created June 2, 2013 18:06
Puzzle agent + core.logic.
(defn sublisto
"The sequence x appears in y."
[x y]
(fresh [a b c]
(appendo a x b)
(appendo b c y)))
(->> (run* [q]
(sublisto [:b :r :r :c] q)
(sublisto [:r :c :c :b] q)
@zachallaun
zachallaun / protocols.js
Last active December 19, 2015 02:19
Kinda Clojure-like protocols
// Possibly wacky and useless protocol experiment
var mapObj = function(obj, fn) {
var ret = {}
for (var k in obj) {
var temp = fn(k, obj[k])
ret[temp[0]] = temp[1]
}
return ret
}