Skip to content

Instantly share code, notes, and snippets.

@disolovyov
Created August 22, 2011 16:36
Show Gist options
  • Select an option

  • Save disolovyov/1162846 to your computer and use it in GitHub Desktop.

Select an option

Save disolovyov/1162846 to your computer and use it in GitHub Desktop.
20 августа: DSL и макросы
macro @from (start, end, body)
syntax ("from", start, "to", end, body) {
System.Console.WriteLine("Compiling from...");
<[
for (mutable i = $start; i <= $end; i++) $body
]>
}
using System.Console;
mutable i = 10;
from 3 to 5 {
i--;
WriteLine("hello");
}
def a = 1;
def b = 2;
def pair = (b, a);
match (pair) {
| (1, 1) => WriteLine("one in both");
| (1, _) => WriteLine("one in first");
| (x, 1) => WriteLine("one in second; first: " + x.ToString());
| _ => WriteLine("no ones");
}
class Turtle
def initialize(&block)
@x = 0
@y = 0
instance_eval &block if block_given?
end
def up
@y -= 1
end
def down
@y += 1
end
def left
@x -= 1
end
def right
@x += 1
end
def start(action)
puts "turtle is #{action} at (#{@x},#{@y})"
end
end
Turtle.new do
up
start :singing
down
3.times { left }
start :dancing
2.times { down }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment