Skip to content

Instantly share code, notes, and snippets.

@pbostrom
pbostrom / gist:6926519
Created October 10, 2013 22:20
max sum problem
(defn f [{:keys [mx acc]} x]
{:mx (max mx (+ acc x))
:acc (max 0 (+ acc x))})
(defn max-sum [v]
(:mx (reduce f {:mx 0 :acc 0} v)))
(max-sum [-1, 5, 6, -2, 20, -50, 4]) ;29
(max-sum [-1, 5, 6, -2, 21, -50, 4, -100, 30, -100, 5, 8, -2, 20, -50, 4]) ;31
(max-sum [-2, 1, -3, 4, -1, 2, 1, -5, 4]) ;6
@pbostrom
pbostrom / repl.txt
Last active December 28, 2015 04:09
Clojail exception
; nrepl.el 0.2.0 (Clojure 1.5.1, nREPL 0.2.1)
user> (use 'clojail.testers)
nil
user> (use 'clojail.core)
nil
user> (def mysb (sandbox secure-tester-without-def))
#'user/mysb
user> (mysb '(range 10))
(0 1 2 3 4 5 6 7 8 9)
user> (mysb '(range count))
(defn test29
"Write a function which takes a string and returns a new string
containing only the capital letters."
[f]
[(= (f "HeLlO, WoRlD!") "HLOWRD")
(empty? (f "nothing"))
(= (f "$#A(*&987Zf") "AZ")])
(test29 identity)
@pbostrom
pbostrom / .tmux.conf
Created January 22, 2014 16:31
emacs/tmux config
set-window-option -g xterm-keys on
@pbostrom
pbostrom / gist:8923613
Created February 10, 2014 20:31
emacs control arrows
(define-key input-decode-map "\e[1;5A" [C-up])
(define-key input-decode-map "\e[1;5B" [C-down])
(define-key input-decode-map "\e[1;5C" [C-right])
(define-key input-decode-map "\e[1;5D" [C-left])
@pbostrom
pbostrom / gist:9006986
Last active August 29, 2015 13:56
Clojure indentation
(defn long-func-name [x y z] :bar)
(defn baz1 []
(long-func-name (some-thing-arg1)
(some-thing-arg2) arg3))
(defn baz2 []
(long-func-name (some-thing-arg1)
(some-thing-arg2) arg3))
(ns foo)
(defprotocol Foo
(alpha [this a])
(beta [this a]))
(ns bar
(:require [foo :as f]))
(defrecord Bar []
Interval Since Last Panic Report: 4179520 sec
Panics Since Last Report: 5
Anonymous UUID: 1120CBDC-7AB8-0506-91A9-C2FD34C0D644
Tue Apr 22 10:56:08 2014
panic(cpu 2 caller 0xffffff8028d26fba): "negative open count (c, 16, 1)"@/SourceCache/xnu/xnu-2050.48.12/bsd/miscfs/specfs/spec_vnops.c:1813
Backtrace (CPU 2), Frame : Return Address
0xffffff8233a93be0 : 0xffffff8028c1d636
0xffffff8233a93c50 : 0xffffff8028d26fba
0xffffff8233a93c90 : 0xffffff8028d2be46
;; 1. top-level let
(let [state (atom 0)]
(defn do-stuff1 []
(swap! state inc))
(defn do-stuff2 []
(swap! state dec)))
;; 2. top-level atom
(def state (atom 0))
;; Don't unroll optional named arguments.
;; While it's nice for callers to not have to wrap optional named arguments in a map,
;; clearly specified configuration maps are laudable.
(release-sharks 2 :laser-beams true) ; good
(release-sharks {:laser-beams true} 2) ; better, the configuration is the least variance argument
(release-sharks 2 {:laser-beams true}) ; bad