Skip to content

Commit

Permalink
Enable currying of procs, fixes most related specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
aardvark179 committed Jan 19, 2017
1 parent 5c4ecb5 commit 6a3bcd3
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 22 deletions.
1 change: 1 addition & 0 deletions maven/jruby-jars/.gitignore
Expand Up @@ -2,3 +2,4 @@ lib/*jar
pkg
pom.jruby-jars.gemspec.xml
lib/jruby-jars/version.rb
/bin/
20 changes: 0 additions & 20 deletions spec/truffle/tags/core/proc/curry_tags.txt
@@ -1,23 +1,3 @@
fails:Proc#curry returns a Proc when called on a proc
fails:Proc#curry returns a Proc when called on a lambda
fails:Proc#curry calls the curried proc with the arguments if sufficient arguments have been given
fails:Proc#curry returns a Proc that consumes the remainder of the arguments unless 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 raises an ArgumentError if passed superfluous arguments when created from a lambda
fails:Proc#curry returns Procs with arities of -1
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 produces Procs that can be passed as the block for instance_exec
fails:Proc#curry with arity argument accepts an optional Integer argument for the arity
fails:Proc#curry with arity argument returns a Proc when called on a proc
fails:Proc#curry with arity argument returns a Proc when called on 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 calls the curried proc with the arguments if _arity_ arguments have been given
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 can be specified multiple times on the same Proc
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 returns Procs with arities of -1 regardless of the value of _arity_
fails:Proc#curry combines arguments and calculates incoming arity accurately for successively currying
fails:Proc#curry with arity argument raises an ArgumentError if called on a lambda that requires fewer than _arity_ arguments
Expand Up @@ -1723,6 +1723,7 @@ public Object getTruffleKernelModule() {
"/core/string_mirror.rb",
"/core/module.rb",
"/core/proc.rb",
"/core/proc_mirror.rb",
"/core/enumerable_helper.rb",
"/core/enumerable.rb",
"/core/enumerator.rb",
Expand Down
1 change: 1 addition & 0 deletions truffle/src/main/ruby/core/mirror.rb
Expand Up @@ -44,6 +44,7 @@ def self.module_mirror(obj)
when ::String then Rubinius::Mirror::String
when ::Range then Rubinius::Mirror::Range
when ::Process then Rubinius::Mirror::Process
when ::Proc then Rubinius::Mirror::Proc
else
begin
Rubinius::Mirror.const_get(obj.class.name.to_sym, false)
Expand Down
4 changes: 2 additions & 2 deletions truffle/src/main/ruby/core/proc.rb
Expand Up @@ -55,8 +55,8 @@ def curry(curried_arity = nil)
raise ArgumentError, "cannot create binding from f proc"
end

f.singleton_class.thunk_method :parameters, [[:rest]]
f.singleton_class.thunk_method :source_location, nil
# f.singleton_class.thunk_method :parameters, [[:rest]]
# f.singleton_class.thunk_method :source_location, nil

f
end
Expand Down
63 changes: 63 additions & 0 deletions truffle/src/main/ruby/core/proc_mirror.rb
@@ -0,0 +1,63 @@
# Copyright (c) 2007-2015, Evan Phoenix and contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Rubinius nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

module Rubinius
class Mirror
class Proc < Mirror
def self.from_block(klass, env)
begin
return Rubinius.invoke_primitive :proc_from_env, env, klass
rescue Rubinius::Internal => exc
if Type.object_kind_of? env, BlockEnvironment
msg = "unable to create Proc from BlockEnvironment"
raise PrimitiveFailure, msg, exc
end
end

begin
env.to_proc
rescue Exception
raise ArgumentError, "Unable to convert #{env.inspect} to a Proc"
end
end

def curry(executable, args, arity)
args.freeze

name = executable.lambda? ? :lambda : :proc

Proc.__send__(name) do |*a, &b|
all_args = args + a
if all_args.size < arity
curry executable, all_args, arity
else
executable[*all_args]
end
end
end
end
end
end

0 comments on commit 6a3bcd3

Please sign in to comment.