• Stars
    star
    244
  • Rank 165,885 (Top 4 %)
  • Language
    Ruby
  • License
    Do What The F*ck ...
  • Created about 13 years ago
  • Updated almost 11 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Programming with Nothing

Programming With Nothing

This code accompanies the Ru3y Manor talk “Programming With Nothing”.

The idea is to implement some basic data structures and control flow under the constraint of only being allowed to create and call procs.

Getting started

All code uses the -> syntax, so Ruby 1.9 is required.

If you are brave, the challenge branch has a set of pending specs and an empty implementation file. Can you make them all pass without breaking any of the rules? Use bundle install to set up RSpec, then just type rspec to run the specs. (To use autotest, run gem install ZenTest and then autotest.)

If you are afraid, the story branch contains a series of commits which fill out the implementation until all the specs pass.

Rules

  • Your code may create procs (with Proc.new, Kernel.proc, Kernel.lambda or ->) and call procs (with Proc#call, Proc#[], Proc#=== or Proc#()).
  • Aside from the above, you may not use any of Ruby's built-in classes, modules, methods, keywords or other language features.
  • You may not assign to local variables.
  • As a practical consideration, you must define constants in order to expose your implementations to the specs. You may also define and later refer to constants for the purpose of code reuse.
  • Constant definition may not be used to sneak recursion in through the back door. A constant is not defined until you have finished defining it, so defining FOO in terms of FOO is cheating.

Hints

  • DECREMENT is hard, so you may need to steal PRED.
  • The evaluation of any proc-valued expression e can be deferred by writing it as -> x { e[x] } as long as it doesn't have any free variables called x. (This is eta-conversion.)
  • Self-application is the simplest way to define a recursive function without cheating. Instead of FOO = ... FOO[...] ..., try BAR = -> f { ... f[f][...] ... }; FOO = BAR[BAR].
  • The more complex Y combinator is a tidier way to define a recursive function, but in Ruby it loops forever; try the Z combinator instead.
  • If you're already familiar with functional programming, beware that operation names and argument order have been chosen to be familiar to Ruby programmers, e.g. UNSHIFT (vs. CONS) takes a list as its first (vs. last) argument. If you are upset, see the pedant branch.

Console

$ irb -Ilib -Ispec -rsupport/helpers

>> include Nothing, Helpers
=> Object

>> to_integer(from_integer(42))
=> 42

>> to_boolean(from_boolean(false))
=> false

>> to_array(from_array([true, 9, :hello]))
=> [true, 9, :hello]

>> to_array(from_array([representation_of(3), representation_of(5)])).map { |n| to_integer(n) }
=> [3, 5]

>> to_integer(ADD[TWO][THREE])
=> 5

Legal

Copyright 2011 Tom Stuart ([email protected], @tomstuart). This is free software; see COPYING for details.

More Repositories

1

monads

Simple Ruby implementations of some common monads.
Ruby
597
star
2

computationbook

Example code for Understanding Computation
Ruby
494
star
3

utf-8-challenges

A short tutorial on UTF-8. Run with `ruby utf_8_challenges.rb`, unskip each test and make it pass!
Ruby
48
star
4

kanren

An example Ruby implementation of μKanren.
Ruby
22
star
5

tradfri

A Ruby interface to IKEA’s smart lighting system
Ruby
18
star
6

dual_number

A Ruby implementation of dual numbers.
Ruby
17
star
7

vector_space

A Ruby library for treating multidimensional values as elements of a vector space.
Ruby
13
star
8

wasminna

Live coding a WebAssembly interpreter in pure Ruby with no dependencies, guided by the Wasm spec’s test suite
Ruby
13
star
9

something

Programming with Something
Ruby
9
star
10

little_scheme

Growing a little Scheme interpreter, guided by The Little Schemer
Ruby
9
star
11

inference-rules

A simple implementation of generic inference rules
Ruby
7
star
12

react-workshop

What Even Is A React (And So Can You!)
JavaScript
6
star
13

govuk-exhibit

A GOV.UK exhibit
Ruby
5
star
14

lox

An implementation of the Lox language from Robert Nystrom’s “Crafting Interpreters”
Ruby
3
star
15

neural-network

HTML
2
star
16

subsequence_matchers

Ruby
2
star
17

capybara-envjs-button-bug

Ruby
1
star
18

rubyforge-redirects

Crowdsourced redirects for old RubyForge URLs
Ruby
1
star
19

genetic-algorithms

A scrappy genetic algorithm visualisation for London Computation Club
HTML
1
star
20

recursion-workshop

JavaScript
1
star
21

mandelbrot

Some scrappy Mandelbrot visualisations for London Computation Club
HTML
1
star
22

heylist

Ruby
1
star
23

negative-numbers

Representing negative numbers in Ruby
Ruby
1
star
24

tapl

A Ruby implementation of typecheckers from Types and Programming Languages
Ruby
1
star
25

hoas

An implementation of higher-order abstract syntax in Ruby
Ruby
1
star