Skip to content

Instantly share code, notes, and snippets.

@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 / Logic1.clj
Created March 7, 2013 05:49
Solving logic puzzle with for comprehension
(defn solve-logic-puzzle []
(let [people [:amaya :bailey :jamari :jason :landon]]
(first
(for [[fortune time cosmopolitan us-weekly vogue] (permutations people) ; magazines
[asiago blue-cheese mascarpone mozzarella muenster] (permutations people) ; cheeses
; We bind the reservations in two steps, so we have a name for the overall order
reservations (permutations people)
:let [[five six seven seven-thirty eight-thirty] reservations]
; THE CONSTRAINTS IN PLAIN ENGLISH
@Engelberg
Engelberg / Logic2.clj
Created March 7, 2013 05:52
Solving logic puzzle with rules in more efficient order
(defn logic-puzzle []
(let [people [:amaya :bailey :jamari :jason :landon]]
(for [[fortune time cosmopolitan us-weekly vogue] (permutations people) ; magazines
:when (not= fortune :jamari)
[asiago blue-cheese mascarpone mozzarella muenster] (permutations people) ; cheeses
:when (= blue-cheese fortune)
:when (not= muenster vogue)
reservations (permutations people)
@Engelberg
Engelberg / SingleA
Last active December 15, 2015 15:39
S = #"\s*" "a" #"\s*"
@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 / 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 / grants.clj
Created March 5, 2014 23:45
Optimizing allocation with loco
(ns mark.loco.grants
(:use loco.core loco.constraints))
; Use whatever scoring function you want for the various
; applicants, but ultimately, you want to assemble this
; information into a data structure.
(def applicants
[{:name "Alex", :score 5, :grant-request 120}
{:name "David", :score 4, :grant-request 100}
@Engelberg
Engelberg / grants2.clj
Created March 6, 2014 09:22
Non-linear version of grants problem
(ns mark.loco.grants2
(:use loco.core loco.constraints))
; Use whatever scoring function you want for the various
; applicants, but ultimately, you want to assemble this
; information into a data structure.
(def applicants
[{:name "Alex", :score 5, :grant-request 120}
{:name "David", :score 4, :grant-request 100}
@Engelberg
Engelberg / Frequencies.md
Last active March 22, 2018 22:06
An analysis of different methods for implementing frequencies in Clojure

First, here is the cleverly concise version of freqs that I showed in class, which builds a bunch of maps that map each element to the number 1, and then merges them together using +:

(defn freqs [l]
  (apply merge-with + (for [item l] {item 1})))

Here is one way of coding it using loop-recur:

@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})