Simply put, destructuring in Clojure is a way extract values from a datastructure and bind them to symbols, without having to explicitly traverse the datstructure. It allows for elegant and concise Clojure code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Monads | |
class Maybe | |
def self.[](value) | |
self.new(value) | |
end | |
def initialize(value) | |
@value = value | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Pipe | |
def self.[] value | |
self.new value | |
end | |
def initialize value | |
@value = value | |
end | |
def >> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Monads | |
class Box | |
def self.[](value) | |
self.new(value) | |
end | |
def initialize(value) | |
@value = value | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; inspired by example from "Clojure Applied" | |
; book says that it's not possible | |
(ns sales.core) | |
(defprotocol Cost | |
(cost [this])) | |
(defrecord Product [price quantity] | |
Cost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn make-transitions [& states] | |
(let [statemap | |
(zipmap | |
(map str states) | |
(rest (conj (vec states) (first states))))] | |
(fn [state] (statemap (str state))))) | |
(def moore-machine (atom :stopped)) | |
(def transitions (make-transitions :starting :started :stopping :stopped)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns machines.moore) | |
(defn make-state-fn [& states] | |
(let [statemap | |
(zipmap | |
(map str states) | |
(rest (conj (vec states) (first states))))] | |
(fn [state] (statemap (str state))))) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Inspired by George Jahad's version: http://georgejahad.com/clojure/debug-repl.html | |
(defmacro local-bindings | |
"Produces a map of the names of local bindings to their values." | |
[] | |
(let [symbols (map key @clojure.lang.Compiler/LOCAL_ENV)] | |
(zipmap (map (fn [sym] `(quote ~sym)) symbols) symbols))) | |
(declare *locals*) | |
(defn eval-with-locals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[clojure.core.async :as a]) | |
(def xform (comp (map inc) | |
(filter even?) | |
(dedupe) | |
(flatmap range) | |
(partition-all 3) | |
(partition-by #(< (apply + %) 7)) | |
(flatmap flatten) | |
(random-sample 1.0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn respond-to? | |
"Check if object can respond to method" | |
[obj method] | |
(boolean | |
(some->> obj | |
class | |
.getMethods | |
(filter #(= (.getName %) (name method))) | |
first | |
.getModifiers |
OlderNewer