Skip to content

Instantly share code, notes, and snippets.

@ctford
ctford / home_alone.clj
Created February 11, 2015 11:46
Some music via Leipzig and Overtone.
(ns home-alone.home-alone
(:require [overtone.live :refer :all]
[leipzig.melody :refer :all]
[leipzig.scale :as scale]
[leipzig.live :as live]
[leipzig.chord :as chord]
[leipzig.canon :as canon]
[leipzig.temperament :as temperament]))
; Instruments
@ctford
ctford / lenses.clj
Created July 12, 2014 20:40
A Clojure lens implementation based on focus and fmap.
(ns shades.lenses)
; We only need three fns that know the structure of a lens.
(defn lens [focus fmap] {:focus focus :fmap fmap})
(defn view [x {:keys [focus]}] (focus x))
(defn update [x {:keys [fmap]} f] (fmap f x))
; The identity lens.
(defn fapply [f x] (f x))
(def id (lens identity fapply))
@ctford
ctford / cdr_police.clj
Created December 15, 2018 11:01
Cdr Police
(ns ture.cdr-police
(:require [overtone.live :refer :all :exclude [stop]]
[leipzig.melody :refer :all]
[leipzig.scale :as scale]
[leipzig.canon :as canon]
[leipzig.live :as live]
[leipzig.live :refer [stop]]
[leipzig.chord :as chord]
[leipzig.temperament :as temperament]))
@ctford
ctford / row-row-row-your-boat.clj
Created June 5, 2012 19:59
Row, row, row your boat!
(ns overtunes.songs.row-row-row-your-boat
(:use
[overtone.live]))
(definst harpsichord [freq 440]
(let [duration 1]
(*
(line:kr 1 1 duration FREE)
(pluck (* (white-noise) (env-gen (perc 0.001 5) :action FREE))
1 1 (/ 1 freq) (* duration 2) 0.25))))
module Data.FSM.Entropy
%default total
data Solfege = Do | Re | Mi | Fa | So | La | Ti
entropy : Solfege -> Solfege -> Nat
entropy Do Do = 1
entropy Do Re = 2
entropy Re Do = 2
entropy Re Re = 1
module Entropy
import Data.List
%default total
data Probability : a -> Nat -> Type where
Occurred : (x : a) -> Probability x bits
begin : Probability [] 0
begin = Occurred []
@ctford
ctford / prism.idr
Created December 10, 2017 22:40
A non-compiling prism implementation where the applicative instance is AWOL.
module Optic.Prism
import Data.List
%default total
-- Const
data Const a b = Constant a
implementation Functor (Const a) where
map _ (Constant x) = (Constant x)
@ctford
ctford / Chain.idr
Created November 4, 2017 22:26
Fuse a chain of functions
module Data.Chain
%default total
data Chain : Type -> Type -> Type
where
Link : (a -> b) -> Chain a b
Section : (a -> b) -> Chain b c -> Chain a c
fuse : Chain a b -> a -> b
fuse (Link f) x = f x
@ctford
ctford / channel.idr
Last active October 29, 2017 21:42
A simple example of dual client/server session types.
module Data.FSM.Channel
import Data.List
%default total
data Protocol : Type
where
Out : a -> Protocol -> Protocol
In : a -> Protocol -> Protocol
Accept : Protocol -> Protocol -> Protocol
Select : Protocol -> Protocol -> Protocol
@ctford
ctford / channel.idr
Created October 22, 2017 21:46
The totality checker thinks `dual` the last case of makes it partial - even though I would have thought the pattern matching shrinks the argument.
%default total
data Channel : (result :Type ) -> Type
where
Send : a -> Channel a
Receive : a -> Channel a
Finished : Channel ()
(>>=) : Channel a -> ((x:a) -> Channel b) -> Channel b
dual : Channel a -> Channel a