Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am engelberg on github.
  • I am markengelberg (https://keybase.io/markengelberg) on keybase.
  • I have a public key whose fingerprint is C3F3 DE10 2B2E DEDA 4613 AC36 530A 864D 3A1B A35E

To claim this, I am signing this object:

@Engelberg
Engelberg / SantaClaus.clj
Created February 19, 2013 18:47
Santa Claus problem in Clojure
;; Ref-based Santa Claus solution, Written by Mark Engelberg
;; (start-simulation) to run
(def *num-elves* 10)
(def *num-reindeer* 9)
;; Santa is a ref containing either {:state :asleep} or {:state :busy, :busy-with <sequence-of-workers>}
(def santa (ref {:state :asleep}))
(defn waiting-room [capacity]
@Engelberg
Engelberg / bowling.clj
Created May 18, 2016 12:33
Bowling kata in 11 lines of clojure
(defn- sum [s] (reduce + s))
(defn score
([gs] (let [s (clojure.string/replace gs #"\d/" #(str (nth % 0) (char (- 106 (int (nth % 0)))))),
g (map #(case % \X 10 \- 0 (- (int %) 48)) s)]
(score g 1)))
([game frame]
(cond
(= frame 10) (sum game)
(= (first game) 10) (+ (sum (take 3 game)) (score (drop 1 game) (inc frame))),
(= (sum (take 2 game)) 10) (+ (sum (take 3 game)) (score (drop 2 game) (inc frame))),
@Engelberg
Engelberg / grid_classes.txt
Created December 27, 2013 02:59
Example of how classes can be thought of as a "parameterized namespace".
; This is sort of a pseudo Clojure/Scala mash-up.
; In an OO language, you might start off with the following class as a namespace
; for all your functions that revolve around manipulating 3x4 grids.
class Grid {
val rows = 3
val cols = 4
val cells = for [row in range(rows), col in range(cols)] yield [row,col]
@Engelberg
Engelberg / grid.clj
Created December 27, 2013 01:50
Why namespaces are not as good as classes
(ns mark.grid)
; I'm working with grids that are primarily 3x4.
; I get up and running with the following simple code.
(def rows 3)
(def cols 4)
(def cells (for [row (range rows), col (range cols)] [row col]))
@Engelberg
Engelberg / SingleA
Last active December 15, 2015 15:39
S = #"\s*" "a" #"\s*"
@Engelberg
Engelberg / teamsplit.clj
Created October 7, 2015 04:03
Loco model for splitting a sequence of numbers into two equally-sized parts, minimizing the disparity of their sums
(ns mark.teamsplit
(:require [loco.core :as l]
[loco.constraints :refer :all]))
; Although this problem was stated in terms of a group of players
; with multiple attributes, we can just represent each player by
; a single number, so this function just takes the list of numbers.
(defn find-teams
"Players is a list of player strengths, timeout is in milliseconds"
@Engelberg
Engelberg / handler.clj
Created April 30, 2015 01:03
Sample web app
(ns compojure-example.handler
(:require [compojure.core :refer :all]
[compojure.route :as route]
[ring.middleware.defaults :refer [wrap-defaults site-defaults api-defaults]]
[ring.util.anti-forgery :refer [anti-forgery-field]]
[ring.util.response :as response] ; for response/redirect, etc.
[hiccup.core :refer [html h]]
[hiccup.form :as form]
[hiccup.element :refer [image link-to]]
[hiccup.page :refer [html5]]))
@Engelberg
Engelberg / count-combinations.clj
Created April 16, 2015 21:00
Memoizing count-combinations
;; gorilla-repl.fileformat = 1
;; @@
(ns count-combinations)
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; **
@Engelberg
Engelberg / empty.md
Last active August 29, 2015 14:18
Empty sequences

As we've seen, seq is the mechanism in Clojure for converting a collection into something that responds to first and rest, so that we can traverse it as we would a linked list. Sequences automatically print at the REPL like a list.

=> (seq '(1 2 3 4))
(1 2 3 4)
=> (seq [1 2 3 4])
(1 2 3 4)
=> (seq #{1 2 3 4})
(1 4 3 2)
=> (seq {:a 1, :b 2})