Created
August 10, 2012 01:36
-
-
Save quietfanatic/3310157 to your computer and use it in GitHub Desktop.
EX examples
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
#Implementation of http://rosettacode.org/wiki/XML/Output | |
escape_xml = *.replace( | |
('<', '<'), | |
('>', '>'), | |
('&', '&'), | |
('"', '"'), | |
("'", '''), | |
); | |
character_remarks (chars : List(Str), remarks : List(Str)) = <<"END" | |
<CharacterRemarks> | |
{ | |
zip(chars, remarks).map({ | |
"<Character name=\"{escape_xml(@char)}\">{escape_xml(@remark)}</Character>\n" | |
}) | |
} | |
</CharacterRemarks> | |
END | |
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
# Implementation of http://rosettacode.org/wiki/Walk_a_directory/Recursively | |
find_matching (dir : Dir, pat : Regex, base : Str = '') = do { | |
for dir { # Gives filename objects that can coerce to strings | |
if @_.is_directory { | |
find_matching(@_, pat, "{base}{@_}/"); | |
} | |
else if @_ ~~ pat { | |
say(@_}; | |
} | |
} | |
} |
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
#Implementation of http://rosettacode.org/wiki/Trabb_Pardo%E2%80%93Knuth_algorithm | |
tpk_default_f (x : float) = abs(x)**0.5 + 5 * x**3 | |
tpk (f : Func(float, float) = tpk_default_f, max : float = 400) = do { | |
$input = prompt("Please enter 11 numbers: ").words; | |
if input.length > 11 { | |
say("Too many numbers."); | |
redo; | |
} | |
if input.length < 11 { | |
say("Too few numbers."); | |
redo; | |
} | |
for input { | |
$result = f(@_); | |
if result > max { | |
say "{@_}: too large"; | |
} | |
else { | |
say "{@_}: {result}"; | |
} | |
} | |
} |
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
# Simply find all the factors | |
factors (x : int) = (1..x).grep(x %% *); | |
# Reduce to prime factors | |
factor (x : int) = { | |
f = (1..(x div 2)).map(* * 2).first(x %% *); | |
.^ = if defined(f) { [f] ~ factor(x div f) } | |
else { [] } | |
} | |
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
#Implementation of http://rosettacode.org/wiki/Cut_a_rectangle | |
rectangle_cuts (w : int, h : int) where (w %% 2 or h %% 2) { | |
Pt (.x : int, .y : int) { | |
.opposite = Pt(w-x, h-y); | |
.neighbors = [ | |
Pt(x+1, y), | |
Pt(x-1, y), | |
Pt(x, y+1), | |
Pt(x, y-1), | |
]; | |
} | |
cutter (visited : List(Pt), at : Pt) : int = { | |
done = at.x == 0|w | |
or at.y == 0|h; | |
overlap = visited.any({@_ == at || @_ == at.opposite}); | |
.^ = if overlap { 0 } | |
else if done { 1 } | |
else { at.neighbors.map({cutter([at] ~ visited, @_)}).reduce(* + *) } | |
} | |
middle = Pt(w div 2, h div 2); | |
.^ = if w %% 2 and h %% 2 { | |
cutter([middle], middle.neighbors[0]) | |
+ cutter([middle], middle.neighbors[2]) | |
} | |
else { | |
cutter([], middle) | |
} | |
} |
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
### Some examples of game entities | |
# An interface for actors | |
Actor ( | |
.act : Action, | |
.react : Action, | |
) { } | |
# Trait for decision making | |
AI ( | |
.interval : Action(int) = do { return 30 }, | |
.decision : Action, | |
.phase : int | |
) { | |
.act = do { | |
phase -= 1; | |
if (phase == 0) { | |
phase = interval; | |
decision; | |
} | |
} | |
} | |
# An entity that uses the above | |
Rat ( | |
.pos : Vec, | |
.vel : Vec = Vec(0, 0), | |
.ai = AI( | |
interval = do { return ceil(rand*rand*100) }, | |
decision = do { | |
vel.x = (-2, 2).pick | |
}, | |
phase : int | |
) | |
) { | |
.^ = Actor( | |
act = ai.act; | |
react = do { | |
s = loudest_sound; | |
if (s.volume > 10) { | |
vel.x = 2 * (pos - s.pos).sign; | |
} | |
} | |
) | |
} | |
# Example of usage | |
rat1 = Rat(pos=Vec(4, 12), ai.phase=2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment