Skip to content

Instantly share code, notes, and snippets.

View inheritance-multi-arity.clj
;; Define a "base type" of Dog
(defrecord Dog [breed])
;; Define a "sub type" of TrainedDog
(defrecord TrainedDog [dog word])
;; The interface that both Dog and TrainedDog will implement
(defprotocol Talker
(bark [_])
(speak
@david-mcneil
david-mcneil / typed-higher-order-functions.clj
Last active Sep 27, 2017
examples of prismatic schema extension to higher order functions
View typed-higher-order-functions.clj
;; (:require [lonocloud.step.async.pschema :as s])
;; Define a higher-order function. Note the 'format-f' argument that is specified to be a function
;; of one arity that takes a String and an s/Int to produce a String
(s/defn write-person [format-f :- (s/fn String [String s/Int])
person :- {:name String
:age s/Int}]
(format-f (:name person) (:age person)))
(def joe {:name "joe" :age 19})
@david-mcneil
david-mcneil / datomic-tutorial-snippet.clj
Created Mar 17, 2012
Port of part of the datomic tutorial to Clojure
View datomic-tutorial-snippet.clj
(ns datomic-tutorial (:import [datomic Peer Util]
[java.io FileReader]))
;;create database
(Peer/createDatabase "datomic:mem://seattle")
(Peer/createDatabase "datomic:mem://seattle")
(let [conn (Peer/connect "datomic:mem://seattle")]
;; setup sample schema
(.get (.transact conn
View sandbox.clj
(ns bolo.sandbox
(:require [bolo.nxt]
[bolo.ino])
(:use [bolo.base :only (dbg eval-in-ns)]
[bolo.ops :only (main forward backward turn-left turn-right wait duration block)]
[clojure.pprint :only (pprint)])
(:import [bolo.ops Wait]))
;; make a program in our tank DSL
View ops.clj
(ns bolo.ops
(:use [bolo.base :only (new-op)]))
;; define the operators for our tank DSL
(new-op main [& commands])
(new-op forward [power])
(new-op backward [power])
(new-op turn-left [power])
View op_demo.clj
(ns bolo.op-demo
(:use [bolo.base :only (new-op)]))
;; example of how an operator works
;; an operator is similar to a Clojure record. The main difference is
;; that an operator presents itself as a Clojure List, whereas a record
;; presents itself as a Clojure Map.
@david-mcneil
david-mcneil / custom-clojure-map.clj
Created Jan 26, 2012
Creating a custom Clojure map type
View custom-clojure-map.clj
(ns people
(:use [clojure.string :only (join)]
[clojure.pprint :only (pprint simple-dispatch)]))
;; we can make maps using the special literal form:
{:a 100
:b 200}
(class {:a 100 :b 200})
@david-mcneil
david-mcneil / inheritance.clj
Created Nov 4, 2010
Demonstration of implementation "inheritance" in clojure
View inheritance.clj
;; Define a "base type" of Dog
(defrecord Dog [breed])
;; Define a "sub type" of TrainedDog
(defrecord TrainedDog [dog word])
;; The interface that both Dog and TrainedDog will implement
(defprotocol Talker
(bark [_])
(speak [_])
View record-blog-100.clj
(pprint (new-rocket-car {:color "Blue" :fuel 100 :name "Zoomer"}))
;; -> "(new-rocket-car {:color "Blue", :fuel 100, :name "Zoomer"})\n"
View record-blog-90.clj
(new-rocket-car
(new-rocket-car {:color "Blue" :fuel 100 :name "Zoomer"})
{:color "Red" :name "Flash"})
;; -> (new-rocket-car {:name "Flash", :fuel 100, :color "Red"})