Skip to content

Commit

Permalink
- Fixed the unary ? and ! operators -- overloaded objects are now…
Browse files Browse the repository at this point in the history
… 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
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 2 deletions.
6 changes: 6 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -544,11 +544,13 @@ scripts/Rosettacode/Generic_swap_1.sf
scripts/Rosettacode/Get_system_command_output.sf
scripts/Rosettacode/Get_system_command_output_1.sf
scripts/Rosettacode/Globally_replace_text_in_several_files.sf
scripts/Rosettacode/Graphical/Death_Star.sf
scripts/Rosettacode/Graphical/Fractal_tree.sf
scripts/Rosettacode/Graphical/Hello_world_Graphical.sf
scripts/Rosettacode/Graphical/Hello_world_Graphical_1.sf
scripts/Rosettacode/Graphical/Munching_squares.sf
scripts/Rosettacode/Graphical/Sierpinski_triangle_Graphical.sf
scripts/Rosettacode/Graphical/Simple_windowed_application.sf
scripts/Rosettacode/Graphical/Ulam_spiral_[for_primes].sf
scripts/Rosettacode/Graphical/Window_creation.sf
scripts/Rosettacode/Graphical/Window_creation_1.sf
Expand Down Expand Up @@ -789,6 +791,7 @@ scripts/Rosettacode/Soundex.sf
scripts/Rosettacode/Spiral_matrix.sf
scripts/Rosettacode/Stack.sf
scripts/Rosettacode/Stack_1.sf
scripts/Rosettacode/Strand_sort.sf
scripts/Rosettacode/String_append.sf
scripts/Rosettacode/String_case.sf
scripts/Rosettacode/String_comparison.sf
Expand Down Expand Up @@ -819,6 +822,7 @@ scripts/Rosettacode/System/Truncate_a_file.sf
scripts/Rosettacode/System_time.sf
scripts/Rosettacode/Terminal_control_Unicode_output.sf
scripts/Rosettacode/Tokenize_a_string.sf
scripts/Rosettacode/Top_rank_per_group.sf
scripts/Rosettacode/Topic_variable.sf
scripts/Rosettacode/Tree_traversal.sf
scripts/Rosettacode/Trigonometric_functions.sf
Expand All @@ -835,6 +839,7 @@ scripts/Rosettacode/Vector_products.sf
scripts/Rosettacode/Walk_a_directory_Non-recursively.sf
scripts/Rosettacode/Walk_a_directory_Non-recursively_1.sf
scripts/Rosettacode/Walk_a_directory_Recursively.sf
scripts/Rosettacode/Wireworld.sf
scripts/Rosettacode/Word_wrap.sf
scripts/Rosettacode/Word_wrap_1.sf
scripts/Rosettacode/Write_language_name_in_3D_ASCII.sf
Expand All @@ -849,6 +854,7 @@ scripts/Rosettacode/Zig-zag_matrix.sf
scripts/run_length_encoding.sf
scripts/run_length_encoding_2.sf
scripts/run_length_encoding_3.sf
scripts/same_fringe.sf
scripts/script.sf
scripts/selection_sort.sf
scripts/short_circuit_evaluation.sf
Expand Down
4 changes: 2 additions & 2 deletions lib/Sidef/Object/Unary.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ package Sidef::Object::Unary {
};

*{__PACKAGE__ . '::' . '?'} = sub {
Sidef::Types::Bool::Bool->new($_[1]->get_value);
Sidef::Types::Bool::Bool->new($_[1]);
};

*{__PACKAGE__ . '::' . '!'} = sub {
Sidef::Types::Bool::Bool->new(not $_[1]->get_value);
Sidef::Types::Bool::Bool->new(not $_[1]);
};

*{__PACKAGE__ . '::' . '>'} = sub {
Expand Down
73 changes: 73 additions & 0 deletions scripts/Rosettacode/Graphical/Death_Star.sf
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 scripts/Rosettacode/Graphical/Simple_windowed_application.sf
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;
45 changes: 45 additions & 0 deletions scripts/Rosettacode/Strand_sort.sf
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);
36 changes: 36 additions & 0 deletions scripts/Rosettacode/Top_rank_per_group.sf
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";
};
41 changes: 41 additions & 0 deletions scripts/Rosettacode/Wireworld.sf
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.. ......
47 changes: 47 additions & 0 deletions scripts/same_fringe.sf
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!";

0 comments on commit 41f9013

Please sign in to comment.