fancy 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +14 -14
- data/Rakefile +16 -4
- data/bin/fancy +3 -0
- data/bin/fspec +20 -0
- data/bin/fyi +27 -11
- data/bin/ifancy +1 -1
- data/boot/fancy_ext.rb +1 -0
- data/boot/fancy_ext/block_env.rb +1 -9
- data/boot/fancy_ext/delegator.rb +64 -0
- data/boot/fancy_ext/object.rb +4 -0
- data/boot/fancy_ext/thread.rb +4 -0
- data/boot/load.rb +5 -1
- data/boot/rbx-compiler/compiler/ast.rb +0 -1
- data/boot/rbx-compiler/compiler/ast/class_def.rb +2 -0
- data/boot/rbx-compiler/compiler/ast/method_def.rb +2 -0
- data/boot/rbx-compiler/compiler/ast/node.rb +10 -0
- data/boot/rbx-compiler/compiler/ast/ruby_args.rb +12 -0
- data/boot/rbx-compiler/compiler/ast/singleton_method_def.rb +2 -0
- data/boot/rbx-compiler/parser/fancy_parser.bundle +0 -0
- data/boot/rbx-compiler/parser/lexer.lex +5 -11
- data/boot/rbx-compiler/parser/parser.rb +16 -5
- data/boot/rbx-compiler/parser/parser.y +39 -24
- data/doc/api/fancy.css +1 -1
- data/doc/api/fancy.jsonp +1 -1
- data/doc/api/fdoc.js +22 -4
- data/doc/api/index.html +5 -6
- data/doc/api/jquery-ui.min.js +401 -0
- data/doc/api/jquery.tools.min.js +192 -0
- data/doc/api/themeswitchertool.js +250 -0
- data/doc/features.md +17 -0
- data/examples/actor_bunnies.fy +32 -0
- data/examples/actors.fy +26 -0
- data/examples/actors_primitive.fy +27 -0
- data/examples/actors_ring.fy +37 -0
- data/examples/armstrong_numbers.fy +1 -1
- data/examples/array.fy +7 -9
- data/examples/async_send.fy +1 -2
- data/examples/blocks.fy +4 -4
- data/examples/call_with_receiver.fy +1 -1
- data/examples/class.fy +1 -1
- data/examples/default_args.fy +4 -1
- data/examples/define_methods.fy +2 -2
- data/examples/echo.fy +1 -1
- data/examples/factorial.fy +1 -1
- data/examples/future_composition.fy +2 -2
- data/examples/futures.fy +0 -5
- data/examples/game_of_life.fy +1 -1
- data/examples/person.fy +1 -1
- data/lib/argv.fy +7 -2
- data/lib/array.fy +109 -42
- data/lib/block.fy +39 -14
- data/lib/boot.fy +2 -0
- data/lib/class.fy +2 -0
- data/lib/compiler/ast.fy +2 -1
- data/lib/compiler/ast/assign.fy +2 -3
- data/lib/compiler/ast/async_send.fy +1 -15
- data/lib/compiler/ast/class_def.fy +2 -1
- data/lib/compiler/ast/expression_list.fy +4 -5
- data/lib/compiler/ast/future_send.fy +1 -10
- data/lib/compiler/ast/goto.fy +46 -0
- data/lib/compiler/ast/identifier.fy +9 -7
- data/lib/compiler/ast/literals.fy +8 -1
- data/lib/compiler/ast/match.fy +14 -4
- data/lib/compiler/ast/message_send.fy +34 -6
- data/lib/compiler/ast/method_def.fy +6 -6
- data/lib/compiler/ast/node.fy +3 -3
- data/lib/compiler/ast/range.fy +1 -0
- data/lib/compiler/ast/script.fy +0 -2
- data/lib/compiler/ast/singleton_method_def.fy +2 -4
- data/lib/compiler/ast/string_interpolation.fy +17 -0
- data/lib/compiler/ast/super.fy +5 -4
- data/lib/compiler/ast/try_catch.fy +8 -6
- data/lib/compiler/ast/tuple_literal.fy +3 -2
- data/lib/compiler/command.fy +0 -1
- data/lib/compiler/compiler.fy +1 -5
- data/lib/compiler/stages.fy +6 -14
- data/lib/documentation.fy +57 -46
- data/lib/enumerable.fy +257 -23
- data/lib/enumerator.fy +122 -15
- data/lib/false_class.fy +10 -1
- data/lib/fancy_spec.fy +263 -61
- data/lib/fdoc.fy +11 -25
- data/lib/fiber.fy +11 -0
- data/lib/file.fy +8 -11
- data/lib/future.fy +84 -5
- data/lib/hash.fy +65 -14
- data/lib/integer.fy +35 -0
- data/lib/iteration.fy +54 -29
- data/lib/message.fy +6 -0
- data/lib/method.fy +0 -16
- data/lib/nil_class.fy +58 -8
- data/lib/number.fy +49 -22
- data/lib/object.fy +371 -65
- data/lib/package.fy +24 -1
- data/lib/package/installer.fy +5 -9
- data/lib/package/specification.fy +2 -2
- data/lib/parser/ext/lexer.lex +15 -11
- data/lib/parser/ext/parser.y +70 -23
- data/lib/parser/methods.fy +33 -14
- data/lib/proxy.fy +33 -3
- data/lib/range.fy +28 -0
- data/lib/rbx.fy +3 -1
- data/lib/rbx/actor.fy +53 -0
- data/lib/rbx/alpha.fy +31 -0
- data/lib/rbx/array.fy +21 -12
- data/lib/rbx/bignum.fy +6 -2
- data/lib/rbx/block.fy +23 -26
- data/lib/rbx/class.fy +54 -2
- data/lib/rbx/code_loader.fy +8 -4
- data/lib/rbx/date.fy +9 -0
- data/lib/rbx/directory.fy +18 -0
- data/lib/rbx/environment_variables.fy +1 -0
- data/lib/rbx/exception.fy +9 -2
- data/lib/rbx/fiber.fy +22 -4
- data/lib/rbx/file.fy +5 -5
- data/lib/rbx/fixnum.fy +5 -0
- data/lib/rbx/float.fy +11 -3
- data/lib/rbx/hash.fy +31 -16
- data/lib/rbx/integer.fy +1 -0
- data/lib/rbx/io.fy +17 -7
- data/lib/rbx/match_data.fy +15 -4
- data/lib/rbx/method.fy +40 -7
- data/lib/rbx/name_error.fy +4 -0
- data/lib/rbx/object.fy +92 -24
- data/lib/rbx/range.fy +20 -6
- data/lib/rbx/regexp.fy +11 -3
- data/lib/rbx/string.fy +51 -1
- data/lib/rbx/stringio.fy +17 -0
- data/lib/rbx/symbol.fy +15 -1
- data/lib/rbx/system.fy +20 -2
- data/lib/rbx/tcp_server.fy +4 -1
- data/lib/rbx/tcp_socket.fy +11 -0
- data/lib/rbx/time.fy +6 -0
- data/lib/rbx/tuple.fy +14 -5
- data/lib/set.fy +144 -29
- data/lib/stack.fy +42 -11
- data/lib/string.fy +118 -8
- data/lib/struct.fy +13 -3
- data/lib/symbol.fy +21 -2
- data/lib/thread_pool.fy +2 -1
- data/lib/true_class.fy +45 -7
- data/lib/tuple.fy +27 -9
- data/lib/version.fy +2 -2
- data/ruby_lib/fancy +43 -0
- data/ruby_lib/fdoc +23 -0
- data/ruby_lib/fspec +3 -0
- data/ruby_lib/fyi +3 -0
- data/ruby_lib/ifancy +3 -0
- data/tests/argv.fy +5 -9
- data/tests/array.fy +323 -196
- data/tests/assignment.fy +29 -29
- data/tests/block.fy +72 -59
- data/tests/class.fy +227 -138
- data/tests/control_flow.fy +83 -51
- data/tests/documentation.fy +8 -8
- data/tests/enumerable.fy +8 -0
- data/tests/enumerator.fy +47 -29
- data/tests/exception.fy +49 -32
- data/tests/file.fy +28 -28
- data/tests/fixnum.fy +170 -0
- data/tests/future.fy +24 -7
- data/tests/hash.fy +55 -38
- data/tests/method.fy +50 -43
- data/tests/nil_class.fy +37 -37
- data/tests/object.fy +152 -70
- data/tests/pattern_matching.fy +67 -31
- data/tests/range.fy +6 -6
- data/tests/set.fy +101 -4
- data/tests/stack.fy +14 -5
- data/tests/string.fy +115 -61
- data/tests/stringio.fy +18 -0
- data/tests/struct.fy +27 -0
- data/tests/symbol.fy +19 -6
- data/tests/true_class.fy +34 -34
- data/tests/tuple.fy +30 -12
- metadata +103 -81
- data/boot/rbx-compiler/compiler/ast/require.rb +0 -20
- data/examples/actor.fy +0 -37
- data/examples/curl_async.fy +0 -37
- data/lib/compiler/ast/require.fy +0 -15
- data/tests/number.fy +0 -135
- data/tests/parsing/sexp.fy +0 -50
@@ -0,0 +1,32 @@
|
|
1
|
+
# Inspired by actors example at http://www.artima.com/weblogs/viewpost.jsp?thread=328540
|
2
|
+
|
3
|
+
class Bunny {
|
4
|
+
def initialize: @id {
|
5
|
+
self @@ hop
|
6
|
+
}
|
7
|
+
def hop {
|
8
|
+
self ++ " " print
|
9
|
+
self @@ hop
|
10
|
+
Thread sleep: 0.5
|
11
|
+
}
|
12
|
+
def stop: respond_to {
|
13
|
+
"Stopping #{self}" println
|
14
|
+
die!
|
15
|
+
respond_to @@ stopped
|
16
|
+
}
|
17
|
+
def to_s {
|
18
|
+
"Bunny(#{@id})"
|
19
|
+
}
|
20
|
+
}
|
21
|
+
|
22
|
+
@thread = Thread current # save current Thread so it can be resumed
|
23
|
+
def self stopped {
|
24
|
+
@amount = @amount - 1
|
25
|
+
{ @thread run } if: (@amount == 0)
|
26
|
+
}
|
27
|
+
|
28
|
+
@amount = 10
|
29
|
+
bunnies = (0..@amount) map: |i| { Bunny new: i }
|
30
|
+
Console readln: "\n\nPress ENTER to stop bunnies\n\n"
|
31
|
+
bunnies each: |b| { b @@ stop: self }
|
32
|
+
Thread stop # wait to be resumed when all bunnies are killed
|
data/examples/actors.fy
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# Does the same as actors_primitive.fy but using
|
2
|
+
# normal objects as actors by sending messages asynchronously (via @@ syntax).
|
3
|
+
class PingPong {
|
4
|
+
Done = false
|
5
|
+
def initialize: @block {
|
6
|
+
}
|
7
|
+
|
8
|
+
def count: count reply: other {
|
9
|
+
@block call
|
10
|
+
if: (count > 1000) then: {
|
11
|
+
count println
|
12
|
+
Done = true
|
13
|
+
} else: {
|
14
|
+
other @@ count: (count + 1) reply: self
|
15
|
+
}
|
16
|
+
}
|
17
|
+
}
|
18
|
+
|
19
|
+
pong = PingPong new: { "-" print }
|
20
|
+
ping = PingPong new: { "." print }
|
21
|
+
|
22
|
+
ping @@ count: 1 reply: pong
|
23
|
+
|
24
|
+
until: { PingPong Done } do: {
|
25
|
+
Thread sleep: 0.1
|
26
|
+
}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
pong = nil
|
2
|
+
done = false
|
3
|
+
|
4
|
+
ping = Actor spawn: {
|
5
|
+
loop: {
|
6
|
+
count = Actor receive
|
7
|
+
"." print
|
8
|
+
{ count println; done = true; break } if: (count > 1000)
|
9
|
+
pong ! (count + 1)
|
10
|
+
}
|
11
|
+
}
|
12
|
+
|
13
|
+
pong = Actor spawn: {
|
14
|
+
loop: {
|
15
|
+
count = Actor receive
|
16
|
+
"-" print
|
17
|
+
{ count println; done = true; break } if: (count > 1000)
|
18
|
+
ping ! (count + 1)
|
19
|
+
}
|
20
|
+
}
|
21
|
+
|
22
|
+
ping ! 1
|
23
|
+
|
24
|
+
# Let the actors process while the main thread sleeps...
|
25
|
+
until: { done } do: {
|
26
|
+
Thread sleep: 0.1
|
27
|
+
}
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class Ring {
|
2
|
+
class Node {
|
3
|
+
def initialize: @next ring: @ring { }
|
4
|
+
|
5
|
+
def count: count {
|
6
|
+
if: @next then: {
|
7
|
+
"." print
|
8
|
+
@next @@ count: (count + 1)
|
9
|
+
} else: {
|
10
|
+
"DONE: " ++ count println
|
11
|
+
@ring done: true
|
12
|
+
}
|
13
|
+
die! # let this actor die to free resources
|
14
|
+
}
|
15
|
+
}
|
16
|
+
|
17
|
+
read_write_slot: 'done
|
18
|
+
def initialize: amount {
|
19
|
+
node = nil
|
20
|
+
amount times: {
|
21
|
+
node = Node new: node ring: self
|
22
|
+
}
|
23
|
+
@start = node
|
24
|
+
@done = false
|
25
|
+
}
|
26
|
+
|
27
|
+
def start {
|
28
|
+
@start count: 1
|
29
|
+
}
|
30
|
+
}
|
31
|
+
|
32
|
+
# create ring and run through it
|
33
|
+
ring = Ring new: 4000
|
34
|
+
ring start
|
35
|
+
until: { ring done } do: {
|
36
|
+
Thread sleep: 0.1
|
37
|
+
}
|
data/examples/array.fy
CHANGED
@@ -5,9 +5,7 @@
|
|
5
5
|
arr = [1,2,3,4,5,6]
|
6
6
|
|
7
7
|
# print each element squared
|
8
|
-
arr each:
|
9
|
-
x squared println
|
10
|
-
}
|
8
|
+
arr each: @{ squared println }
|
11
9
|
|
12
10
|
# display each element with its index in the array
|
13
11
|
arr each_with_index: |x i| {
|
@@ -21,22 +19,22 @@ arr map: 'squared . inspect println
|
|
21
19
|
arr map: 'doubled . inspect println
|
22
20
|
|
23
21
|
# print array of all elements smaller than 4
|
24
|
-
arr select:
|
22
|
+
arr select: @{ < 4 } . inspect println
|
25
23
|
|
26
24
|
# print array of all elements that are not smaller than 4
|
27
|
-
arr reject:
|
25
|
+
arr reject: @{ < 4 } . inspect println
|
28
26
|
|
29
27
|
# prints: [5, 6]
|
30
|
-
arr take_while:
|
28
|
+
arr take_while: @{ < 5 } . inspect println
|
31
29
|
|
32
30
|
"testing reduce:init_val: " print
|
33
31
|
arr reduce: |acc x| { acc * x } init_val: 1 . println # same as: 1*1*2*3*4*5*6
|
34
32
|
|
35
33
|
"testing any?: " print
|
36
|
-
arr any?:
|
34
|
+
arr any?: @{ > 3 } . println # prints: true
|
37
35
|
|
38
36
|
"testing all?: " print
|
39
|
-
arr all?:
|
37
|
+
arr all?: @{ < 7 } . println # prints: true
|
40
38
|
|
41
39
|
"testing from:to: " print
|
42
40
|
arr [[3,5]] . inspect println # prints: [4, 5, 6]
|
@@ -49,4 +47,4 @@ arr size println # prints: 6
|
|
49
47
|
arr to_s println # prints: 123456
|
50
48
|
|
51
49
|
"testing inspect: " print
|
52
|
-
arr inspect println # prints: [1, 2, 3, 4, 5, 6]
|
50
|
+
arr inspect println # prints: [1, 2, 3, 4, 5, 6]
|
data/examples/async_send.fy
CHANGED
data/examples/blocks.fy
CHANGED
@@ -8,8 +8,8 @@ y = |x y| { Console println: $ x + y }
|
|
8
8
|
y call: [2, 3] # calls y and prints: 5
|
9
9
|
|
10
10
|
# prints numbers 0 to 20
|
11
|
-
|
12
|
-
while: {
|
13
|
-
Console println:
|
14
|
-
|
11
|
+
num = 0
|
12
|
+
while: { num <= 20 } do: {
|
13
|
+
Console println: num
|
14
|
+
num = num + 1
|
15
15
|
}
|
data/examples/class.fy
CHANGED
data/examples/default_args.fy
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# default_args.fy
|
2
2
|
# Example of fancy's default arguments
|
3
3
|
|
4
|
-
def arg1: arg1 arg2: arg2 ("default_arg2") arg3: arg3 ("default_arg3") {
|
4
|
+
def arg1: arg1 ("default_arg1") arg2: arg2 ("default_arg2") arg3: arg3 ("default_arg3") {
|
5
5
|
"arguments are: " println
|
6
6
|
arg1 println
|
7
7
|
arg2 println
|
@@ -15,3 +15,6 @@ arg1: "hello" arg2: "world"
|
|
15
15
|
|
16
16
|
Console newline
|
17
17
|
arg1: "hello"
|
18
|
+
|
19
|
+
Console newline
|
20
|
+
arg1
|
data/examples/define_methods.fy
CHANGED
@@ -4,12 +4,12 @@ arr define_singleton_method: "foo" with: {
|
|
4
4
|
self inspect println
|
5
5
|
}
|
6
6
|
|
7
|
-
arr
|
7
|
+
arr receive_message: 'foo
|
8
8
|
|
9
9
|
arr undefine_singleton_method: "foo"
|
10
10
|
|
11
11
|
try {
|
12
|
-
arr
|
12
|
+
arr receive_message: 'foo
|
13
13
|
} catch NoMethodError => e {
|
14
14
|
e println
|
15
15
|
}
|
data/examples/echo.fy
CHANGED
data/examples/factorial.fy
CHANGED
@@ -4,8 +4,8 @@ def do_large_computation: x {
|
|
4
4
|
x upto: (x ** x)
|
5
5
|
}
|
6
6
|
|
7
|
-
#
|
8
|
-
# and creates a new
|
7
|
+
# FutureSend#&& takes a Block (or a Callable - something that implements 'call:)
|
8
|
+
# and creates a new FutureSend that executes the given Block with the value of the first FutureSend
|
9
9
|
# when it has finished its computation. This makes pipelining tasks easy.
|
10
10
|
|
11
11
|
f = self @ do_large_computation: 5 && @{select: 'even?} && @{inspect println}
|
data/examples/futures.fy
CHANGED
data/examples/game_of_life.fy
CHANGED
data/examples/person.fy
CHANGED
data/lib/argv.fy
CHANGED
@@ -1,8 +1,13 @@
|
|
1
|
+
ARGV documentation: """
|
2
|
+
ARGV is a singleton instance of @Array@ and holds all (command-line)
|
3
|
+
arguments passed to a Fancy programm before it starts executing.
|
4
|
+
"""
|
5
|
+
|
1
6
|
def ARGV for_option: op_name do: block {
|
2
7
|
"Runs a given block if an option is in ARGV."
|
3
8
|
|
4
9
|
ARGV index: op_name . if_true: |idx| {
|
5
|
-
if: (block
|
10
|
+
if: (block arity > 0) then: {
|
6
11
|
ARGV[idx + 1] if_true: |arg| {
|
7
12
|
block call: [arg]
|
8
13
|
ARGV remove_at: idx
|
@@ -20,7 +25,7 @@ def ARGV for_options: op_names do: block {
|
|
20
25
|
|
21
26
|
op_names size times: |i| {
|
22
27
|
if: (ARGV index: (op_names[i])) then: |idx| {
|
23
|
-
if: (block
|
28
|
+
if: (block arity > 0) then: {
|
24
29
|
if: (ARGV[idx + 1]) then: |arg| {
|
25
30
|
block call: [arg]
|
26
31
|
ARGV remove_at: idx
|
data/lib/array.fy
CHANGED
@@ -8,13 +8,22 @@ class Array {
|
|
8
8
|
include: FancyEnumerable
|
9
9
|
|
10
10
|
def Array new: size {
|
11
|
-
"
|
11
|
+
"""
|
12
|
+
@size Initial size of the @Array@ to be created (values default to @nil).
|
13
|
+
|
14
|
+
Creates a new Array with a given @size (default value is @nil).
|
15
|
+
"""
|
12
16
|
|
13
17
|
Array new: size with: nil
|
14
18
|
}
|
15
19
|
|
16
20
|
def clone {
|
17
|
-
"
|
21
|
+
"""
|
22
|
+
@return A shallow copy of the @Array@.
|
23
|
+
|
24
|
+
Clones (shallow copy) the @Array@.
|
25
|
+
"""
|
26
|
+
|
18
27
|
new = []
|
19
28
|
each: |x| {
|
20
29
|
new << x
|
@@ -23,7 +32,17 @@ class Array {
|
|
23
32
|
}
|
24
33
|
|
25
34
|
def append: arr {
|
26
|
-
"
|
35
|
+
"""
|
36
|
+
@arr Other @Array@ to be appended to @self.
|
37
|
+
@return @self
|
38
|
+
|
39
|
+
Appends another @Array@ onto this one.
|
40
|
+
|
41
|
+
Example:
|
42
|
+
a = [1,2,3]
|
43
|
+
a append: [3,4,5]
|
44
|
+
a # => [1,2,3,3,4,5]
|
45
|
+
"""
|
27
46
|
|
28
47
|
arr each: |x| {
|
29
48
|
self << x
|
@@ -31,11 +50,13 @@ class Array {
|
|
31
50
|
self
|
32
51
|
}
|
33
52
|
|
34
|
-
def []
|
53
|
+
def [index] {
|
35
54
|
"""
|
36
|
-
|
37
|
-
|
38
|
-
|
55
|
+
@index Index to get the value for or @Array@ of 2 indices used for a sub-array.
|
56
|
+
@return Element(s) stored in @self at @index, possibly @nil or an empty @Array@.
|
57
|
+
|
58
|
+
Given an @Array@ of 2 @Fixnum@s, it returns the sub-array between the given indices.
|
59
|
+
If given a single @Fixnum@, returns the element at that index.
|
39
60
|
"""
|
40
61
|
|
41
62
|
# if given an Array, interpret it as a from:to: range subarray
|
@@ -47,34 +68,46 @@ class Array {
|
|
47
68
|
}
|
48
69
|
|
49
70
|
def first {
|
50
|
-
"
|
71
|
+
"""
|
72
|
+
@return The first element in the @Array@.
|
73
|
+
"""
|
51
74
|
at: 0
|
52
75
|
}
|
53
76
|
|
54
77
|
def second {
|
55
|
-
"
|
78
|
+
"""
|
79
|
+
@return The second element in the @Array@.
|
80
|
+
"""
|
56
81
|
at: 1
|
57
82
|
}
|
58
83
|
|
59
84
|
def third {
|
60
|
-
"
|
85
|
+
"""
|
86
|
+
@return The third element in the @Array@.
|
87
|
+
"""
|
61
88
|
at: 2
|
62
89
|
}
|
63
90
|
|
64
91
|
def fourth {
|
65
|
-
"
|
92
|
+
"""
|
93
|
+
@return The fourth element in the @Array@.
|
94
|
+
"""
|
66
95
|
at: 3
|
67
96
|
}
|
68
97
|
|
69
98
|
def rest {
|
70
|
-
"
|
99
|
+
"""
|
100
|
+
@return All elements in an @Array@ after the first one.
|
101
|
+
|
102
|
+
Returns all elements except the first one as a new @Array@.
|
103
|
+
"""
|
71
104
|
from: 1 to: -1
|
72
105
|
}
|
73
106
|
|
74
107
|
def each: block {
|
75
108
|
"""
|
76
109
|
@block @Block@ to be called for each element in @self.
|
77
|
-
@return
|
110
|
+
@return @self
|
78
111
|
|
79
112
|
Calls a given @Block@ with each element in the @Array@.
|
80
113
|
"""
|
@@ -86,17 +119,26 @@ class Array {
|
|
86
119
|
} catch (Fancy NextIteration) => ex {
|
87
120
|
}
|
88
121
|
}
|
89
|
-
self
|
122
|
+
return self
|
90
123
|
} catch (Fancy BreakIteration) => ex {
|
91
|
-
ex
|
124
|
+
ex result
|
92
125
|
}
|
93
126
|
}
|
94
127
|
|
128
|
+
def reverse_each: block {
|
129
|
+
size - 1 downto: 0 do: |i| {
|
130
|
+
block call: [at: i]
|
131
|
+
}
|
132
|
+
self
|
133
|
+
}
|
134
|
+
|
95
135
|
def each_with_index: block {
|
96
136
|
"""
|
97
137
|
@block @Block@ to be called with each element and its inde in the @Array@.
|
138
|
+
@return @self
|
98
139
|
|
99
|
-
Iterate over all elements in Array
|
140
|
+
Iterate over all elements in @Array@.
|
141
|
+
Calls a given @Block@ with each element and its index.
|
100
142
|
"""
|
101
143
|
|
102
144
|
i = 0
|
@@ -104,19 +146,19 @@ class Array {
|
|
104
146
|
block call: [x, i]
|
105
147
|
i = i + 1
|
106
148
|
}
|
107
|
-
nil
|
108
149
|
}
|
109
150
|
|
110
151
|
def =? other {
|
111
152
|
"""
|
112
153
|
@other Other @Array@ to compare this one to.
|
154
|
+
@return @true, if all elements of @other are in @self, @false otherwise.
|
113
155
|
|
114
156
|
Compares two Arrays where order does not matter.
|
115
157
|
"""
|
116
158
|
|
117
159
|
if: (other is_a?: Array) then: {
|
118
160
|
if: (size != (other size)) then: {
|
119
|
-
|
161
|
+
false
|
120
162
|
} else: {
|
121
163
|
all?: |x| { other includes?: x }
|
122
164
|
}
|
@@ -188,7 +230,7 @@ class Array {
|
|
188
230
|
@return Elements of @Array@ joined to a @String@.
|
189
231
|
|
190
232
|
Joins all elements with the empty @String@.
|
191
|
-
[\"hello\", \"world\", \"!\"] join # => \"hello,
|
233
|
+
[\"hello\", \"world\", \"!\"] join # => \"hello,world!\"
|
192
234
|
"""
|
193
235
|
|
194
236
|
join: ""
|
@@ -242,7 +284,9 @@ class Array {
|
|
242
284
|
}
|
243
285
|
|
244
286
|
def println {
|
245
|
-
"
|
287
|
+
"""
|
288
|
+
Prints each element on a seperate line.
|
289
|
+
"""
|
246
290
|
|
247
291
|
each: |x| {
|
248
292
|
x println
|
@@ -250,11 +294,41 @@ class Array {
|
|
250
294
|
}
|
251
295
|
|
252
296
|
def to_s {
|
253
|
-
"
|
297
|
+
"""
|
298
|
+
@return @String@ representation of @Array@.
|
299
|
+
|
300
|
+
Returns @String@ representation of @Array@.
|
301
|
+
"""
|
254
302
|
|
255
303
|
reduce: |x y| { x ++ y } init_val: ""
|
256
304
|
}
|
257
305
|
|
306
|
+
def inspect {
|
307
|
+
"""
|
308
|
+
@return Pretty-printed @String@ representation of @self.
|
309
|
+
|
310
|
+
Returns a pretty-printed @String@ representation of @self.
|
311
|
+
Example:
|
312
|
+
[1, 'foo, \"bar\", 42] inspect # => \"[1, 'foo, \\\"bar\\\", 42]\"
|
313
|
+
"""
|
314
|
+
|
315
|
+
str = "["
|
316
|
+
each: |x| {
|
317
|
+
str = str + (x inspect)
|
318
|
+
} in_between: {
|
319
|
+
str = str + ", "
|
320
|
+
}
|
321
|
+
str + "]"
|
322
|
+
}
|
323
|
+
|
324
|
+
def to_a {
|
325
|
+
"""
|
326
|
+
@return @self.
|
327
|
+
"""
|
328
|
+
|
329
|
+
self
|
330
|
+
}
|
331
|
+
|
258
332
|
def * num {
|
259
333
|
"""
|
260
334
|
Returns a new @Array@ that contains the elements of self num times
|
@@ -269,13 +343,23 @@ class Array {
|
|
269
343
|
}
|
270
344
|
|
271
345
|
def + other_arr {
|
272
|
-
"
|
346
|
+
"""
|
347
|
+
@return Concatenation of @self with another @Array@
|
348
|
+
|
349
|
+
Returns concatenation with another @Array@.
|
350
|
+
[1,2,3] + [3,4,5] # => [1,2,3,3,4,5]
|
351
|
+
"""
|
273
352
|
|
274
353
|
clone append: other_arr
|
275
354
|
}
|
276
355
|
|
277
356
|
def indices {
|
278
|
-
"
|
357
|
+
"""
|
358
|
+
@return @Array@ of all indices of @self.
|
359
|
+
|
360
|
+
Returns an @Array@ of all the indices of an @Array@.
|
361
|
+
[1,2,3] indices # => [0,1,2]
|
362
|
+
"""
|
279
363
|
|
280
364
|
0 upto: (size - 1)
|
281
365
|
}
|
@@ -286,6 +370,7 @@ class Array {
|
|
286
370
|
@return @Array@ of all indices for a given value within an @Array@ (possibly empty).
|
287
371
|
|
288
372
|
Returns an Array of all indices of this item. Empty Array if item does not occur.
|
373
|
+
[1, 'foo, 2, 'foo] indices_of: 'foo # => [1, 3]
|
289
374
|
"""
|
290
375
|
|
291
376
|
tmp = []
|
@@ -338,7 +423,7 @@ class Array {
|
|
338
423
|
|
339
424
|
def select_with_index: block {
|
340
425
|
"""
|
341
|
-
Same as select:, just gets also called with an additional argument
|
426
|
+
Same as #select:, just gets also called with an additional argument
|
342
427
|
for each element's index value.
|
343
428
|
"""
|
344
429
|
|
@@ -363,22 +448,4 @@ class Array {
|
|
363
448
|
return [object] + object
|
364
449
|
}
|
365
450
|
}
|
366
|
-
|
367
|
-
def sum {
|
368
|
-
"""
|
369
|
-
Calculates the sum of all the elements in the Enumerable
|
370
|
-
(assuming them to be Numbers (implementing '+' & '*')).
|
371
|
-
"""
|
372
|
-
|
373
|
-
reduce: |x y| { x + y } init_val: 0
|
374
|
-
}
|
375
|
-
|
376
|
-
def product {
|
377
|
-
"""
|
378
|
-
Calculates the product of all the elements in the Enumerable
|
379
|
-
(assuming them to be Numbers (implementing '+' & '*')).
|
380
|
-
"""
|
381
|
-
|
382
|
-
reduce: |x y| { x * y } init_val: 1
|
383
|
-
}
|
384
451
|
}
|