Skip to content

Instantly share code, notes, and snippets.

module Main where
import Control.Concurrent.Chan
import Control.Concurrent
-- A sieve that receives numbers from "inChan", filters
-- out the factors of "prime" and sends the results
-- out through "outChan".
sieve inChan outChan prime = do
print prime
primes = sieve [2..]
where
sieve (p:ps) = p : (sieve [n | n <- ps, n `mod` p /= 0])
slowlyWriteList2Chan chan [] = do return ()
slowlyWriteList2Chan chan (n:ns) = do
writeChan chan n
yield -- Magic happens with this line!
slowlyWriteList2Chan chan ns
@srikumarks
srikumarks / muse-tab-syntax-example.scm
Created November 26, 2010 08:27
P4P examples using muSE's "tab-syntax" mode
define m 10
define this-better-be-6 (add 1 2 3)
define this-better-be-0 add()
define five() 5
define (trpl x) (add x x x)
define (g a b c) (add a b c)
define (d/dx f)
define delta 0.001
@srikumarks
srikumarks / muse-scheme-syntax-example.scm
Created November 26, 2010 08:28
Scheme interpretation of examples in muse-tab-syntax-example.scm
(define m 10)
(define this-better-be-6
(add 1 2 3))
(define this-better-be-0
(add))
(define (five) 5)
@srikumarks
srikumarks / timeit.js
Created September 18, 2011 09:54
A useful javascript profiling function ...
// f(N) is run a few times, timed and some stats are
// returned as an object.
function timeit(f, N) {
var start, stop, dt;
var worst = 0, best = 1000 * 3600, mean = 0, sigma = 0;
var i, M = 7;
for (i = 0; i < M; ++i) {
start = Date.now();
f(N);
stop = Date.now();
@srikumarks
srikumarks / global-access.js
Created September 18, 2011 10:12
Test functions to show that accessing Math as a global is much slower than if it were local.
function f1(N) {
var i, sum = 0;
for (i = 0; i < N; ++i) {
sum += Math.sin(i);
}
return sum;
}
function f2(f) {
return function (N) {
@srikumarks
srikumarks / js_const_var_speed.js
Created December 4, 2011 23:26
V8 Javascript Speed comparison for "const" and "var" usage.
function timeit(f, N, S) {
var start, timeTaken;
var stats = {min: 1e50, max: 0, N: 0, sum: 0, sqsum: 0};
var i;
for (i = 0; i < S; ++i) {
start = Date.now();
f(N);
timeTaken = Date.now() - start;
@srikumarks
srikumarks / watch.js
Created December 14, 2011 06:20
A utility to watch for changes to the value addressed by an object and a key.
var watch = (function () {
// A task queue for calling tasks asynchronously.
// Note that one task queue processes all the notification
// callbacks for all installed watchers. This allows
// the use of lots of watchers without incurring a
// corresponding increase in setTimeouts. If we
// did each notification in its own timer callback,
// then we might incur unwanted delays between
@srikumarks
srikumarks / SeqM.js
Created March 25, 2012 08:39
A customizable way to code sequences of potentially asynchronous actions in Javascript - either for GUI purposes or in node.js.
// There is a recent Hacker News thread discussing Python
// creator Guido van Rossum's objections to a callback
// based API, based on its poor ability to work with
// exceptions. (http://news.ycombinator.com/item?id=3750817)
//
// Since Node.js and much of web stuff deals with callbacks
// ... a lot, I thought it might be possible to address that concern
// using a simple, flexible sequencing function. Here is my take
// on it -
//