Skip to content

Instantly share code, notes, and snippets.

@bmabey
bmabey / Correlated traces in PyMC3.ipynb
Last active August 29, 2015 14:05
Correlated traces in PyMC3 IPython notebook
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bmabey
bmabey / challenge.clj
Last active August 29, 2015 13:57
async challenge from Philipp Haller's presenation http://www.infoq.com/presentations/rx-async using clojure's core.async
(ns challenge
" via http://www.infoq.com/presentations/rx-async
Two input streams with the following values:
stream1: 7, 1, 0, 2, 3, 1, ...
stream2: 0, 7, 0, 4, 6, 5, ...
Task:
Create a new output stream that
• yields, for each value of stream1, the sum of the previous 3
• values of stream1,except if the sum is greater than some threshold in
which case the next value of stream2 should be subtracted.
@bmabey
bmabey / simple.clj
Last active December 29, 2015 18:59
(ns simple-sim
(require [clojure.core.async :as async]
[clojure.tools.logging :as log]
criterium.core
[com.benmabey.clj-sim :as sim]))
;; Andreas' code
(defn event [env type val]
(let [rc (async/chan)]
@bmabey
bmabey / async.clj
Last active December 25, 2015 02:39
(ns rbl.utils.async
(:require [clojure.core.async :refer :all]
[clojure.tools.macro :refer [macrolet]]))
(defn batch-until-pause
"Batches values from the `in` channel into a single a vec of values up until no
messages have appeared in `timeout-pause` milliseconds. `max-batch-time` is the
number of milliseconds to wait overall before sending a batch."
([timeout-pause max-batch-time in]
(batch-until-pause timeout-pause max-batch-time in (chan)))
(ns schema-helpers
(:require [schema.core :as s]))
(defn tuple
"Returns a schema for a sequence containing exactly the schema/name pairs.
To denote an optional element use {schema name} instead of [schema name].
Example:
(def t (tuple [String 'foo] [s/Keyword 'bar] {Number 'num}))
(ns rbl.utils.async
(:require [clojure.core.async :refer :all]))
(defmacro sleep
([ms & stop-channels]
(let [to (list 'timeout ms)]
`(alts! ~(into [to] stop-channels))))
([ms]
`(<! (timeout ~ms))))
@bmabey
bmabey / sandbox.clj
Last active June 17, 2016 19:36
macros to stop a loop and timeouts in core.async
(require '[clojure.core.async :as async :refer :all])
(defmacro sleep
([ms stop-channel]
`(alts! [(timeout ~ms) ~stop-channel]))
([ms]
`(<! (timeout ~ms))))
(defmacro go-loop [bindings & body]
(let [stop (first bindings)]
@bmabey
bmabey / default_maps.clj
Last active February 14, 2023 18:02
Maps with default values in Clojure
(ns default-maps
"Maps with default values, similar to how you can specify default values and blocks to Ruby's Hash.
Examples:
user> (def b (assoc-default {:foo 34 :bar 34} (fn [_ k] (str k))))
#'user/b
user> (get b :foo-bar)
\":foo-bar\"
user> (def m (assoc-default {:foo 34 :bar 34} 343))
@bmabey
bmabey / sqlmagic.py
Last active December 11, 2015 10:18
Simple cell and line IPython Notebook magics for connecting to a ODBC DB and querying
from IPython.core.magic import (Magics, magics_class, line_magic,
cell_magic, line_cell_magic)
import pyodbc
import pandas.io.sql as sql
@magics_class
class SQLMagic(Magics):
con = None
@bmabey
bmabey / user_agent.clj
Created October 24, 2012 15:55
Example of using the java lib UserAgentUtils in clojure to parse user agent strings
(ns rbl.feature-extraction.user-agent
(:use [clojure.core.memoize :only [memo]])
(:require [clojure.tools.logging :as log])
(:import [nl.bitwalker.useragentutils UserAgent DeviceType Browser OperatingSystem]))
(defn str->features [string]
(try
(let [user-agent (UserAgent. (or string ""))]
{:browser_group (-> user-agent .getBrowser .getGroup .getName)
:os_group (-> user-agent .getOperatingSystem .getGroup .getName)