Skip to content

Commit

Permalink
- Extended the Hash.default() method to accept a block which is execu…
Browse files Browse the repository at this point in the history
…ted each time it can't find a given key.

Example:
	var hash = Hash.new;

	var x = 0;
	hash.default { x += 1 };

	say hash[:a];    # prints: 1
	say hash[:b];    # prints: 2
	say hash[:c];    # prints: 3

- Improved the parsing of colon-barewords (`:bareword`); now it accepts numbers (e.g.: `:1234`)

new file:   scripts/Expensive/fibonacci_word_fractal.sf
new file:   scripts/hash_default_block_value.sf
  • Loading branch information
trizen committed Sep 9, 2015
1 parent 43e1f00 commit 4522d5b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 9 deletions.
2 changes: 2 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ scripts/Expensive/chebyshev_coefficients.sf
scripts/Expensive/dice_game_solver.sf
scripts/Expensive/factorial_approximations.sf
scripts/Expensive/fibonacci_validation.sf
scripts/Expensive/fibonacci_word_fractal.sf
scripts/Expensive/gd_sierpinsky_triangle.sf
scripts/Expensive/haversine_formula.sf
scripts/Expensive/langton_s_ant.sf
Expand Down Expand Up @@ -287,6 +288,7 @@ scripts/group_precedence.sf
scripts/hailstone.sf
scripts/happy_2_years_old.sf
scripts/hash_autovivification.sf
scripts/hash_default_block_value.sf
scripts/hash_default_value.sf
scripts/hash_from_array.sf
scripts/hash_grep.sf
Expand Down
20 changes: 12 additions & 8 deletions lib/Sidef/Exec.pm
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,18 @@ package Sidef::Exec {
(ref($self_obj) && $self_obj->isa('HASH'))
|| ($self_obj = Sidef::Types::Hash::Hash->new);

$self_obj->{data}{$ind->get_value} //=
Sidef::Variable::Variable->new(
name => '',
type => 'var',
value => $l < $#{$expr->{ind}}
? Sidef::Types::Hash::Hash->new
: ($self_obj->default)
);
$self_obj->{data}{$ind->get_value} //= Sidef::Variable::Variable->new(
name => '',
type => 'var',
value => $l < $#{$expr->{ind}}
? Sidef::Types::Hash::Hash->new
: do {
my $default = $self_obj->{__DEFAULT_VALUE__};
defined($default) && $default->SUPER::isa('Sidef::Types::Block::Code')
? $default->run
: $default;
}
);
}
: do {
(ref($self_obj) && $self_obj->isa('ARRAY'))
Expand Down
2 changes: 1 addition & 1 deletion lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ package Sidef::Parser {
}

# Bareword followed by a fat comma or a colon character
if ( /\G:([_\pL][_\pL\pN]*)/gc
if ( /\G:([_\pL\pN]+)/gc
|| /\G([_\pL][_\pL\pN]*)(?=\h*=>|:(?![=:]))/gc) {
return Sidef::Types::String::String->new($1);
}
Expand Down
68 changes: 68 additions & 0 deletions scripts/Expensive/fibonacci_word_fractal.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Fibonacci_word/fractal
#

var(m=17, scale=3) = ARGV»to_i»()...;

(var world = Hash.new)[0][0] = 1;
var loc = Complex(0, 0);
var dir = Complex::i;

func fib_word(n) {
static fib = ['1', '0'];
fib[n] \\= (fib_word(n-1) + fib_word(n-2));
}

func step {
scale.times {
loc += dir;
world[loc.im][loc.re] = 1;
}
}

func turn_left { dir *= Complex::i };
func turn_right { dir *= -Complex::i };

var n = 1;
fib_word(m).each_char { |c|
if (c == '0') {
step();
n % 2 == 0 ? turn_left()
: turn_right()
} else { n++ }
}

func braille_graphics(a) {
var (xlo, xhi, ylo, yhi) = +([Math.inf, -Math.inf]*2)...;

a.each_key { |y|
ylo.min!(y.to_i);
yhi.max!(y.to_i);
a[y].each_key { |x|
xlo.min!(x.to_i);
xhi.max!(x.to_i);
}
}

for (var y = ylo; y <= yhi; y += 4) {
for (var x = xlo; x <= xhi; x += 2) {
var cell = 0x2800;

a[y+0][x+0] && (cell += 1);
a[y+1][x+0] && (cell += 2);
a[y+2][x+0] && (cell += 4);
a[y+0][x+1] && (cell += 8);
a[y+1][x+1] && (cell += 16);
a[y+2][x+1] && (cell += 32);
a[y+3][x+0] && (cell += 64);
a[y+3][x+1] && (cell += 128);

print cell.chr;
};
print "\n";
}
}

braille_graphics(world);
18 changes: 18 additions & 0 deletions scripts/hash_default_block_value.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/ruby

#
## Block default value for hashes
#

var hash = Hash.new;

var x = 0;
hash.default { x += 1 };

say hash[:a]; # prints: 1
say hash[:b]; # prints: 2
say hash[:c]; # prints: 3

assert_eq(hash[:a], 1);
assert_eq(hash[:b], 2);
assert_eq(hash[:c], 3);

0 comments on commit 4522d5b

Please sign in to comment.