Skip to content

Commit

Permalink
- Fixed a warning when the Object class in opened.
Browse files Browse the repository at this point in the history
Example:
	class Object {	# now works silently

	}

- Added a few more Sidef scripts.
  • Loading branch information
trizen committed Oct 31, 2015
1 parent a353469 commit 7812586
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 1 deletion.
3 changes: 3 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ scripts/Graphical/user_input.sf
scripts/Graphical/window_creation.sf
scripts/Graphical/window_creation_1.sf
scripts/Graphical/yin_and_yang.sf
scripts/happy_numbers.sf
scripts/huffman_coding.sf
scripts/Interactive/cosmic_calendar.sf
scripts/Interactive/draw_a_clock.sf
Expand Down Expand Up @@ -356,6 +357,7 @@ scripts/RosettaCode/entropy.sf
scripts/RosettaCode/enumerations.sf
scripts/RosettaCode/enumerations_1.sf
scripts/RosettaCode/equilibrium_index.sf
scripts/RosettaCode/euler_method.sf
scripts/RosettaCode/evaluate_binomial_coefficients.sf
scripts/RosettaCode/factorial.sf
scripts/RosettaCode/fibonacci_n-step_number_sequence.sf
Expand Down Expand Up @@ -536,6 +538,7 @@ scripts/RosettaCode/walk_a_dir_non_recursively.sf
scripts/RosettaCode/word_wrap.sf
scripts/RosettaCode/zeckendorf_number_representation.sf
scripts/RosettaCode/zig-zag_matrix.sf
scripts/sets.sf
scripts/sierpinski_triangle.sf
scripts/simple_moving_average.sf
scripts/simple_moving_average_oo.sf
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Sidef

To learn about Sidef, check out the following links:

* Github: https://github.com/trizen/sidef
* Gitbook: http://trizen.gitbooks.io/sidef-lang/content/
* Tutorial: https://github.com/trizen/sidef/wiki
* RosettaCode: http://rosettacode.org/wiki/Sidef
Expand Down
2 changes: 1 addition & 1 deletion lib/Sidef/Deparse/Perl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ HEADER
? (join(' ', map { ref($_) ? $self->_dump_class_name($_) : $_ } @{$self->{inherit}}) . ' ')
: ''
)
. "Sidef::Object::Object);\n";
. ($self->{package_name} eq 'Sidef::Object::Object' ? '' : "Sidef::Object::Object") . ");\n";
}

if ($is_class and exists $self->{class_vars} and not $self->{ref_class}) {
Expand Down
36 changes: 36 additions & 0 deletions scripts/RosettaCode/euler_method.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Euler_method#Sidef
#

func euler_method(t0, t1, k, step_size) {
var results = [[0, t0]];
step_size.to(100).by(step_size).each { |s|
t0 -= ((t0 - t1) * k * step_size);
results << [s, t0];
}
return results;
}

func analytical(t0, t1, k, time) {
(t0 - t1) * exp(-time * k) + t1;
}

var (T0, T1, k) = (100, 20, .07);
var r2 = euler_method(T0, T1, k, 2).grep { _[0] %% 10 };
var r5 = euler_method(T0, T1, k, 5).grep { _[0] %% 10 };
var r10 = euler_method(T0, T1, k, 10).grep { _[0] %% 10 };
say "Time\t 2 err(%) 5 err(%) 10 err(%) Analytic";
say "-"*76;
r2.range.each { |i|
var an = analytical(T0, T1, k, r2[i][0]);
printf("%4d\t#{'%9.3f' * 7}\n",
r2[i][0],
r2[i][1], (r2[i][1] / an) * 100 - 100,
r5[i][1], (r5[i][1] / an) * 100 - 100,
r10[i][1], (r10[i][1] / an) * 100 - 100,
an);
}
21 changes: 21 additions & 0 deletions scripts/happy_numbers.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Happy_numbers
#

func happy(n) is cached {
static seen = Hash;

return true if n.is_one;
return false if seen.has_key(n);

seen{n} = 1;
happy(n.digits »**» 2 -> sum)
}

var count = 0;
{ |i|
happy(i) ? say i : next;
++count == 8 && break;
} * Math.inf;
98 changes: 98 additions & 0 deletions scripts/sets.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Set#Sidef
#

class Set(*set) {

method init {
var elems = set;
set = Hash.new;
elems.each { |e| self += e }
}

method +(elem) {
set{elem} = elem;
self;
}

method del(elem) {
set.delete(elem);
}

method has(elem) {
set.has_key(elem);
}

method (Set that) {
Set(set.values..., that.values...);
}

method (Set that) {
Set(set.keys.grep{ |k| k that }
.map { |k| set{k} }...);
}

method (Set that) {
Set(set.keys.grep{|k| !(k that) }
.map {|k| set{k} }...);
}

method ^(Set that) {
var d = ((self that) (that self));
Set(d.values...);
}

method count { set.len }

method (Set that) {
(self that -> count.is_zero) && (that self -> count.is_zero);
}

method values { set.values }

method (Set that) {
that.set.keys.each { |k|
k self || return false;
}
return true;
}

method to_s {
"Set{" + set.values.map{|e| "#{e}"}.sort.join(', ') + "}"
}
}

class Object {
method (Set set) {
set.has(self);
}
}

#
## Testing
#

var x = Set(1, 2, 3);
5..7 -> each { |i| x += i };

var y = Set(1, 2, 4, x);

say "set x is: #{x}";
say "set y is: #{y}";

[1,2,3,4,x].each { |elem|
say ("#{elem} is ", elem y ? '' : 'not', " in y");
}

var (w, z);
say ("union: ", x y);
say ("intersect: ", x y);
say ("z = x ∖ y = ", z = (x y) );
say ("y is ", x y ? "" : "not ", "a subset of x");
say ("z is ", x z ? "" : "not ", "a subset of x");
say ("z = (x ∪ y) ∖ (x ∩ y) = ", z = ((x y) (x y)));
say ("w = x ^ y = ", w = (x ^ y));
say ("w is ", w z ? "" : "not ", "equal to z");
say ("w is ", w x ? "" : "not ", "equal to x");

0 comments on commit 7812586

Please sign in to comment.