Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 69936a1f71f3
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ba569698058c
Choose a head ref
  • 4 commits
  • 2 files changed
  • 2 contributors

Commits on May 11, 2015

  1. Partially fixed specs for Array#combination. Currently works when a b…

    …lock is passed but not when an Enumerable#to_a is called.
    Sidu Ponnappa committed May 11, 2015
    Copy the full SHA
    b39b348 View commit details

Commits on May 12, 2015

  1. Fixed issue caused by yielding an array instead of a copy of the array

    Replaced self.dup with `self.slice()`
    Moved var declarations to the top to reflect hoisting
    Sidu Ponnappa committed May 12, 2015
    Copy the full SHA
    7faf74b View commit details

Commits on May 14, 2015

  1. Missed declaring var length; fixed

    Sidu Ponnappa committed May 14, 2015
    Copy the full SHA
    b871c2b View commit details
  2. Merge pull request #848 from c42engineering/rubyspec_array_combination

    Rubyspec: Array#combination
    vais committed May 14, 2015

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    ba56969 View commit details
Showing with 55 additions and 11 deletions.
  1. +55 −1 opal/corelib/array.rb
  2. +0 −10 spec/filters/bugs/array.rb
56 changes: 55 additions & 1 deletion opal/corelib/array.rb
Original file line number Diff line number Diff line change
@@ -573,7 +573,61 @@ def collect!(&block)

self
end


def combination(n)
num = Opal.coerce_to! n, Integer, :to_int
return enum_for :combination, num unless block_given?

%x{
var i;
var length;
var stack;
var chosen;
var lev;
var done;
var next;
if (num === 0) {
#{yield []}
} else if (num === 1) {
for (i = 0, length = self.length; i < length; i++) {
#{yield `[self[i]]`}
}
}
else if (num === self.length) {
#{yield `self.slice()`}
}
else if (num >= 0 && num < self.length) {
stack = [];
for (i = 0; i <= num + 1; i++) {
stack.push(0);
}
chosen = [];
lev = 0;
done = false;
stack[0] = -1;
while (!done) {
chosen[lev] = self[stack[lev+1]];
while (lev < num - 1) {
lev++;
next = stack[lev+1] = stack[lev] + 1;
chosen[lev] = self[next];
}
#{ yield `chosen.slice()` }
lev++;
do {
done = (lev === 0);
stack[lev]++;
lev--;
} while ( stack[lev+1] + num === self.length + lev + 1 );
}
}
}
self
end

def compact
%x{
var result = [];
10 changes: 0 additions & 10 deletions spec/filters/bugs/array.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
opal_filter "Array" do
fails "Array#clone copies singleton methods"

fails "Array#combination generates from a defensive copy, ignoring mutations"
fails "Array#combination yields a partition consisting of only singletons"
fails "Array#combination yields [] when length is 0"
fails "Array#combination yields a copy of self if the argument is the size of the receiver"
fails "Array#combination yields nothing if the argument is out of bounds"
fails "Array#combination yields the expected combinations"
fails "Array#combination yields nothing for out of bounds length and return self"
fails "Array#combination returns self when a block is given"
fails "Array#combination returns an enumerator when no block is provided"

fails "Array#<=> calls <=> left to right and return first non-0 result"
fails "Array#<=> returns -1 if the arrays have same length and a pair of corresponding elements returns -1 for <=>"
fails "Array#<=> returns +1 if the arrays have same length and a pair of corresponding elements returns +1 for <=>"