-
-
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.
- Flat lists in array indices will now return back a list of lvalues.
Example: var arr = %w(a b c d e f g); var ind = [0,1,2]; say arr[ind...] # prints: "abc" The previous syntax `arr.@[ind]` is considered deprecated.
- Loading branch information
trizen
committed
Dec 17, 2015
1 parent
0603970
commit 5fa3c64
Showing
5 changed files
with
125 additions
and
10 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/S-Expressions#Sidef | ||
# | ||
|
||
var t = frequire('Text::Balanced'); | ||
|
||
func sexpr(txt) { | ||
txt.trim!; | ||
|
||
var m = txt.match(/^\((.*)\)$/s) | ||
|| die "Not an S-expression: <<#{txt}>>"; | ||
txt = m[0]; | ||
|
||
var w; | ||
var ret = []; | ||
while (!txt.is_empty) { | ||
given (txt.first) { | ||
when('(') { | ||
(w, txt) = t.extract_bracketed(txt, '()'); | ||
w = sexpr(w); | ||
} | ||
when ('"') { | ||
(w, txt) = t.extract_delimited(txt, '"') | ||
w.sub!(/^"(.*)"/, {|s1| s1 }); | ||
} | ||
default { | ||
txt.sub!(/^(\S+)/, {|s1| w = s1; '' }); | ||
} | ||
} | ||
ret << w; | ||
txt.trim_beg!; | ||
} | ||
return ret; | ||
} | ||
|
||
func sexpr2txt(String e) { | ||
e ~~ /[\s"\(\)]/ ? do { e.gsub!('"', '\\"'); %Q("#{e}") } : e; | ||
} | ||
|
||
func sexpr2txt(expr) { | ||
'(' + expr.map {|e| sexpr2txt(e) }.join(' ') + ')'; | ||
} | ||
|
||
var s = sexpr(%q{ | ||
((data "quoted data" 123 4.5) | ||
(data (!@# (4.5) "(more" "data)"))) | ||
}); | ||
|
||
say s; # dump structure | ||
say sexpr2txt(s); # convert back |
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,30 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/Singleton | ||
# | ||
|
||
class Singleton(name) { | ||
static instance; | ||
|
||
method new(name) { | ||
instance ||= Singleton.bless(Hash(name => name)); | ||
} | ||
method new { | ||
Singleton.new(nil); | ||
} | ||
} | ||
|
||
var s1 = Singleton('foo'); | ||
say s1.name; #=> 'foo' | ||
say s1.object_id; #=> '30424504' | ||
|
||
var s2 = Singleton(); | ||
say s2.name; #=> 'foo' | ||
say s2.object_id; #=> '30424504' | ||
|
||
s2.name = 'bar'; # change name in s2 | ||
say s1.name; #=> 'bar' | ||
|
||
assert_eq(s1.object_id, s2.object_id); | ||
assert_eq(s1.name, s2.name); |
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,30 @@ | ||
#!/usr/bin/ruby | ||
|
||
# | ||
## http://rosettacode.org/wiki/Stem-and-leaf_plot | ||
# | ||
|
||
var data = %i( | ||
12 127 28 42 39 113 42 18 44 118 44 | ||
37 113 124 37 48 127 36 29 31 125 139 | ||
131 115 105 132 104 123 35 113 122 42 117 | ||
119 58 109 23 105 63 27 44 105 99 41 | ||
128 121 116 125 32 61 37 127 29 113 121 | ||
58 114 126 53 114 96 25 109 7 31 141 | ||
46 13 27 43 117 116 27 7 68 40 31 | ||
115 124 42 128 52 71 118 117 38 27 106 | ||
33 117 116 111 40 119 47 105 57 122 109 | ||
124 115 43 120 43 27 27 18 28 48 125 | ||
107 114 34 133 45 120 30 127 31 116 146 | ||
).sort; | ||
|
||
var stem_unit = 10; | ||
var h = data.group_by { |i| i / stem_unit -> int } | ||
|
||
var rng = RangeNum(h.keys.map{.to_i}.minmax); | ||
var stem_format = "%#{rng.min.len.max(rng.max.len)}d"; | ||
|
||
rng.each { |stem| | ||
var leafs = (h{stem} \\ []) | ||
say(stem_format % stem, ' | ', leafs.map { _ % stem_unit }.join(' ')) | ||
} |