Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
8f67a0d0
Unverified
Commit
8f67a0d0
authored
Nov 04, 2021
by
Yukihiro "Matz" Matsumoto
Committed by
GitHub
Nov 04, 2021
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5569 from dearblue/array-product
Added `Array#product` method
parents
b940ed69
5ea26260
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
0 deletions
+69
-0
mrbgems/mruby-array-ext/mrblib/array.rb
mrbgems/mruby-array-ext/mrblib/array.rb
+47
-0
mrbgems/mruby-array-ext/test/array.rb
mrbgems/mruby-array-ext/test/array.rb
+22
-0
No files found.
mrbgems/mruby-array-ext/mrblib/array.rb
View file @
8f67a0d0
...
...
@@ -896,4 +896,51 @@ class Array
alias
append
push
alias
prepend
unshift
alias
filter!
select
!
##
# call-seq:
# ary.product(*arys) -> array
# ary.product(*arys) { |item| ... } -> self
def
product
(
*
arys
,
&
block
)
size
=
arys
.
size
i
=
size
while
i
>
0
i
-=
1
unless
arys
[
i
].
kind_of?
(
Array
)
raise
TypeError
,
"no implicit conversion into Array"
end
end
i
=
size
total
=
self
.
size
total
*=
arys
[
i
-=
1
].
size
while
i
>
0
if
block
result
=
self
list
=
->
(
*
,
e
)
{
block
.
call
e
}
class
<<
list
;
alias
[]
=
call
;
end
else
result
=
[
nil
]
*
total
list
=
result
end
i
=
0
while
i
<
total
group
=
[
nil
]
*
(
size
+
1
)
j
=
size
n
=
i
while
j
>
0
j
-=
1
a
=
arys
[
j
]
b
=
a
.
size
group
[
j
+
1
]
=
a
[
n
%
b
]
n
/=
b
end
group
[
0
]
=
self
[
n
]
list
[
i
]
=
group
i
+=
1
end
result
end
end
mrbgems/mruby-array-ext/test/array.rb
View file @
8f67a0d0
...
...
@@ -421,3 +421,25 @@ assert('Array#transpose') do
assert_raise
(
TypeError
)
{
[
1
].
transpose
}
assert_raise
(
IndexError
)
{
[[
1
],
[
2
,
3
,
4
]].
transpose
}
end
assert
"Array#product"
do
assert_equal
[[
1
],
[
2
],
[
3
]],
[
1
,
2
,
3
].
product
assert_equal
[],
[
1
,
2
,
3
].
product
([])
assert_equal
[],
[
1
,
2
,
3
].
product
([
4
,
5
,
6
],
[])
expect
=
[[
1
,
5
,
8
],
[
1
,
5
,
9
],
[
1
,
6
,
8
],
[
1
,
6
,
9
],
[
1
,
7
,
8
],
[
1
,
7
,
9
],
[
2
,
5
,
8
],
[
2
,
5
,
9
],
[
2
,
6
,
8
],
[
2
,
6
,
9
],
[
2
,
7
,
8
],
[
2
,
7
,
9
],
[
3
,
5
,
8
],
[
3
,
5
,
9
],
[
3
,
6
,
8
],
[
3
,
6
,
9
],
[
3
,
7
,
8
],
[
3
,
7
,
9
],
[
4
,
5
,
8
],
[
4
,
5
,
9
],
[
4
,
6
,
8
],
[
4
,
6
,
9
],
[
4
,
7
,
8
],
[
4
,
7
,
9
]]
assert_equal
expect
,
[
1
,
2
,
3
,
4
].
product
([
5
,
6
,
7
],
[
8
,
9
])
expect
=
[[
1
,
4
,
7
],
[
1
,
4
,
8
],
[
1
,
4
,
9
],
[
1
,
5
,
7
],
[
1
,
5
,
8
],
[
1
,
5
,
9
],
[
1
,
6
,
7
],
[
1
,
6
,
8
],
[
1
,
6
,
9
],
[
2
,
4
,
7
],
[
2
,
4
,
8
],
[
2
,
4
,
9
],
[
2
,
5
,
7
],
[
2
,
5
,
8
],
[
2
,
5
,
9
],
[
2
,
6
,
7
],
[
2
,
6
,
8
],
[
2
,
6
,
9
],
[
3
,
4
,
7
],
[
3
,
4
,
8
],
[
3
,
4
,
9
],
[
3
,
5
,
7
],
[
3
,
5
,
8
],
[
3
,
5
,
9
],
[
3
,
6
,
7
],
[
3
,
6
,
8
],
[
3
,
6
,
9
]]
assert_equal
expect
,
[
1
,
2
,
3
].
product
([
4
,
5
,
6
],
[
7
,
8
,
9
])
base
=
[
1
,
2
,
3
]
x
=
[]
assert_equal
base
,
base
.
product
([
4
,
5
,
6
],
[
7
,
8
,
9
])
{
|
e
|
x
<<
e
}
assert_equal
expect
,
x
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment