as-extensions 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README.md +10 -6
  2. data/ext/array.rb +19 -18
  3. metadata +3 -3
data/README.md CHANGED
@@ -13,12 +13,16 @@ Read the RDoc if you use them.
13
13
 
14
14
  ## How To Install
15
15
 
16
- 1. Install Jeweler (http://github.com/technicalpickles/jeweler).
17
- 2. Clone the Git repository
18
- 3. Run:
19
- rake build
20
- gem install pkg/*.gem
21
- 4. Add "require 'as-extensions'" to your code.
16
+ 1) Install Jeweler (http://github.com/technicalpickles/jeweler).
17
+
18
+ 2) Clone the Git repository
19
+
20
+ 3) Run:
21
+
22
+ rake build
23
+ gem install pkg/*.gem
24
+
25
+ 4) Add `require 'as-extensions'` to your code.
22
26
 
23
27
  ## Copyright
24
28
 
data/ext/array.rb CHANGED
@@ -18,27 +18,27 @@
18
18
  #++
19
19
 
20
20
  Array.class_eval do
21
-
21
+
22
22
  # Boolean AND of elements
23
23
  def band
24
24
  inject{ |s,x| s && x }
25
25
  end
26
-
26
+
27
27
  # For compatibility purpose.
28
28
  def bmap(&blk)
29
29
  map(&blk).band
30
30
  end
31
-
31
+
32
32
  # Boolean OR of elements
33
33
  def bor
34
34
  inject{ |s,x| s || x }
35
35
  end
36
-
36
+
37
37
  # Call ASE::deepcompact(self)
38
38
  def deepcompact
39
39
  ASE::deepcompact(self)
40
40
  end
41
-
41
+
42
42
  def first=(x)
43
43
  raise IndexError, 'empty array' if empty?
44
44
  self[0] = x
@@ -62,60 +62,61 @@ Array.class_eval do
62
62
 
63
63
  alias :head :first
64
64
  alias :head= :first=
65
-
65
+
66
66
  def init
67
67
  self[0..-2]
68
68
  end
69
-
69
+
70
70
  def last=(x)
71
71
  raise IndexError, 'empty array' if empty?
72
72
  self[-1] = x
73
73
  end
74
-
74
+
75
75
  # Map a function (ie. Method object) to members
76
76
  def map_f(f, *args)
77
77
  self.map{ |x| f.call(self, *args) }
78
78
  end
79
-
79
+
80
80
  # Map a method of members to themselves
81
81
  def map_m(sym, *args)
82
82
  self.map{ |x| x.send(sym.to_sym, *args) }
83
83
  end
84
-
84
+
85
85
  # In-place version of map_m
86
86
  def map_m!(sym, *args)
87
87
  self.map!{ |x| x.send(sym.to_sym, *args) }
88
88
  end
89
-
89
+
90
90
  # Reverse map_m: members are arguments
91
91
  def map_mr(sym, obj, *args)
92
92
  self.map{ |x| obj.send(sym.to_sym, x, *args) }
93
93
  end
94
-
94
+
95
95
  # In-place version of map_mr
96
96
  def map_mr!(sym, obj, *args)
97
97
  self.map!{ |x| obj.send(sym.to_sym, x, *args) }
98
98
  end
99
-
99
+
100
100
  # Return a random element.
101
101
  def pick(secure=false)
102
- self[secure ? ActiveSupport::SecureRandom.random_number(size) : Kernel.rand(size)]
102
+ @secure_random ||= (RUBY_VERSION < "1.9") ? ActiveSupport::SecureRandom : SecureRandom
103
+ self[secure ? @secure_random.random_number(size) : Kernel.rand(size)]
103
104
  end
104
-
105
+
105
106
  def tail
106
107
  self[1..-1] || []
107
108
  end
108
-
109
+
109
110
  # See vsum for an example of what this does
110
111
  # Another example: vapply(:to_a) transposes a matrix!
111
112
  def vapply(x)
112
113
  (head||[]).zip(*tail).map_m(x)
113
114
  end
114
-
115
+
115
116
  # Vector sum
116
117
  # For instance: [[1,2],[1,3]].vsum => [2,5]
117
118
  def vsum
118
119
  vapply(:sum)
119
120
  end
120
-
121
+
121
122
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Pierre Chapuis
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-08-05 00:00:00 +02:00
17
+ date: 2011-11-09 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency