[ create a new paste ] login | about

aaronla

Name: Aaron Lahman
Email:
Site/Blog: http://ra3s.com/wordpress/dysfunctional-programming/
Location: Redmond, Wa
Default language: Python
Favorite languages: C++, Scheme, Python
About:

Saved pastes by aaronla:

Python, pasted on Aug 19:
1
2
3
4
5
@str
def example():
  print hello

print example[1:4]
view (5 lines, 1 line of output)
Python, pasted on Feb 28:
1
2
3
import datetime
import time
import threading
...
view (22 lines)
Python, pasted on Feb 20:
1
2
3
4
5
def g(f, y, all_legal_inputs_to_f):
  "returns x for y = f(x)"
  for x in all_legal_inputs_to_f:
    if y == f(x): 
      return x
...
view (15 lines, 11 lines of output)
Python, pasted on Feb 19:
1
2
3
4
import sys
print (sys.version) 
[x for x in [1]]
print x
view (4 lines, 3 lines of output)
Python, pasted on Feb 19:
1
2
3
4
5
def loop(g):
  x = None
  for x in g: pass
  return x
def fprint(*xs): 
...
view (12 lines, 5 lines of output)
C, pasted on Oct 24:
1
2
3
4
5
// hypothetical C# async sequencing 

interface IAsyncEnumerable<out T>
{
  IAsyncEnumerator<out T> GetEnumerator(CancellationToken token);
...
view (86 lines)
Plain Text, pasted on Oct 21:
1
2
3
4
5
// async iteration in JavaScript?

function SelectAsync(asyncGenerator, asyncProjection) {
  return function(cancellationToken) {
    return spawnSequence(function*(Yielder){
...
view (15 lines)
C, pasted on Oct 15:
1
2
3
4
5
// C# async sequencing 

interface IAsyncEnumerable<out T>
{
  IAsyncEnumerator<out T> GetEnumerator();
...
view (55 lines)
C, pasted on Oct 15:
1
2
3
4
5
// C#-ish, assuming IAsyncEnumerable

IAsyncEnumerable<int> f( IAsyncEnumerable<int> eg) {
  int i = 1;
  yield i++;
...
view (30 lines)
Plain Text, pasted on Oct 15:
1
2
3
4
5
// promises/A only, not promises/A+

// extract :: w a -> a
// duplicate :: w a -> w (w a)
// extend :: (w a -> b) -> w a -> w b
...
view (20 lines)
Plain Text, pasted on Oct 14:
1
2
3
4
5
// async fibonacci
function async_fib(x) { return spawn(function*() {
  throw new Task.Result(
    x < 2 ? x 
...
view (14 lines)
C, pasted on Sep 23:
1
2
3
4
5
// async enumerables and observables

public IAsyncEnumerable<T> ae2ao(IAsyncObservable<T> source)
{
  return AsyncEnumerableEx.Create(async Yield => 
...
view (36 lines)
C, pasted on Sep 23:
1
2
3
4
5
// async enumerables and observables

public IAsyncEnumerable<T> ae2ao(IAsyncObservable<T> source)
{
  return AsyncEnumerableEx.Create(async Yield => 
...
view (36 lines)
Plain Text, pasted on Sep 23:
1
2
3
4
5
var dr = new DataReader(aStream)

function flow() {
  while (dr.UnconsumedBufferLength > 0) {
    console.log(dr.readString());
...
view (10 lines)
C++, pasted on Sep 20:
1
2
3
4
// this seems like a *Bad Idea*, but is probably *technically* valid

void* rmalloc(size_t sz) {
  void *pv;
...
view (19 lines)
C++, pasted on Sep 19:
1
2
3
4
5
// resumable functions and Rx

template <class T>
void ImperativeGenerator(
  function<task(Subject<T>&)> producer,
...
view (28 lines)
C++, pasted on Sep 19:
1
2
3
4
5
#include <iostream>
using namespace std;

template <class T>
class function {
...
view (51 lines, 3 lines of output)
Python, pasted on Sep 4:
1
2
3
4
5
# not_faster.py 

import time
a = [i % 256 for i in range(300000)]
sum = 0
...
view (10 lines, 1 line of output)
Python, pasted on Sep 4:
1
2
3
4
5
# faster_maybe.py 

import time
a = [8 - (i % 17) for i in range(300000)]
sum = 0
...
view (10 lines, 1 line of output)
Python, pasted on Sep 4:
1
2
3
4
5
# less_slow.py

import time
from random import shuffle
a = [i for i in range(300000)]
...
view (15 lines, 1 line of output)
Python, pasted on Sep 4:
1
2
3
4
5
# slow.py

import time
from random import shuffle
a = [i for i in range(300000)]
...
view (12 lines, 1 line of output)
Python, pasted on Sep 4:
1
2
3
4
5
# fast.py 

import time
a = [i for i in range(300000)]
sum = 0
...
view (10 lines, 1 line of output)
C++, pasted on Aug 20:
1
2
3
4
5
#include <iostream>
#include <string>
using namespace std;

void doit(class Base*);
...
view (25 lines, 3 lines of output)
C++, pasted on Aug 20:
1
2
3
4
5
#include <iostream>
#include <string>
using namespace std;

class Base {
...
view (21 lines, 1 line of output)
C++, pasted on Aug 15:
1
2
3
4
5
template <int n, int m>
struct trial_divide_composite
{
  enum { value = (n%m == 0) || trial_divide_composite<n, m-1>::value };
};
...
view (28 lines, 3 lines of output)
C++, pasted on Aug 15:
1
2
3
4
5
template <int n, int m>
struct trial_divide_composite
{
  enum { value = (n%m == 0) || trial_divide_composite<n, m-1>::value };
};
...
view (28 lines)
C++, pasted on Aug 15:
1
2
3
4
5
template <int n, int m>
struct trial_divide_composite
{
  enum { value = (n%m == 0) || trial_divide_composite<n, m-1>::value };
};
...
view (28 lines, 3 lines of output)
Ruby, pasted on Aug 1:
1
2
3
4
5
# 
# statemachine demo
#

$sprites = []
...
view (74 lines, 20 lines of output)
Plain Text, pasted on Jul 31:
1
2
3
4
5
> function *adding() { for (;;) { yield ((yield null) + (yield null)); }}
undefined
> it = adding()
{}
> it.send(2)
...
view (11 lines)
C, pasted on Jul 31:
1
2
3
4
5
#define _XOPEN_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <ucontext.h>
...
view (80 lines)
C++, pasted on Jul 23:
1
2
3
4
5
#define CGOTO(label) { _dispatch = (label); goto _dispatcher; }
#define BEGIN_CGOTO int _dispatch=-1; _dispatcher: switch (_dispatch) { case -1:;
#define END_CGOTO  }

int main() {
...
view (22 lines, 3 lines of output)
C, pasted on Jun 25:
1
2
3
4
5
int f(int x)
{
    if (x == 0 || x == INT_MIN)
        return x;
    else
...
view (20 lines)
Plain Text, pasted on Jun 4:
1
2
3
4
5
// here's a prototypical contravariant type.
abstract class OutStream<T> {
  write(T value);
}

...
view (37 lines)
Python, pasted on May 10:
1
2
3
4
5
def printx(*args):
  print args

def f():
  printx("f->", (yield 1))
...
view (11 lines, 8 lines of output)
Python, pasted on May 10:
1
2
3
4
5
def f():
  print "f->", (yield 1)
  print "f->", (yield 2)

g = f();
...
view (8 lines, 8 lines of output)
Python, pasted on May 9:
1
2
3
4
5
def arith2 (input):
  @memo_unyield
  def expr (Return, input):
    if (yield [0,1]):
      x = yield factor(input)
...
view (55 lines)
Python, pasted on May 8:
1
2
3
4
5
# compute pythagorean triples using nondeterministic choice
# skip ahead to the 'pythag' function

import time
c0 = time.clock()
...
view (44 lines, 7 lines of output)
Python, pasted on May 8:
1
2
3
4
5
import time
c0 = time.clock()
def unyield (fn):
  def replacement (*args):
    c = []
...
view (37 lines, 7 lines of output)
Python, pasted on May 5:
1
2
3
#
# cpexhaustivetopdown.py
#
...
view (88 lines, 27 lines of output)
Python, pasted on May 5:
1
2
3
#
# cpsimpletopdown.py
#
...
view (92 lines, 21 lines of output)
C, pasted on Apr 24:
1
2
3
4
static Tuple<TimeSpan, long> PerfTest(Action<long /*iterations*/> unitUnderTest)
{
  long n = 100;
  var sw = new StopWatch();
...
view (22 lines)
Plain Text, pasted on Apr 4:
1
2
3
4
5
static IEnumerable<T> Anonerator(
  Func<Func<T, Task>, Task> iteratorator
  )
{
    T current = default(T);
...
view (19 lines)
Python, pasted on Feb 21:
1
2
3
4
5
def def_struct(name, *fields):
    c = "class %s(object):\n" % name
    c+= "  def __init__(self" + ''.join(',%s'%f for f in fields) + "):\n"
    c+= (''.join(
        "    self.%s=%s\n"%(f,f) for f in fields) if fields else 
...
view (13 lines, 2 lines of output)
Python, pasted on Feb 8:
1
2
3
4
5
g = Grammar()
g['expr'] = g['addend'] & pRep(pLiteral('+') & g['addend'])
g['addend'] = g['factor'] & pRep(pLiteral('*') & g['factor'])
g['factor'] = g['number'] | g['parenthetic']
g['number'] = reduce(pOr, (pLiteral(c) for c in '0123456789'))
...
view (30 lines)
C, pasted on Jan 12:
1
2
3
4
5
#include <time.h>
#include <sys/times.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
...
view (74 lines, 2 lines of output)
C, pasted on Dec 4:
1
2
3
4
5
#include <stdio.h>

int main()
{
  int a[2];
...
view (13 lines, 1 line of output)
Plain Text, pasted on Nov 16:
1
2
3
4
5
In[3]:= (* 
   grammars

  parse[g, input, succ, fail] => 
   succ [result, remainingInput]
...
view (61 lines)
C, pasted on Oct 23:
1
2
3
4
/* simple tuples */
#define LISTSIZE(S) LISTSIZEx S
#define LISTSIZEx(...) LISTSIZExx(__VA_ARGS__, 5,4,3,2,1)
#define LISTSIZExx(a,b,c,d,e,N, ...) N
...
view (11 lines, 2 lines of output)
Plain Text, pasted on Oct 4:
1
2
3
4
5
? #define A(X, Y) Y X
? #define B 1 ## 2 ## 3
? .singlestep  A(B, 2)
current:  [1 ## 2 ## 3] in A( _ , 2 )
? step
...
view (14 lines)
Python, pasted on Oct 3:
1
2
3
4
5
def rc4(key):
  i = j = 0
  while 1:
      i = (i + 1) % len(key)
      j = (j + key[i]) % len(key)
...
view (17 lines, 1 line of output)
C, pasted on Sep 19:
1
2
3
4
5
/* this really oughtn't work! */

#include <stdio.h>

char* slen_(char* s)
...
view (22 lines, 3 lines of output)
Python, pasted on Sep 3:
1
2
3
4
reverse = (lambda g: (lambda f: g(lambda *arg: f(f)(*arg))) (
           lambda f: g(lambda *arg: f(f)(*arg))))(
           lambda r: lambda s: s[-1] + r(s[:-1]) if len(s) > 0 else "")
print reverse("hello")
view (4 lines, 1 line of output)
Scheme, pasted on Jul 28:
1
2
3
4
5
(require srfi/19)

(define (repeat fn n)
  (if (= n 1) 
    (fn)
...
view (14 lines, 1 line of output)
C, pasted on Jun 26:
1
2
3
4
5
#include <stdio.h>

#define NEXT goto **ip++
int main()
{
...
view (44 lines, 1 line of output)
C, pasted on Jun 26:
1
2
3
4
5
#include <stdio.h>

#define NEXT goto **ip++
int main()
{
...
view (24 lines, 1 line of output)
Python, pasted on Jun 6:
1
2
userInput = '1'
print eval('x+1', { 'x': int(userInput)}, {}) 
view (2 lines, 1 line of output)
Scheme, pasted on May 31:
1
(display (< 1 "1")) 
view (1 line, 1 line of output)
Python, pasted on May 31:
1
2
3
4
5
def evalit(s):
  print "%r => %r" %(s, eval(s, globals(), globals()))

evalit("0 == False")
evalit("type(0) == type(False)")
view (5 lines, 2 lines of output)
Python, pasted on May 31:
1
2
3
4
5
def evalit(s):
  print "%r => %r" %(s, eval(s, globals(), globals()))

print "consistent inequivalence:"
evalit("1 == '1'")
...
view (11 lines, 7 lines of output)
C, pasted on May 18:
1
2
3
4
5
#include <stdio.h>

int main()
{
  *(int*)0;
...
view (8 lines, 1 line of output)
C++, pasted on May 15:
1
2
3
4
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
...
view (21 lines, 3 lines of output)
C++, pasted on Apr 23:
1
2
3
4
5
using System;
using System.Runtime.InteropServices;
using System.Drawing;

namespace ConsoleApplication1
...
view (46 lines)
C, pasted on Apr 23:
1
2
3
4
5
#include <stdio.h>

int main()
{
    int* p = malloc(sizeof(int));
...
view (12 lines, 1 line of output)
C, pasted on Apr 23:
1
2
3
4
5
#include <stdio.h>

int main()
{
    int* p = malloc(sizeof(int));
...
view (15 lines, 1 line of output)
Python, pasted on Mar 2:
1
2
3
4
5
# simplified monadic packrat (almost CFG) parser
#  derived from parse6.py, added (lazy) backtracking   
#
#  parser :: str -> [ (value, str) ]
class lcons (object):
...
view (140 lines, 22 lines of output)
Python, pasted on Mar 2:
1
2
3
4
# simplified monadic (PEG) parser. no memoization, some backtracking.
#  parser :: str -> maybe (value, str)
def ret (value): 
    return lambda s: (value, s)
...
view (98 lines, 16 lines of output)
Python, pasted on Feb 15:
1
2
3
4
def main():
  print "now, take it for a spin"
  print "", to_seq([1,2,3]).to_list()
    
...
view (58 lines, 6 lines of output)
Python, pasted on Feb 14:
1
2
3
4
5
from parse import *
from test1 import timeit

def group(p):
  return p % (lambda v: ('group', v))
...
view (31 lines)
Python, pasted on Feb 10:
1
2
3
4
5
# first, some boilerplate
class Seq:
  def __init__(self, defer):
    self._defer = defer
  def force(self):
...
view (53 lines, 6 lines of output)
Python, pasted on Feb 10:
1
2
3
4
5
# first, some boilerplate
class Seq:
  def __init__(self, defer):
    self._defer = defer
  def force(self):
...
view (56 lines, 6 lines of output)
C++, pasted on Feb 8:
1
2
3
4
5
#include <iostream>

template <typename T> struct Base {
   void f(int) {
       std::cout << "Base<T>::f\n";
...
view (23 lines, 1 line of output)
C++, pasted on Feb 8:
1
2
3
4
5
#include <iostream>

template <typename T> struct Base {
   void f(int) {
       std::cout << "Base<T>::f\n";
...
view (23 lines, 2 lines of output)
C++, pasted on Dec 22:
1
2
3
4
5
#include <iostream>
#include <iomanip>
using namespace std;

namespace n1 {
...
view (27 lines, 3 lines of output)
C++, pasted on Dec 22:
1
2
3
4
#include <iostream>
#include <iomanip>
#include <typeinfo>
using namespace std;
...
view (32 lines, 5 lines of output)
Python, pasted on Dec 13:
1
2
import operator 
print len(reduce(operator.add, (((),),((),))))
view (2 lines, 1 line of output)
Python, pasted on Oct 22:
1
2
3
4
5
import random
import time


def analyzeCoin(coin, numSamples=1000):
...
view (37 lines, 2 lines of output)
C++, pasted on Oct 21:
1
2
3
4
5
    #include <iostream>
    #include <iomanip>
    using namespace std;

    template <class T>
...
view (35 lines, 6 lines of output)
C++, pasted on Oct 21:
1
2
3
4
5
#include <iostream>
#include <iomanip>
using namespace std;

struct S{
...
view (16 lines, 3 lines of output)
C++, pasted on Oct 7:
1
2
3
4
5
typedef int HRESULT ;
bool SUCCEEDED(HRESULT x){return(x)>=0;}
HRESULT f(){return 0;}
#define g f
#define h f
...
view (38 lines)
Python, pasted on Sep 30:
1
2
3
4
def e(x):
  return e(x-1) if x else 0
def e(x, e=e):
  return e(x-1) if x else 1
...
view (7 lines, 5 lines of output, 1 comment)
Python, pasted on Aug 25:
1
2
fact = lambda x: 1 if x<1 else x*fact(x-1)
print fact(20)
view (2 lines, 1 line of output)
C++, pasted on Aug 12:
1
2
3
4
5
// in reference to http://hbfs.wordpress.com/2011/08/09/programming-challenge-luminance/
#include <iostream>
#include <iomanip>

using namespace std;
...
view (66 lines, 2 lines of output)
Python, pasted on Aug 4:
1
2
3
4
#! python

# digraph.txt used: http://jnicholl.org/Cryptanalysis/Data/DigramFrequencies.php
import math
...
view (42 lines, 4 lines of output)
Scheme, pasted on Jan 12:
1
2
3
4
5
;; QR card for scheme
;; (c) 2011 Aaron Lahman

;; r5rs load module
(load {filename-string})
...
view (152 lines)
Scheme, pasted on Jan 11:
1
2
3
4
;QR card for scheme

;r5rs load module
(load {filename-string})
...
view (118 lines)
Scheme, pasted on Aug 7:
1
2
3
;;
;; hello world plt-scheme servlet
;;
...
view (35 lines)
Scheme, pasted on May 2:
1
2
3
4
5
; mb.ss
;
; mandelbrot set

(define (1+ r)
...
view (51 lines, 30 lines of output)
C++, pasted on Mar 16:
1
2
3
4
5
    #include <iostream>
    using namespace std;

    void f() {
        throw 1;
...
view (31 lines, 1 line of output)
Python, pasted on Feb 26:
1
2
3
4
5
def adds_dynamic_z_decorator(f):
  def replacement(*arg,**karg):
    # create a new 'z' binding in globals, saving previous
    if 'z' in globals():
      oldZ = (globals()['z'],)
...
view (42 lines, 9 lines of output)
C++, pasted on Dec 22:
1
2
3
4
5
/// answer to question on blog post
/// http://eli.thegreenplace.net/2008/12/22/an-rc-circuit-puzzle/
///
/// From the exercise, Eli said "But the current through the capacitor 
/// and resistor is the same current".  I'm not sure where you get this 
...
view (11 lines)
C++, pasted on Dec 22:
1
2
3
4
5
#include <type_traits>

using std::tr1::is_base_of;

// --- policy metaprogramming ---
...
view (79 lines)
Lua, pasted on Nov 28:
1
2
3
4
5
function memoize(f)
  store = {}
  function anon(x)
    if store[x] then
      return store[x]
...
view (28 lines, 2 lines of output)
Lua, pasted on Nov 28:
1
2
3
4
5
function memoize(f)
  store = {}
  function anon(x)
    if store[x] then
      return store[x]
...
view (30 lines, 2 lines of output)
Lua, pasted on Nov 28:
1
2
3
4
5
function memoize(f)
  store = {}
  function anon(x)
    if store[x] then
      return store[x]
...
view (29 lines, 1 line of output)
C++, pasted on Nov 21:
1
2
3
4
struct to_listoftuples
{
    template <class OuterContainerType>
    static PyObject* convert(OuterContainerType const& a)
...
view (18 lines)
C++, pasted on Nov 20:
1
2
3
4
5
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
template <class It>
...
view (28 lines, 1 line of output)
C++, pasted on Nov 19:
1
2
3
4
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
...
view (26 lines, 1 line of output)
C++, pasted on Oct 31:
1
2
3
4
5
#include <iostream>
#include <iomanip>
#include <set>
#include <algorithm>
#include <iterator>
...
view (40 lines, 5 lines of output)
C++, pasted on Oct 31:
1
2
3
4
5
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <iterator>
...
view (40 lines, 5 lines of output)
C++, pasted on Oct 2:
1
2
3
4
5
#include <iostream>
#include <iomanip>
#include <iterator>
#include <list>
using namespace std;
...
view (25 lines, 5 lines of output)
C++, pasted on Oct 2:
1
2
3
4
5
#include <iostream>
#include <iomanip>
#include <iterator>
#include <vector>
using namespace std;
...
view (25 lines, 5 lines of output)
C++, pasted on Sep 27:
1
2
3
4
5
#include <iostream>
#include <iomanip>
#include <map>

// memoizer.  
...
view (61 lines, 4 lines of output)
C++, pasted on Sep 23:
1
2
3
4
5
#include <iostream>
using namespace std;
template <int I, bool Fizz = !(I % 3), bool Buzz = !(I % 5)> struct number {};
template <int I> void fizzbuzz(number<I, false, false>){ cout << I << "\n"; }
template <int I> void fizzbuzz(number<I, true, false>){ cout << "Fizz\n"; }
...
view (21 lines, 100 lines of output)