epitools 0.1.6 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.1.8
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{epitools}
8
- s.version = "0.1.6"
8
+ s.version = "0.1.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["epitron"]
12
- s.date = %q{2010-06-17}
12
+ s.date = %q{2010-07-19}
13
13
  s.description = %q{Miscellaneous utility libraries to make my life easier.}
14
14
  s.email = %q{chris@ill-logic.com}
15
15
  s.extra_rdoc_files = [
@@ -8,6 +8,7 @@ __DIR__ = File.dirname(__FILE__)
8
8
  permutations
9
9
  ratio
10
10
  zopen
11
+ powerset
11
12
  ].each do |mod|
12
13
  require File.join(__DIR__, "epitools", mod)
13
14
  end
@@ -1,7 +1,7 @@
1
1
  require 'pp'
2
2
 
3
- # Such a long cumbersome name!
4
- Enum = Enumerable::Enumerator
3
+ # Alias "Enumerable::Enumerator" to "Enum"
4
+ Object.const_set(:Enum, Enumerable::Enumerator) rescue nil
5
5
 
6
6
  class Object
7
7
  # Default "integer?" behaviour.
@@ -257,6 +257,18 @@ module Enumerable
257
257
  result
258
258
  end
259
259
 
260
+ #
261
+ # Returns the powerset of the Enumerable
262
+ #
263
+ # Example:
264
+ # [1,2].powerset #=> [[], [1], [2], [1, 2]]
265
+ #
266
+ def powerset
267
+ # the bit pattern of the numbers from 0..2^(elements)-1 can be used to select the elements of the set...
268
+ (0...2**size).map do |bitmask|
269
+ select.with_index{ |e, i| bitmask[i] == 1 }
270
+ end
271
+ end
260
272
 
261
273
  end
262
274
 
@@ -145,6 +145,10 @@ describe Enumerable do
145
145
  %w[hi there].foldl(:+).should == "hithere"
146
146
  end
147
147
 
148
+ it "powersets" do
149
+ [1,2,3].powerset.should == [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
150
+ end
151
+
148
152
  end
149
153
 
150
154
  describe Hash do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epitools
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 6
10
- version: 0.1.6
9
+ - 8
10
+ version: 0.1.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - epitron
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-17 00:00:00 -04:00
18
+ date: 2010-07-19 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency