From 7812586da8692dd6d2db6a4b5411d396ba419e9a Mon Sep 17 00:00:00 2001 From: trizen Date: Sat, 31 Oct 2015 06:56:17 +0200 Subject: [PATCH] - Fixed a warning when the Object class in opened. Example: class Object { # now works silently } - Added a few more Sidef scripts. --- MANIFEST | 3 + README.md | 1 + lib/Sidef/Deparse/Perl.pm | 2 +- scripts/RosettaCode/euler_method.sf | 36 +++++++++++ scripts/happy_numbers.sf | 21 +++++++ scripts/sets.sf | 98 +++++++++++++++++++++++++++++ 6 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 scripts/RosettaCode/euler_method.sf create mode 100644 scripts/happy_numbers.sf create mode 100644 scripts/sets.sf diff --git a/MANIFEST b/MANIFEST index 88a5e501f..38f52277c 100644 --- a/MANIFEST +++ b/MANIFEST @@ -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 @@ -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 @@ -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 diff --git a/README.md b/README.md index 7e75f5c00..cd6028e7f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/Sidef/Deparse/Perl.pm b/lib/Sidef/Deparse/Perl.pm index 8bc7a680a..daea1a083 100644 --- a/lib/Sidef/Deparse/Perl.pm +++ b/lib/Sidef/Deparse/Perl.pm @@ -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}) { diff --git a/scripts/RosettaCode/euler_method.sf b/scripts/RosettaCode/euler_method.sf new file mode 100644 index 000000000..3ba8575af --- /dev/null +++ b/scripts/RosettaCode/euler_method.sf @@ -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); +} diff --git a/scripts/happy_numbers.sf b/scripts/happy_numbers.sf new file mode 100644 index 000000000..433b143d3 --- /dev/null +++ b/scripts/happy_numbers.sf @@ -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; diff --git a/scripts/sets.sf b/scripts/sets.sf new file mode 100644 index 000000000..b3970e84f --- /dev/null +++ b/scripts/sets.sf @@ -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");