-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixed a warning when the Object class in opened.
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
Showing
6 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |