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: d18909fbe586
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: f5f2c5cb35d1
Choose a head ref
  • 2 commits
  • 3 files changed
  • 2 contributors

Commits on Jul 16, 2015

  1. Implement Proc#curry

    Fix #1009
    vais committed Jul 16, 2015
    Copy the full SHA
    fdfe1c9 View commit details
  2. Merge pull request #1011 from vais/proc-curry

    Implement Proc#curry
    elia committed Jul 16, 2015
    Copy the full SHA
    f5f2c5c View commit details
Showing with 54 additions and 23 deletions.
  1. +53 −0 opal/corelib/proc.rb
  2. +1 −0 spec/filters/bugs/kernel.rb
  3. +0 −23 spec/filters/bugs/proc.rb
53 changes: 53 additions & 0 deletions opal/corelib/proc.rb
Original file line number Diff line number Diff line change
@@ -47,6 +47,59 @@ def lambda?

# FIXME: this should support the various splats and optional arguments
def arity
`if (self.$$is_curried) { return -1; }`
`self.length`
end

def source_location
`if (self.$$is_curried) { return nil; }`
nil
end

def binding
`if (self.$$is_curried) { #{raise ArgumentError, "Can't create Binding"} }`
nil
end

def parameters
`if (self.$$is_curried) { return #{[[:rest]]}; }`
nil
end

def curry(arity = undefined)
%x{
if (arity === undefined) {
arity = self.length;
}
else {
arity = #{Opal.coerce_to!(arity, Integer, :to_int)};
if (self.$$is_lambda && arity !== self.length) {
#{raise ArgumentError, "wrong number of arguments (#{`arity`} for #{`self.length`})"}
}
}
function curried () {
var args = $slice.call(arguments),
length = args.length;
if (length > arity && self.$$is_lambda) {
#{raise ArgumentError, "wrong number of arguments (#{`length`} for #{`arity`})"}
}
if (length >= arity) {
return self.$call.apply(self, args);
}
return function () {
return curried.apply(null,
args.concat($slice.call(arguments)));
}
};
curried.$$is_lambda = self.$$is_lambda;
curried.$$is_curried = true;
return curried;
}
end

end
1 change: 1 addition & 0 deletions spec/filters/bugs/kernel.rb
Original file line number Diff line number Diff line change
@@ -42,6 +42,7 @@
fails "Kernel#eval allows creating a new class in a binding"
fails "Kernel#eval coerces an object to string"
fails "Kernel#eval does not alter the value of __FILE__ in the binding"
fails "Kernel#eval does not make Proc locals visible to evaluated code"
fails "Kernel#eval does not share locals across eval scopes"
fails "Kernel#eval doesn't accept a Proc object as a binding"
fails "Kernel#eval evaluates such that consts are scoped to the class of the eval"
23 changes: 0 additions & 23 deletions spec/filters/bugs/proc.rb
Original file line number Diff line number Diff line change
@@ -18,30 +18,7 @@
fails "Proc#call on a Proc created with Kernel#lambda or Kernel#proc raises an ArgumentError on missing arguments when self is a lambda"
fails "Proc#call on a Proc created with Kernel#lambda or Kernel#proc raises an ArgumentError on missing arguments when self is a lambda"
fails "Proc#clone returns a copy of self"
fails "Proc#curry calls the curried proc with the arguments if sufficient arguments have been given"
fails "Proc#curry can be called multiple times on the same Proc"
fails "Proc#curry can be passed superfluous arguments if created from a proc"
fails "Proc#curry combines arguments and calculates incoming arity accurately for successively currying"
fails "Proc#curry produces Procs that can be passed as the block for instance_exec"
fails "Proc#curry produces Procs that raise ArgumentError for #binding"
fails "Proc#curry produces Procs that return [[:rest]] for #parameters"
fails "Proc#curry produces Procs that return nil for #source_location"
fails "Proc#curry raises an ArgumentError if passed superfluous arguments when created from a lambda"
fails "Proc#curry returns Procs with arities of -1"
fails "Proc#curry returns a Proc that consumes the remainder of the arguments unless sufficient arguments have been given"
fails "Proc#curry returns a Proc when called on a lambda"
fails "Proc#curry returns a Proc when called on a proc"
fails "Proc#curry with arity argument accepts an optional Integer argument for the arity"
fails "Proc#curry with arity argument calls the curried proc with the arguments if _arity_ arguments have been given"
fails "Proc#curry with arity argument can be specified multiple times on the same Proc"
fails "Proc#curry with arity argument raises an ArgumentError if called on a lambda that requires fewer than _arity_ arguments"
fails "Proc#curry with arity argument raises an ArgumentError if called on a lambda that requires more than _arity_ arguments"
fails "Proc#curry with arity argument raises an ArgumentError if passed more than _arity_ arguments when created from a lambda"
fails "Proc#curry with arity argument retains the lambda-ness of the Proc on which its called"
fails "Proc#curry with arity argument returns Procs with arities of -1 regardless of the value of _arity_"
fails "Proc#curry with arity argument returns a Proc that consumes the remainder of the arguments when fewer than _arity_ arguments are given"
fails "Proc#curry with arity argument returns a Proc when called on a lambda"
fails "Proc#curry with arity argument returns a Proc when called on a proc"
fails "Proc#dup returns a copy of self"
fails "Proc#hash returns an Integer"
fails "Proc#inspect for a proc created with UnboundMethod#to_proc returns a description including '(lambda)' and optionally including file and line number"