Skip to content

Commit

Permalink
Merge pull request #507 from kostya/first
Browse files Browse the repository at this point in the history
Enumerable#first
  • Loading branch information
alex committed Mar 19, 2013
2 parents ba9afd9 + 97c6da6 commit 196b052
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 40 deletions.
8 changes: 6 additions & 2 deletions lib-topaz/array.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -137,8 +137,12 @@ def delete(obj, &block)
return nil return nil
end end


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


def flatten(level = -1) def flatten(level = -1)
Expand Down
11 changes: 11 additions & 0 deletions lib-topaz/enumerable.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,14 @@
module Enumerable module Enumerable

def first(*args)
if args.empty?
self.each { |e| return e }
nil
else
take(*args)
end
end

def map def map
result = [] result = []
self.each do |x| self.each do |x|
Expand Down Expand Up @@ -95,6 +105,7 @@ def detect(ifnone = nil, &block)
alias find detect alias find detect


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


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

def ===(value) def ===(value)
self.include?(value) self.include?(value)
end end
Expand Down
11 changes: 0 additions & 11 deletions spec/tags/core/array/first_tags.txt
Original file line number Original file line 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 Original file line 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 gathers whole arrays as elements when each yields multiple
fails:Enumerable#first when passed an argument consumes only what is needed
2 changes: 0 additions & 2 deletions spec/tags/core/enumerable/take_tags.txt
Original file line number Original file line 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 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.

4 changes: 4 additions & 0 deletions topaz/modules/topaz.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ def method_intmask(self, space, w_int):
elif space.is_kind_of(w_int, space.w_bignum): elif space.is_kind_of(w_int, space.w_bignum):
bigint = space.bigint_w(w_int) bigint = space.bigint_w(w_int)
return space.newint(intmask(bigint.uintmask())) return space.newint(intmask(bigint.uintmask()))

@moduledef.function("coerce_int")
def method_coerce_int(self, space, w_obj):
return space.convert_type(w_obj, space.w_fixnum, "to_int")
1 change: 0 additions & 1 deletion topaz/objects/rangeobject.py
Original file line number Original file line 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.w_end = w_end
self.exclusive = excl self.exclusive = excl


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

0 comments on commit 196b052

Please sign in to comment.