This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn box-vert [q] | |
(let [n (* q 8)] | |
(if (< n 1) | |
\space | |
(char (+ 0x2580 (min n 8)))))) | |
(defn box-horz [q] | |
(let [n (* q 8)] | |
(if (< n 1) | |
\space |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Use a green "λ>" as prompt. (Continued lines begin with " |".) | |
:set prompt "\SOH\ESC[32;1m\STXλ>\SOH\ESC[0m\STX " | |
:set prompt2 "\SOH\ESC[32;1m\STX |\SOH\ESC[0m\STX " | |
-- Hoogle (cabal install hoogle && hoogle data) | |
:def hoogle \str -> return $ ":! hoogle search --color --count=10 " ++ show str | |
:def hoogle-all \str -> return $ ":! hoogle search --color " ++ show str | |
:def doc \str -> return $ ":! hoogle search --color --info " ++ show str | |
-- Pointfree (cabal install pointfree) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class Box<T> { | |
private let _value: () -> T | |
public init(_ value: T) { self._value = {value} } | |
public var value: T { return _value() } | |
} | |
enum Tree1<T> { | |
case Leaf(T) | |
case Left(Tree1<T>, T) | |
case Right(T, Tree1<T>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func first<S: SequenceType>(xs: S) -> S.Generator.Element? { | |
var g = xs.generate() | |
return g.next() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(fn [{:keys [states start accepts transitions]}] | |
(->> (conj clojure.lang.PersistentQueue/EMPTY {:prefix [], :pos start}) | |
(iterate (fn [q] | |
(let [[{:keys [prefix pos]} tail] ((juxt peek pop) q)] | |
(into tail (for [[alpha pos'] (transitions pos)] | |
{:prefix (conj prefix alpha), :pos pos'}))))) | |
(take-while peek) | |
(map peek) | |
(filter (comp accepts :pos)) | |
(map (comp (partial apply str) :prefix)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; This is probably either a silly idea or a foolish implementation of an okay one. | |
(defn ret* | |
[bindings & body] | |
(if-let [[k v & more] (not-empty bindings)] | |
`(let [k# ~v] | |
(if (reduced? k#) | |
k# | |
(let [~k k#] | |
~(apply ret* more body)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn get-user-from-request [request] | |
(when-let [ident (get-in request [:session ::friend/identity :current])] | |
(magic/user-by-id (get-in request [:myapp :system :db]) ident))) | |
(defn wrap-user | |
"Add user information to the request map if logged in." | |
[handler] | |
(fn [request] | |
(if-let [user (get-user-from-request request)] | |
(handler (assoc-in request [:myapp :user] user)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(fn [[_ l r]] | |
(letfn [(mirror? | |
[[lx ll lr :as l] [rx rl rr :as r]] | |
(or (not (or l r)) | |
(and (= lx rx) | |
(and (mirror? ll rr) | |
(mirror? lr rl)))))] | |
(mirror? l r))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Not quite as bad as https://twitter.com/ivansafrin/status/457248037157740544 but still hairy. | |
#include <memory> | |
#include <type_traits> | |
#include <iostream> | |
template <typename Creator, typename Destructor, typename... Arguments> | |
auto make_resource(Creator c, Destructor d, Arguments &&... args) { | |
using value_t = typename std::decay<decltype(*c(std::forward<Arguments>(args)...))>::type; | |
using ptr_t = std::unique_ptr<value_t, void (*)(value_t *)>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <memory> | |
#include <cassert> | |
template<typename T> | |
struct idx_vector { | |
typedef int index; | |
std::vector<std::unique_ptr<T>> pointers_; | |
std::vector<index> free_indices_; |