Skip to content

Instantly share code, notes, and snippets.

View fogus's full-sized avatar
💭
attempting to learn how to better learn

Fogus fogus

💭
attempting to learn how to better learn
View GitHub Profile
@fogus
fogus / fogus.rb
Last active August 29, 2015 14:25 — forked from raganwald/fogus.rb
# See https://twitter.com/fogus/status/623312803345117184
def meth a, b, c
[yield(a), yield(b), yield(c)]
end
meth(1, 2, 3) { |x| x * x }
# => [1, 4, 9]
arr = [1962, 6, 14, lambda { |x| x.to_s }]
@fogus
fogus / README.md
Last active August 29, 2015 14:21 — forked from davidbalbert/README.md

This is a partial implementation of Piumarta and Warth's Open, extensible object models paper in Ruby. It is the simpler implementation where bind returns a method rather than a closure that wraps the method and some arbitrary piece of data. It also doesn't implement any method caching. I'm not super happy with the the split between in the "low level" Ruby object system, and the "higher level" system that we're implementing, but I don't have time to do a redesign before publishing today's PotW :).

Running the program will drop you into a shell where you can play with the object model. It's nicer if you have pry installed because then you'll have access to local variables like symbol and s_delegated.

@fogus
fogus / contracts.md
Last active August 29, 2015 14:20 — forked from frenchy64/contracts.md

Contracts in Clojure

Developing a gradual typing system for Clojure requires a rich contracts library. Some libraries already exist like Schema and core.contracts. These do not provide more advanced contracts like polymorphic function contracts or dependent contracts. This article explores the possibilities in implementing polymorphic function contracts in Clojure.

Racket's contract library has the concept of Opaque values which can be effectively locked with a key. This is a problem when directing control flow, because the only difference we want from adding contracts is more errors and there is potential to change return values. For example

(defn minc [n]
  (if (number? n)
 (inc n)
@fogus
fogus / poly-con.md
Last active August 29, 2015 14:20 — forked from frenchy64/poly-con.md

Polymorphic contracts

(ns utyped1)

(defn id identity)
(defmulti transmogrify
"Rewrites the last form of a thread-last to use transducer (if possible)."
(fn [f xform src & args] f))
(defmacro transmogrify->>
"Like ->> but uses transducers"
([x] x)
([src & xs]
(let [end (last xs)
xforms (butlast xs)
@fogus
fogus / midi.clj
Last active August 29, 2015 14:18 — forked from alandipert/midi.clj
(import '(javax.sound.midi MidiSystem Synthesizer))
(defn play-note [synth channel note-map]
(let [{:keys [note velocity duration]
:or {note 60
velocity 127
duration 1000}} note-map]
(. channel noteOn note velocity)
(Thread/sleep duration)
(. channel noteOff note)))
@fogus
fogus / lazy.rkt
Last active August 29, 2015 14:16 — forked from dvanhorn/lazy.rkt
#lang racket
(require redex)
;; There are many different semantics for Call-By-Need
;; in the literature. Some are purely syntactic:
;; they model sharing using `let' and have complicated
;; notions of evaluation contexts (e.g. Ariola and
;; Felleisen). Others are heap-based natural semantics
;; (e.g. Launchbury).
: site-counter ( literal-int -- int )
here cell - { addr } \ store in 'addr' the address of the literal int preceding this in the user's code
postpone dup \ compile code to ...dup the int at the top of the data stack
postpone 1+ \ ...increment it
addr postpone literal \ ...push the above 'addr' onto the data stack
postpone ! \ ...copy the incremented value INTO THE USERS CODE
; immediate \ do all this at compile time
\ Now for a "normal" word defintion
: bar ( -- )
@fogus
fogus / sql.clj
Last active August 29, 2015 14:16 — forked from alandipert/sql.clj
(require '[clojure.core.strint :as si] ;[org.clojure/core.incubator "0.1.3"]
'[clojure.string :as str])
(defn deterpolate
[s]
(let [inter (@#'si/interpolate s)]
(vec (list* (str/join "?" (filter string? inter))
(filter (complement string?) inter)))))
(defmacro <?
@fogus
fogus / paths.clj
Last active August 29, 2015 14:14 — forked from alandipert/paths.clj
(defn paths
([root] (paths [] root))
([parent x]
(if (map? x)
(mapcat (fn [[k v]] (paths (conj parent k) v)) x)
[parent])))
(def m
{:q {:z 1}
:x {:y {:a 1}