Skip to content

Instantly share code, notes, and snippets.

@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 / 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))),
(ns utils.cond
"A collection of variations on Clojure's core macros. Let's see which features
end up being useful."
{:author "Christophe Grand"}
(:refer-clojure :exclude [cond when-let if-let]))
(defmacro if-let
"A variation on if-let where all the exprs in the bindings vector must be true.
Also supports :let."
([bindings then]

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 / core.clj
Last active January 5, 2017 09:30
Twenty four using partitions
(ns twentyfour.core
(:use clojure.math.combinatorics))
(def ops ['+ '- '* '/])
(def commutative #{'+ '*})
;; We can generate all the possible expressions efficiently with combinatorics' partitions
;; partitions automatically handles duplicates for us, which keeps the process efficient
(defn expressions [nums]
@Engelberg
Engelberg / wordlist.txt
Created February 21, 2017 07:30
Esperanto wordlist from Baza Radikaro with unique 3-letter prefixes
abelo
abio
abomeno
abrikoto
absoluta
abunda
acida
adapti
adicii
adjektivo
@Engelberg
Engelberg / cond-example.clj
Created April 3, 2017 06:07
refactoring with better-cond
; The sample to refactor
(if-let [x (foo)]
(if-let [y (bar x)]
(if-let [z (goo x y)]
(do
(qux x y z)
(log "it worked")
true)
(do
(log "goo failed")
@Engelberg
Engelberg / core_subset.clj
Created August 2, 2018 23:06
Comparison Clojure to Java
public class CoreSubsetObjective implements Objective<SubsetSolution, CoreSubsetData>{
/**
* Evaluates the given subset solution using the underlying data, by computing the average
* distance between all pairs of selected items. If less than two items are selected,
* the evaluation is defined to have a value of 0.0.
*
* @param solution subset solution
* @param data core subset data
* @return evaluation with a value set to the average distance between all pairs of selected items;