Skip to content

Instantly share code, notes, and snippets.

;; 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 September 27, 2017 09:55
examples of prismatic schema extension to higher order functions
;; (: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 March 17, 2012 15:03
Port of part of the datomic tutorial to Clojure
(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
(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
(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])
(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 January 26, 2012 20:43
Creating a custom Clojure map type
(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 November 4, 2010 01:05
Demonstration of implementation "inheritance" in clojure
;; 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 [_])
(pprint (new-rocket-car {:color "Blue" :fuel 100 :name "Zoomer"}))
;; -> "(new-rocket-car {:color "Blue", :fuel 100, :name "Zoomer"})\n"
(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"})