-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixed the unary
?
and !
operators -- overloaded objects are now…
… respected correctly. new file: scripts/Rosettacode/Graphical/Death_Star.sf new file: scripts/Rosettacode/Graphical/Simple_windowed_application.sf new file: scripts/Rosettacode/Strand_sort.sf new file: scripts/Rosettacode/Top_rank_per_group.sf new file: scripts/Rosettacode/Wireworld.sf new file: scripts/same_fringe.sf
- Loading branch information
trizen
committed
Sep 16, 2015
1 parent
e3f131a
commit 41f9013
Showing
8 changed files
with
283 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/Death_Star#Sidef | ||
# | ||
|
||
# Writes a PGM to stdout. | ||
|
||
func sq(*nums) { | ||
nums »**» 2 «+»; | ||
} | ||
|
||
func hitf(sph, x, y) { | ||
x -= sph[0]; | ||
y -= sph[1]; | ||
|
||
var z = (sq(sph[3]) - sq(x, y)); | ||
z < 0 && return; | ||
|
||
z.sqrt!; | ||
[sph[2] - z, sph[2] + z]; | ||
} | ||
|
||
func normalize(v) { | ||
var n = sq(v...).sqrt; | ||
v »/» n; | ||
} | ||
|
||
func dot(x, y) { | ||
var s = (x[0]*y[0] + x[1]*y[1] + x[2]*y[2]); | ||
s > 0 ? s : 0; | ||
} | ||
|
||
var pos = [120, 120, 0, 120]; | ||
var neg = [-77, -33, -100, 190]; | ||
var light = normalize([-12, 13, -10]); | ||
|
||
func draw(k, amb) { | ||
STDOUT.binmode(':raw'); | ||
print ("P5\n", pos[0]*2 + 3, " ", pos[1]*2 + 3, "\n255\n"); | ||
|
||
range((pos[1] - pos[3] - 1), (pos[1] + pos[3] + 1)).each { |y| | ||
var row = []; | ||
range((pos[0] - pos[3] - 1), (pos[0] + pos[3] + 1)).each { |x| | ||
var hit = 0; | ||
var hs = []; | ||
var h = hitf(pos, x, y); | ||
|
||
if (!h) { hit = 0; h = [0, 0] } | ||
elsif (!(hs = hitf(neg, x, y))) { hit = 1; hs = [0, 0] } | ||
elsif (hs[0] > h[0]) { hit = 1 } | ||
elsif (hs[1] > h[0]) { hit = (hs[1] > h[1] ? 0 : 2) } | ||
else { hit = 1 }; | ||
|
||
var (val, v); | ||
given(hit) | ||
> 0 { val = 0} | ||
> 1 { v = [x-pos[0], y-pos[1], h[0]-pos[2]] } | ||
: { v = [neg[0]-x, neg[1]-y, neg[2]-hs[1]] } | ||
; | ||
|
||
if (v) { | ||
v = normalize(v); | ||
val = int((dot(v, light)**k + amb) * 255); | ||
val = (val > 255 ? 255 : (val < 0 ? 0 : val)); | ||
}; | ||
row.append(val); | ||
}; | ||
print 'C*'.pack(row...); | ||
} | ||
} | ||
|
||
draw(2, 0.2); |
33 changes: 33 additions & 0 deletions
33
scripts/Rosettacode/Graphical/Simple_windowed_application.sf
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/Simple_windowed_application | ||
# | ||
|
||
require('Gtk2') -> init; | ||
|
||
# Window. | ||
var window = %s'Gtk2::Window'.new; | ||
window.signal_connect('destroy' => { %s'Gtk2'.main_quit }); | ||
|
||
# VBox. | ||
var vbox = %s'Gtk2::VBox'.new(0, 0); | ||
window.add(vbox); | ||
|
||
# Label. | ||
var label = %s'Gtk2::Label'.new('There have been no clicks yet.'); | ||
vbox.add(label); | ||
|
||
# Button. | ||
var count = 0; | ||
var button = %s'Gtk2::Button'.new(' Click Me '); | ||
vbox.add(button); | ||
button.signal_connect('clicked' => { | ||
label.set_text(++count); | ||
}); | ||
|
||
# Show. | ||
window.show_all; | ||
|
||
# Main loop. | ||
%s'Gtk2'.main; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/Sorting_algorithms/Strand_sort | ||
# | ||
|
||
func merge(x, y) { | ||
var out = []; | ||
while (x && y) { | ||
given (x[-1] <=> y[-1]) | ||
> 1 { out.prepend(x.pop) } | ||
> -1 { out.prepend(y.pop) } | ||
: { out.prepend(x.pop, y.pop) } | ||
}; | ||
x + y + out; | ||
} | ||
|
||
func strand(x) { | ||
x || return []; | ||
var out = [x.shift]; | ||
if (x.len) { | ||
range(-x.len, -1).each { |i| | ||
if (x[i] >= out[-1]) { | ||
out.append(x.pop_at(i)); | ||
} | ||
} | ||
}; | ||
out; | ||
} | ||
|
||
func strand_sort(x) { | ||
var out = []; | ||
while (var strd = strand(x)) { | ||
out = merge(out, strd); | ||
}; | ||
out; | ||
} | ||
|
||
var a = 10.of {100.rand.int}; | ||
say "Before: #{a}"; | ||
|
||
var new = strand_sort(a.copy); | ||
say "After: #{new}"; | ||
|
||
assert_eq(a.sort, new); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/Top_rank_per_group | ||
# | ||
|
||
var data = <<'EOF'.lines.map{ (var h = Hash.new)[%w(name id salary dept)] = .split(','); h }; | ||
Tyler Bennett,E10297,32000,D101 | ||
John Rappl,E21437,47000,D050 | ||
George Woltman,E00127,53500,D101 | ||
Adam Smith,E63535,18000,D202 | ||
Claire Buckman,E39876,27800,D202 | ||
David McClellan,E04242,41500,D101 | ||
Rich Holcomb,E01234,49500,D202 | ||
Nathan Adams,E41298,21900,D050 | ||
Richard Potter,E43128,15900,D101 | ||
David Motsinger,E27002,19250,D202 | ||
Tim Sampair,E03033,27000,D101 | ||
Kim Arlich,E10001,57000,D190 | ||
Timothy Grove,E16398,29900,D190 | ||
EOF | ||
|
||
var n = 3; | ||
|
||
data.map { _[:dept] }.uniq.sort.each { |d| | ||
var es = data.grep { _[:dept] == d }.sort {|a,b| | ||
b[:salary].to_num <=> a[:salary].to_num | ||
}; | ||
say d; | ||
n.times { | ||
es || break; | ||
var e = es.shift; | ||
printf("%-15s | %-6s | %5d\n", e[%w(name id salary)]...); | ||
}; | ||
print "\n"; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/Wireworld | ||
# | ||
|
||
var f = [[], DATA.lines.map {['', .chomp.split('')..., '']}..., []]; | ||
|
||
var world; | ||
|
||
range(1, 10).each { | ||
world = f.map { .join(" ") + "\n" }.join; | ||
var a = [[]]; | ||
range(1, f.end-1).each { |y| | ||
var r = f[y]; | ||
var rr = ['']; | ||
range(1, r.end-1).each { |x| | ||
var c = r[x]; | ||
rr.append( | ||
given(c) | ||
> 'H' { 't' } | ||
> 't' { '.' } | ||
> '.' { <. H>[f[y-1 .. y+1].flat_map{_[x-1 .. x+1]}.count('H') ~~ [1,2]] } | ||
: { c } | ||
); | ||
}; | ||
rr.append(''); | ||
a.append(rr); | ||
}; | ||
f = [a..., []]; | ||
} | ||
|
||
say world; | ||
assert_eq(world, "\n t H . t H . t H . t H \n . H \n . . . \n . . \n H t H . . . . . . . \n\n"); | ||
|
||
__DATA__ | ||
tH......... | ||
. . | ||
... | ||
. . | ||
Ht.. ...... |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/Same_Fringe | ||
# | ||
|
||
var trees = [ | ||
# 0..2 are same | ||
[ 'd', [ 'c', [ 'a', 'b', ], ], ], | ||
[ [ 'd', 'c' ], [ 'a', 'b' ] ], | ||
[ [ [ 'd', 'c', ], 'a', ], 'b', ], | ||
# and this one's different! | ||
[ [ [ [ [ [ 'a' ], 'b' ], 'c', ], 'd', ], 'e', ], 'f' ], | ||
]; | ||
|
||
func get_tree_iterator(*rtrees) { | ||
var tree; | ||
closure { | ||
tree = rtrees.pop; | ||
while (tree.is_an(Array)) { | ||
rtrees.append(tree[1]); | ||
tree = tree[0]; | ||
}; | ||
return tree; | ||
} | ||
} | ||
|
||
func cmp_fringe(a, b) { | ||
var ti1 = get_tree_iterator(a); | ||
var ti2 = get_tree_iterator(b); | ||
loop { | ||
var (L, R) = (ti1(), ti2()); | ||
all { L != nil; R != nil; L == R } && next; | ||
all { L == nil; R == nil } && return "Same"; | ||
return "Different"; | ||
} | ||
} | ||
|
||
var a = cmp_fringe(trees[0], trees[1]); | ||
var b = cmp_fringe(trees[1], trees[2]); | ||
var c = cmp_fringe(trees[2], trees[3]); | ||
|
||
assert_eq(a, "Same"); | ||
assert_eq(b, "Same"); | ||
assert_eq(c, "Different"); | ||
|
||
say "** Test passed!"; |