seq 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -51,11 +51,11 @@ s.next #=> nil
51
51
 
52
52
  # Standard methods will increment the number of items that have been returned meaning
53
53
  # you have to call #reset
54
- s.reset
54
+ s.reset
55
55
  s.next #=> 1
56
56
 
57
57
  # You can use special ! versions to avoid incrementing the number of cycles, but note
58
- # this will start and finish based on the current index and items returned. So here
58
+ # this will start and finish based on the current index and items returned. So here
59
59
  # we are on the second item in the original list, ie. "2".
60
60
 
61
61
  s.map! {|i| i * 2 } #=> [4, 6, 8, 2, 4, 6, 8, 2, 4]
@@ -88,8 +88,19 @@ Lazily evaluates a block using starting list given.
88
88
 
89
89
  ```ruby
90
90
  fibs = Seq::Lazy.new([1,1]) {|list| list[-1] + list[-2] }
91
- fibs.take(10) #=> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
91
+ fibs.take(10) #=> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
92
92
  fibs.take(20) #=> [89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040]
93
93
  fibs.next! #=> 832040
94
94
  fibs.reset
95
95
  ```
96
+
97
+
98
+ ## Seq::Paged
99
+
100
+ Returns elements from a page one at a time.
101
+
102
+ ```ruby
103
+ s = Seq::Paged.new {|page| [page, page+1, page+2] }
104
+ s.take(10) #=> [0, 1, 2, 1, 2, 3, 2, 3, 4, 3]
105
+ # extra spacing added to show pages
106
+ ```
data/lib/seq.rb CHANGED
@@ -43,17 +43,17 @@ class Seq
43
43
  @items = items
44
44
  @offset = offset
45
45
  @default = default
46
-
46
+
47
47
  self.reset
48
48
  end
49
-
49
+
50
50
  # Resets the Seqs position to the same as when initialized.
51
51
  def reset
52
52
  @cycles = 0
53
53
  @index = @offset
54
54
  end
55
-
56
- # @return Until ended it returns the next item from the list, when ended it returns
55
+
56
+ # @return Until ended it returns the next item from the list, when ended it returns
57
57
  # the default item.
58
58
  def next
59
59
  if ended?
@@ -62,27 +62,27 @@ class Seq
62
62
  @list[@index].tap { inc }
63
63
  end
64
64
  end
65
-
66
- # Increment the list index, the number of cycles completed and if at the end of
65
+
66
+ # Increment the list index, the number of cycles completed and if at the end of
67
67
  # the list returns to the first item.
68
- #
68
+ #
69
69
  # @return [Integer] Number of items that have been returned.
70
70
  def inc
71
71
  if @index+1 == @list.size
72
- @index = 0
72
+ @index = 0
73
73
  else
74
74
  @index += 1
75
75
  end
76
76
  @cycles += 1
77
77
  end
78
-
78
+
79
79
  # Iterates over each item as returned by #next until #ended?.
80
80
  def each
81
81
  until ended?
82
82
  yield self.next
83
83
  end
84
84
  end
85
-
85
+
86
86
  include Enumerable
87
87
 
88
88
  # @return [Array] The items that would be returned by repeated calls to #next
@@ -90,7 +90,7 @@ class Seq
90
90
  # @raise [RangeError] If #infinite?, otherwise it creates an infinite loop!
91
91
  def entries
92
92
  raise RangeError if infinite?
93
-
93
+
94
94
  i, c = @index, @cycles
95
95
  r = super
96
96
  @index, @cycles = i, c
@@ -101,7 +101,7 @@ class Seq
101
101
  def to_a
102
102
  @list
103
103
  end
104
-
104
+
105
105
  # @return Whether the Seq returns infinite items.
106
106
  def infinite?
107
107
  @items == Float::INFINITY
@@ -111,15 +111,15 @@ class Seq
111
111
  def ended?
112
112
  @cycles >= @items
113
113
  end
114
-
114
+
115
115
  # Any method called on a Seq with ending with !, will be caught by method
116
116
  # missing, it will then attempt to call the non ! version but will not
117
- # alter the index or number of cycles completed.
117
+ # alter the index or number of cycles completed.
118
118
  def method_missing(sym, *args, &block)
119
- if sym.to_s[-1] == "!" &&
119
+ if sym.to_s[-1] == "!" &&
120
120
  self.respond_to?(sym.to_s[0..-2].to_sym) &&
121
121
  ! [:infinite?, :ended?, :to_a, :entries, :inc, :reset].include?(sym.to_s[0..-2].to_sym)
122
-
122
+
123
123
  begin
124
124
  i, c = @index, @cycles
125
125
  self.send(sym.to_s[0..-2].to_sym, *args, &block)
@@ -133,5 +133,6 @@ class Seq
133
133
  end
134
134
 
135
135
  require 'seq/lazy'
136
+ require 'seq/paged'
136
137
  require 'seq/random'
137
138
  require 'seq/version'
@@ -19,7 +19,7 @@ class Seq
19
19
  # fibs.take!(2) #=> [21, 34]
20
20
  #
21
21
  class Lazy < Seq
22
-
22
+
23
23
  # Creates a new Lazy seq instance.
24
24
  #
25
25
  # @param list [Array] Starting values
@@ -35,24 +35,24 @@ class Seq
35
35
  @block = block
36
36
  @offset = offset
37
37
  @default = default
38
-
38
+
39
39
  self.reset
40
40
  end
41
-
42
- # Resets the state of the lazy seq. It also calculates any values
41
+
42
+ # Resets the state of the lazy seq. It also calculates any values
43
43
  # necessary to get to the offset.
44
44
  def reset
45
45
  @index = @list.size
46
46
  @cycles = 0
47
-
47
+
48
48
  until @list.size >= @offset
49
49
  @list[@index] = @block.call(@list[0..@index-1])
50
50
  @index += 1
51
51
  end
52
-
52
+
53
53
  @index = @offset
54
54
  end
55
-
55
+
56
56
  # @return [Object]
57
57
  # Until ended it returns the next item from +list+ if it exists or calculates
58
58
  # it then stores it in +list+, if ended it returns the default value.
@@ -68,6 +68,6 @@ class Seq
68
68
  end
69
69
  end
70
70
  end
71
-
71
+
72
72
  end
73
73
  end
@@ -0,0 +1,81 @@
1
+ class Seq
2
+
3
+ # Paged seqs evaluate a block that returns a page (Array) of items that are
4
+ # then returned one at a time. This is useful for working with web services
5
+ # that return pages of results, when you need them as a list.
6
+ #
7
+ # @example
8
+ #
9
+ # s = Seq::Paged.new {|page| [page, page+1, page+2] }
10
+ # s.take(10) #=> [0, 1, 2, 1, 2, 3, 2, 3, 4, 3]
11
+ # # extra spacing added to show pages
12
+ #
13
+ #
14
+ # require 'flickraw'
15
+ # # Authenticate FlickRaw...
16
+ #
17
+ # f = Seq::Paged.new {|page|
18
+ # flickr.people.getPhotos(:user_id => 'me', :page => page).to_a
19
+ # }
20
+ # f.next #=> {"id"=>"8688497043", "owner"=>"75695140@N04", ...}
21
+ # f.take(100).map {|photo| photo['title'] }.grep(/DSC/)
22
+ # #=> ["DSC01485-edit", "DSC01485", "DSC01482-edit", "DSC01481", ...]
23
+ #
24
+ class Paged < Seq
25
+
26
+ # Creates a new Paged seq instance.
27
+ #
28
+ # @param offset [Integer] Index of item to start at
29
+ # @param default [Object] Value to return when finished
30
+ #
31
+ # @yield [page] Block to be called which returns the next page of items
32
+ # @yieldparam page [Integer] Page to be returned, begins at 0
33
+ def initialize(offset=0, default=nil, &block)
34
+ @block = block
35
+ @offset = offset
36
+ @default = default
37
+
38
+ self.reset
39
+ end
40
+
41
+ # Resets the state of the paged seq. It also calculates any values necessary
42
+ # to get to the offset.
43
+ def reset
44
+ @index = 0
45
+ @page = 0
46
+ @done = false
47
+ @items = []
48
+
49
+ until @index >= @offset
50
+ self.next
51
+ end
52
+ end
53
+
54
+ # @return Whether the Paged seq has returned all of its items.
55
+ def ended?
56
+ @index >= @items.size && @done
57
+ end
58
+
59
+ # @return [Object] Until ended it return the next item in the paged list. If
60
+ # ended it returns the default value.
61
+ def next
62
+ return @default if ended?
63
+
64
+ while @items.size <= @index
65
+ loaded = @block.call(@page)
66
+ @page += 1
67
+ if loaded.empty?
68
+ @done = true
69
+ return @default
70
+ end
71
+ @items += loaded
72
+ end
73
+
74
+ item = @items[@index]
75
+ @index += 1
76
+ item
77
+ end
78
+
79
+
80
+ end
81
+ end
@@ -1,4 +1,3 @@
1
1
  class Seq
2
- # Current version
3
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
4
3
  end
@@ -6,7 +6,7 @@ class LazySeqTest < MiniTest::Unit::TestCase
6
6
  def setup
7
7
  @seq = Seq::Lazy.new([1, 1]) {|l| l[-1] + l[-2] }
8
8
  end
9
-
9
+
10
10
  def test_defines_next
11
11
  assert_equal 1, @seq.next
12
12
  assert_equal 1, @seq.next
@@ -16,10 +16,10 @@ class LazySeqTest < MiniTest::Unit::TestCase
16
16
  assert_equal 8, @seq.next
17
17
  assert_equal 13, @seq.next
18
18
  end
19
-
19
+
20
20
  def test_offset_works
21
21
  s = Seq::Lazy.new([1], 1.0/0, 5) {|l| l.last + 1 }
22
22
  assert_equal 6, s.next
23
23
  end
24
24
 
25
- end
25
+ end
@@ -0,0 +1,46 @@
1
+ $: << File.dirname(__FILE__) + '/..'
2
+ require 'helper'
3
+
4
+ class PagedSeqTest < MiniTest::Unit::TestCase
5
+
6
+ PAGE_COUNT = 10
7
+ PAGE_SIZE = 10
8
+
9
+ def setup
10
+ @block = lambda do |page|
11
+ return [] if page == PAGE_COUNT
12
+ (page*PAGE_SIZE...page.succ*PAGE_SIZE).to_a
13
+ end
14
+ end
15
+
16
+ def test_defines_next
17
+ s = Seq::Paged.new(&@block)
18
+
19
+ 0.upto(99) do |n|
20
+ assert_equal n, s.next
21
+ end
22
+
23
+ assert_equal nil, s.next
24
+ end
25
+
26
+ def test_defines_default
27
+ s = Seq::Paged.new(0, "default", &@block)
28
+
29
+ 0.upto(99) do |n|
30
+ assert_equal n, s.next
31
+ end
32
+
33
+ assert_equal "default", s.next
34
+ end
35
+
36
+ def test_offset_works
37
+ s = Seq::Paged.new(5, &@block)
38
+
39
+ 5.upto(99) do |n|
40
+ assert_equal n, s.next
41
+ end
42
+
43
+ assert_equal nil, s.next
44
+ end
45
+
46
+ end
@@ -12,21 +12,21 @@ class SeqTest < MiniTest::Unit::TestCase
12
12
  assert_equal [], @empty.to_a
13
13
  assert_equal [1, 2, 3, 4], @seq.to_a
14
14
  end
15
-
15
+
16
16
  def test_can_become_expanded_array
17
- assert_raises RangeError do
17
+ assert_raises RangeError do
18
18
  @empty.entries
19
19
  end
20
-
20
+
21
21
  assert_equal [2, 3, 4, 1, 2, 3, 4], @seq.entries
22
22
  end
23
-
23
+
24
24
  def test_can_iterate
25
25
  @empty.each_with_index do |a,i|
26
26
  assert_equal a, nil
27
27
  break if i > 3
28
28
  end
29
-
29
+
30
30
  @seq.each_with_index do |a,i|
31
31
  assert_equal a, case i
32
32
  when 0 then 2
@@ -39,11 +39,11 @@ class SeqTest < MiniTest::Unit::TestCase
39
39
  end
40
40
  end
41
41
  end
42
-
43
- def test_gets_next_item
42
+
43
+ def test_gets_next_item
44
44
  assert_equal nil, @empty.next
45
45
  assert_equal nil, @empty.next
46
-
46
+
47
47
  assert_equal 2, @seq.next
48
48
  assert_equal 3, @seq.next
49
49
  assert_equal 4, @seq.next
@@ -62,4 +62,4 @@ class SeqTest < MiniTest::Unit::TestCase
62
62
  assert_equal 2, @seq.next
63
63
  end
64
64
 
65
- end
65
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-22 00:00:00.000000000 +01:00
13
- default_executable:
12
+ date: 2013-05-01 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: minitest
17
- requirement: &2156880320 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ~>
@@ -22,7 +21,12 @@ dependencies:
22
21
  version: 2.3.1
23
22
  type: :development
24
23
  prerelease: false
25
- version_requirements: *2156880320
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.3.1
26
30
  description: ! " A Seq is created with an array, and optionally a number of elements\n
27
31
  \ to return, an offset to start at and a default item to return when\n ended.
28
32
  Call #next to return the next item.\n \n A Seq::Random will return randomly
@@ -37,14 +41,15 @@ files:
37
41
  - Rakefile
38
42
  - LICENCE
39
43
  - lib/seq/lazy.rb
44
+ - lib/seq/paged.rb
40
45
  - lib/seq/random.rb
41
46
  - lib/seq/version.rb
42
47
  - lib/seq.rb
43
48
  - test/helper.rb
44
49
  - test/seq/lazy_test.rb
50
+ - test/seq/paged_test.rb
45
51
  - test/seq/random_test.rb
46
52
  - test/seq_test.rb
47
- has_rdoc: true
48
53
  homepage: http://github.com/hawx/seq
49
54
  licenses: []
50
55
  post_install_message:
@@ -65,12 +70,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
70
  version: '0'
66
71
  requirements: []
67
72
  rubyforge_project:
68
- rubygems_version: 1.6.2
73
+ rubygems_version: 1.8.23
69
74
  signing_key:
70
75
  specification_version: 3
71
76
  summary: Seqs cycle over elements of an array.
72
77
  test_files:
73
78
  - test/helper.rb
74
79
  - test/seq/lazy_test.rb
80
+ - test/seq/paged_test.rb
75
81
  - test/seq/random_test.rb
76
82
  - test/seq_test.rb
83
+ has_rdoc: