This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
(require racket/function | |
racket/list | |
racket/dict) | |
(provide (struct-out lens) | |
lens-get | |
lens-mod | |
lens-set |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use [num] [ | |
num: 0 | |
count: func [x] [ | |
num: num + x | |
print num | |
] | |
] | |
count 1 count 1 count 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
listEven :: [a] -> Bool | |
listEven [] = True | |
listEven (_:xs) = listOdd xs | |
listOdd :: [a] -> Bool | |
listOdd [] = False | |
listOdd (_:xs) = listEven xs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.! | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#_(deftest maybe-plus | |
(is (= :bogus | |
@(m/plus [m/maybe-zero-val | |
(m/do m/maybe | |
[_ (m/maybe 1)] | |
(throw (Exception. "Should not be thrown")))])))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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] |