Skip to content

Commit

Permalink
- Added the Bool.pick() method (and Bool.rand() as alias) to choose a…
Browse files Browse the repository at this point in the history
… random boolean value

- Added the Number.to_num() method (and Number.to_n() as alias) which simply returns the self object
- Minor fixes
  • Loading branch information
trizen committed Dec 2, 2015
1 parent 1f7991d commit d71f860
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
7 changes: 7 additions & 0 deletions lib/Sidef/Types/Bool/Bool.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ package Sidef::Types::Bool::Bool {

sub true { $true }
sub false { $false }

sub pick {
rand(1) < 0.5 ? $true : $false;
}

*rand = \&pick;
}

sub get_value { ${$_[0]} }
sub to_bool { $_[0] }
*to_b = \&to_bool;

*{__PACKAGE__ . '::' . '|'} = sub {
my ($self, $arg) = @_;
Expand Down
7 changes: 7 additions & 0 deletions lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ package Sidef::Types::Number::Number {
$GET_PERL_VALUE ? ${$_[0]}->numify : ${$_[0]};
}

sub to_n {
$_[0]
}

*to_num = \&to_n;

sub mod {
my ($self, $num) = @_;
$self->new($$self % $num->get_value);
Expand Down Expand Up @@ -318,6 +324,7 @@ package Sidef::Types::Number::Number {

*as_int = \&int;
*to_i = \&int;
*to_int = \&int;

sub max {
my ($self, $arg) = @_;
Expand Down
2 changes: 1 addition & 1 deletion scripts/Expensive/root_of_a_function.sf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ range(start+step, stop, step).each { |x|
when (0) {
say "Root found at #{x}";
}
when (sign && ((value > 0) != sign)) {
case (sign && ((value > 0) != sign)) {
say "Root found near #{x}";
}
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/Games/guess_the_number_with_feedback.sf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ say "Guess the number between 1 and 10";
loop {
given(var n = Sys.scanln("> ").to_i) {
when (number) { say "You guessed it."; break }
when (n < number) { say "Too low" }
case (n < number) { say "Too low" }
default { say "Too high" }
}
}
8 changes: 4 additions & 4 deletions scripts/Games/guess_the_number_with_feedback__player_.sf
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ loop {
 
++tries;
given(var score = STDIN.readline) {
when(max <= min) {
case(max <= min) {
say "\nI give up...";
break;
}
when(score ~~ /^h/i) {
when(/^h/i) {
min = guess+1;
}
when(score ~~ /^l/i) {
when(/^l/i) {
max = guess;
}
when(score ~~ /^e/i) {
when(/^e/i) {
say "\nI knew it! It took me only #{tries} tries.";
break;
}
Expand Down

0 comments on commit d71f860

Please sign in to comment.