Created
September 24, 2008 01:36
-
-
Save defunkt/12456 to your computer and use it in GitHub Desktop.
This file contains 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
(# examples i used to develop the never-completed Oil) | |
(# treetop parser is at http://gist.github.com/12454) | |
(# a simple math class | |
(= Math (Object.clone | |
(def square (x) (* x x)) | |
(= square (do (x) (* x x))) | |
(def abs (x) ( | |
if (>= x 0) | |
(x) | |
else | |
(- 0 x) | |
)) | |
(def abs (x) (if (>= x 0) (x) (- 0 x))) | |
(def abs (x) (if (>= x 0) then (x) else (- 0 x))) | |
)) | |
) | |
(puts (-3.abs)) | |
(# Math.abs -3) (# 3) |
This file contains 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
(puts (+ 1 2)) | |
(= a 1) | |
(= b 2) | |
(puts (+ a b)) | |
(puts (+ 1 (+ 1 1))) | |
(= name "chris") | |
(puts name) |
This file contains 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
(# recursive nightmare) | |
(= factorial (do (n) (if (== n 0) | |
(1) | |
(else | |
(puts (slots)) | |
(* n (factorial (- n 1))) | |
) | |
) | |
)) | |
(factorial 5) | |
(# puts (factorial 5)) | |
(# total => 120) | |
(# 5 * 24) | |
(# 4 * 6) | |
(# 3 * 2) | |
(# 2 * 1) | |
(# 1 * 1) | |
(# 1) |
This file contains 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
(puts "Hello world!") |
This file contains 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
(if (== 1 1) (puts "true") (puts "false")) |
This file contains 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
(= hello (do ( | |
(puts "hello world") | |
(puts "nicely done") | |
(puts a) | |
(puts (stack)) | |
(= s (stack)) | |
(puts s) | |
))) | |
(= ret (do (1))) | |
(puts (+ 2 (ret))) | |
(puts "----") | |
(= a (+ 1 1)) | |
(hello) | |
(# No z yet! Sucks in calling scope but doesn't modify it!) | |
(= say (do (x y) ( | |
(puts "I want to say...") | |
(puts x) | |
(puts y) | |
(puts z) | |
))) | |
(= z "haha!") | |
(say "hi mom" "you're the best") | |
(# | |
# Uncommenting this should raise an exception - | |
# proof that our lambda isn't bleeding into the calling | |
# scope. | |
#) | |
( # (puts y) ) | |
(def yell ( | |
(puts "I'm gonna yell...") | |
(puts z) | |
)) | |
(yell) |
This file contains 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
(= a (+ 1 1)) | |
(= b (+ 1 2 3 4 5)) | |
(= c (+ 1 (2 * 2))) | |
(= d (/ 100 10)) | |
(puts a b c d) |
This file contains 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
(def assert (expected actual) ( | |
(if (!= expected actual) | |
(then | |
(puts "Failed!") | |
(puts "-> Expected: #{expected}") | |
(puts "-> Actual: #{actual}") | |
) | |
(else | |
(puts "Passed!") | |
(puts "-> #{actual} was #{expected}") | |
) | |
) | |
)) | |
(= name "chris") | |
(assert "My name is chris" "My name is #{name}") | |
(assert "strip me" (" strip me ".strip)) | |
(assert "chomp" ("chomp!".chomp "!")) | |
(assert "chris wanstrath" ("chris wanstrath".squeeze " ")) | |
(# "chris wanstrath" must equal ("chris wanstrath".squeeze " ")) | |
(assert "chris wanstrath" ("chris" + " wanstrath")) | |
(# what happens here?) | |
(# name << " wanstrath") | |
(# assert "chris wanstrath" name) | |
(# what gets returned here?) | |
(# puts ("chris sammy".scan "s")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment