Skip to content

Instantly share code, notes, and snippets.

#lang racket/base
(require racket/control)
(struct monad (bind return) #:transparent)
(define-syntax-rule (with-monad ([val m]) body ...)
(let* ([m-once m]
[val
(lambda (ma)
@Sgeo
Sgeo / lens.rkt
Created February 17, 2014 06:29
#lang racket/base
(require racket/function
racket/list
racket/dict)
(provide (struct-out lens)
lens-get
lens-mod
lens-set
use [num] [
num: 0
count: func [x] [
num: num + x
print num
]
]
count 1 count 1 count 1
REBOL [
Title: "Monads"
Author: "Sgeo"
]
do-monad: func [
monad "An object containing a /bind and a /return."
assigns [block!]
result [block!]
/local ma f next-assigns arg
@Sgeo
Sgeo / gist:4717756
Created February 5, 2013 21:20
Demonstration of mutually recursive tail-call optimized functions in Scala.
/* code taken verbatim from http://www.scala-lang.org/api/current/index.html#scala.util.control.TailCalls$ */
def isEven(xs: List[Int]): TailRec[Boolean] =
if (xs.isEmpty) done(true) else tailcall(isOdd(xs.tail))
def isOdd(xs: List[Int]): TailRec[Boolean] =
if (xs.isEmpty) done(false) else tailcall(isEven(xs.tail))
@Sgeo
Sgeo / gist:4717537
Created February 5, 2013 20:52
Haskell code to emulate in Scala and Clojure. Idea for the functions taken from http://www.scala-lang.org/api/current/index.html#scala.util.control.TailCalls$
listEven :: [a] -> Bool
listEven [] = True
listEven (_:xs) = listOdd xs
listOdd :: [a] -> Bool
listOdd [] = False
listOdd (_:xs) = listEven xs
waitFor: anAnnouncementClass
"Blocks the current process until anAnnouncementClass is announced.
Note that synchronous code using this method runs the risk of missing announcements."
| val sem |
sem := Semaphore new.
self on: anAnnouncementClass do: [:ann | val := ann. sem signal].
sem wait.
^val.
DynamicVariable subclass: #DynamicGeneratorVar
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Sgeo-DynamicGenerator'!
!DynamicGeneratorVar commentStamp: 'SethJGold 1/14/2013 06:41' prior: 0!
DynamicGeneratorVar holds the current DynamicGenerator in use. Should not be used directly by client code.!
@Sgeo
Sgeo / gist:4005862
Created November 3, 2012 04:18
Some hypothetical test.
#_(deftest maybe-plus
(is (= :bogus
@(m/plus [m/maybe-zero-val
(m/do m/maybe
[_ (m/maybe 1)]
(throw (Exception. "Should not be thrown")))]))))
@Sgeo
Sgeo / gist:3846768
Created October 7, 2012 01:30
delimonads
(ns delimonads.core
(:require [monads.core :as m])
(:use delimc.core))
(reset
(defn bind-cont
"Binds its monadic value argument to the continuation.
Easy to understand explanation: (f (bind-cont x) y) becomes do {x' <- x; f x' y}"
[monadic-value]