Skip to content

Instantly share code, notes, and snippets.

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