Skip to content

Instantly share code, notes, and snippets.

@ohpauleez
ohpauleez / service.clj
Last active August 29, 2015 14:03
Pedestal 0.3.0 example with SSE
;; In Pedestal 0.3.1, you won't have to use an atom.
;; You'll pass the channel into the SSE creation function (instead of it passing one back to you)
(def event-ch (atom nil))
(defn home-page
[request]
(ring-resp/response "Hello World!"))
(defroutes routes
[[["/" {:get home-page}
@ohpauleez
ohpauleez / debugrepl.clj
Last active August 29, 2015 14:17
Another debug repl
;; Simpler debug-repl based on https://github.com/stuarthalloway/circumspec/blob/master/src/clojure/contrib/debug.clj
;; This `debug-repl` also tries to capture line metadata from forms/files, as well as exceptions
;; Some people may want to rename `debug-repl` to `break`, and `quit-dr` to `continue`
(defmacro local-bindings
"Produces a map of the names of local bindings to their values."
[]
(let [symbols (map key @clojure.lang.Compiler/LOCAL_ENV)]
(zipmap (map (fn [sym] `(quote ~sym)) symbols) symbols)))
args_that_need_validation = ["one", "two", "three"]
def newStudy(**kwargs):
for key in kwargs:
if (key in args_that_need_validation) and not kwargs[key]:
raise Exception("You don't have %s" % str(key))
(defn step-seq [result n]
(if (= n 1)
result
(recur (conj result (step n))
(step n))))
(defn step-seq! [n]
(loop [result (transient [])
cnt n]
(if (= cnt 1)
@ohpauleez
ohpauleez / gist:652858
Created October 29, 2010 03:37
futures
user=> (future (+ 1 2 3))
#<core$future_call$reify__5403@d2d58b: 6>
user=> (def f *1)
#'user/f
user=> @f
6
user=> (map #(future (apply * %)) (partition-all (+ 2 num-of-cores) (range 25)))
(#<core$future_call$reify__5403@3d3c33b7: 0> #<core$future_call$reify__5403@3b6752c9: 840> #<core$future_call$reify__5403@7c6c2896: 7920> #<core$future_call$reify__5403@20dccfab: 32760> #<core$future_call$reify__5403@c5f468: 93024> #<core$future_call$reify__5403@4430d83d: 212520> #<core$future_call$reify__5403@62c4afc4: 24>)
user=> (def my-futures *1)
#'user/my-futures
(ns clopi.core
(:import (java.util.zip GZIPInputStream)
(java.io StringReader)))
(def *feed-archive-url* "http://clojars.org/repo/feed.clj.gz")
(defn gunzip
"Unzip a gzip archive into an input stream"
[archive]
(GZIPInputStream. (clojure.java.io/input-stream archive)))
@ohpauleez
ohpauleez / sub.clj
Created January 26, 2012 02:29
Picking out a subject
;; This is O(n*m) for all cases, where n is the length of a, and m is the length of b
user=> a
"8th grade biology"
user=> b
["chemistry" "biology" "algebra"]
user=> (def s (cstr/split a #" "))
#'user/s
user=> s
["8th" "grade" "biology"]
user=> (for [w b
<?php namespace cacher;
class CacheMixin
{
public static $default_cache_settings = array();
public static function __callStatic($name, $args)
{
echo "\nHello, $name\n";
$cache_settings = static::$default_cache_settings;
@ohpauleez
ohpauleez / leiningen_defaults.md
Created February 20, 2012 22:37
Examples of usering uberclj

How to make Leiningen use uberclj for all commands

  1. Fetch the latest: wget http://www.pauldee.org/uberclj/uberclj-1.4.0-latest.jar; mv uberclj-1.4.0-latest.jar ~/.lein

  2. Update your lein bash script from:

    CLOJURE_JAR="$HOME/.m2/repository/org/clojure/clojure/1.2.1/clojure-1.2.1.jar"

To

(comment
Given some rules like:
{(if ?x ?y nil) (when ?x ?y)
(when true . ?x) (do . ?x)}
And the `unify` and `check-form` functions below,
I would like to apply `unify` until I `unify` returns nil.