Skip to content

Instantly share code, notes, and snippets.

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