Skip to content

Commit

Permalink
Merge branch 'master' into fibers
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Gaynor committed Mar 19, 2013
2 parents eb0320b + 7c53873 commit 6d13e55
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 49 deletions.
8 changes: 6 additions & 2 deletions lib-topaz/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,12 @@ def delete(obj, &block)
return nil
end

def first
return self[0]
def first(*args)
if args.empty?
self[0]
else
take(*args)
end
end

def flatten(level = -1)
Expand Down
32 changes: 29 additions & 3 deletions lib-topaz/enumerable.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
module Enumerable
def first(*args)
if args.empty?
self.each { |e| return e }
nil
else
take(*args)
end
end

def map
result = []
self.each do |x|
Expand All @@ -9,9 +18,25 @@ def map

alias collect map

def inject(memo)
self.each do |x|
memo = (yield memo, x)
def inject(*args)
dropped = 0
meth = nil
case args.length
when 0
memo = self.first
dropped = 1
when 1
memo = args[0]
when 2
memo = args[0]
meth = args[1]
end
self.drop(dropped).each do |x|
if meth
memo = memo.send(meth, x)
else
memo = (yield memo, x)
end
end
memo
end
Expand Down Expand Up @@ -95,6 +120,7 @@ def detect(ifnone = nil, &block)
alias find detect

def take(n)
n = Topaz.convert_type(n, Fixnum, :to_int)
raise ArgumentError.new("attempt to take negative size") if n < 0
result = []
unless n == 0
Expand Down
8 changes: 8 additions & 0 deletions lib-topaz/range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ def each(&block)
self
end

def first(*args)
if args.empty?
self.begin
else
take(*args)
end
end

def ===(value)
self.include?(value)
end
Expand Down
11 changes: 0 additions & 11 deletions spec/tags/core/array/first_tags.txt
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
fails:Array#first returns the first count elements if given a count
fails:Array#first returns an empty array when passed count on an empty array
fails:Array#first returns an empty array when passed count == 0
fails:Array#first returns an array containing the first element when passed count == 1
fails:Array#first returns the entire array when count > length
fails:Array#first returns an array which is independent to the original when passed count
fails:Array#first tries to convert the passed argument to an Integer using #to_int
fails:Array#first raises a TypeError if the passed argument is not numeric
fails:Array#first does not return subclass instance when passed count on Array subclasses
fails:Array#first is not destructive
fails:Array#first
6 changes: 0 additions & 6 deletions spec/tags/core/array/take_tags.txt

This file was deleted.

11 changes: 0 additions & 11 deletions spec/tags/core/enumerable/first_tags.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1 @@
fails:Enumerable#first returns the first element
fails:Enumerable#first returns nil if self is empty
fails:Enumerable#first when passed an argument returns the first count elements if given a count
fails:Enumerable#first when passed an argument returns an empty array when passed count on an empty array
fails:Enumerable#first when passed an argument returns an empty array when passed count == 0
fails:Enumerable#first when passed an argument returns an array containing the first element when passed count == 1
fails:Enumerable#first when passed an argument raises an ArgumentError when count is negative
fails:Enumerable#first when passed an argument returns the entire array when count > length
fails:Enumerable#first when passed an argument tries to convert the passed argument to an Integer using #to_int
fails:Enumerable#first when passed an argument raises a TypeError if the passed argument is not numeric
fails:Enumerable#first when passed an argument gathers whole arrays as elements when each yields multiple
fails:Enumerable#first when passed an argument consumes only what is needed
5 changes: 0 additions & 5 deletions spec/tags/core/enumerable/inject_tags.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
fails:Enumerable#inject can take two argument
fails:Enumerable#inject ignores the block if two arguments
fails:Enumerable#inject can take a symbol argument
fails:Enumerable#inject without argument takes a block with an accumulator (with first element as initial value) and the current element. Value of block becomes new accumulator
fails:Enumerable#inject gathers whole arrays as elements when each yields multiple
fails:Enumerable#inject without inject arguments(legacy rubycon)
fails:Enumerable#inject returns nil when fails(legacy rubycon)
2 changes: 0 additions & 2 deletions spec/tags/core/enumerable/take_tags.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
fails:Enumerable#take when passed an argument tries to convert the passed argument to an Integer using #to_int
fails:Enumerable#take when passed an argument raises a TypeError if the passed argument is not numeric
fails:Enumerable#take when passed an argument gathers whole arrays as elements when each yields multiple
7 changes: 0 additions & 7 deletions spec/tags/core/range/first_tags.txt

This file was deleted.

7 changes: 6 additions & 1 deletion topaz/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@
""
])
COPYRIGHT = "topaz - Copyright (c) Alex Gaynor and individual contributors\n"
RUBY_REVISION = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]).rstrip()
RUBY_REVISION = subprocess.check_output([
"git",
"--git-dir", os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir, ".git"),
"rev-parse", "--short", "HEAD"
]).rstrip()



@specialize.memo()
Expand Down
7 changes: 7 additions & 0 deletions topaz/modules/topaz.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from rpython.rlib.rarithmetic import intmask

from topaz.module import Module, ModuleDef
from topaz.objects.classobject import W_ClassObject


class Topaz(Module):
Expand All @@ -15,3 +16,9 @@ def method_intmask(self, space, w_int):
elif space.is_kind_of(w_int, space.w_bignum):
bigint = space.bigint_w(w_int)
return space.newint(intmask(bigint.uintmask()))

@moduledef.function("convert_type", method="symbol")
def method_coerce_int(self, space, w_obj, w_type, method):
if not isinstance(w_type, W_ClassObject):
raise space.error(space.w_TypeError, "type argument must be a class")
return space.convert_type(w_obj, w_type, method)
1 change: 0 additions & 1 deletion topaz/objects/rangeobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def method_initialize(self, space, w_start, w_end, excl=False):
self.w_end = w_end
self.exclusive = excl

@classdef.method("first")
@classdef.method("begin")
def method_begin(self, space):
return self.w_start
Expand Down

0 comments on commit 6d13e55

Please sign in to comment.