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:
I hereby claim:
To claim this, I am signing this object:
;; 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] |
(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))), |
; 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] | |
(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])) |
S = #"\s*" "a" #"\s*" |
(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" |
(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]])) |
;; gorilla-repl.fileformat = 1 | |
;; @@ | |
(ns count-combinations) | |
;; @@ | |
;; => | |
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"} | |
;; <= | |
;; ** |
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})