darkhelmet-darkext 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/Manifest.txt +18 -0
- data/PostInstall.txt +7 -0
- data/README.rdoc +49 -0
- data/Rakefile +28 -0
- data/lib/darkext/array.rb +46 -0
- data/lib/darkext/boolean.rb +11 -0
- data/lib/darkext/hash.rb +11 -0
- data/lib/darkext/numeric.rb +13 -0
- data/lib/darkext/statistics.rb +102 -0
- data/lib/darkext/string.rb +19 -0
- data/lib/darkext/symbol.rb +5 -0
- data/lib/darkext.rb +4 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/test/test_darkext.rb +11 -0
- data/test/test_helper.rb +3 -0
- metadata +93 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
PostInstall.txt
|
4
|
+
README.rdoc
|
5
|
+
Rakefile
|
6
|
+
lib/darkext.rb
|
7
|
+
script/console
|
8
|
+
script/destroy
|
9
|
+
script/generate
|
10
|
+
test/test_darkext.rb
|
11
|
+
test/test_helper.rb
|
12
|
+
lib/darkext/array.rb
|
13
|
+
lib/darkext/boolean.rb
|
14
|
+
lib/darkext/hash.rb
|
15
|
+
lib/darkext/numeric.rb
|
16
|
+
lib/darkext/statistics.rb
|
17
|
+
lib/darkext/string.rb
|
18
|
+
lib/darkext/symbol.rb
|
data/PostInstall.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
= darkext
|
2
|
+
|
3
|
+
* http://github.com/darkhelmet/darkext/
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Just some useful Ruby functionality. No particular focus, except usefulness
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* FIX (list of features or problems)
|
12
|
+
|
13
|
+
== SYNOPSIS:
|
14
|
+
|
15
|
+
FIX (code sample of usage)
|
16
|
+
|
17
|
+
== REQUIREMENTS:
|
18
|
+
|
19
|
+
* FIX (list of requirements)
|
20
|
+
|
21
|
+
== INSTALL:
|
22
|
+
|
23
|
+
* $ gem sources -a http://gems.github.com # unless you already have done this
|
24
|
+
* $ gem install darkhelmet-darkext
|
25
|
+
|
26
|
+
== LICENSE:
|
27
|
+
|
28
|
+
(The MIT License)
|
29
|
+
|
30
|
+
Copyright (c) 2008 FIXME full name
|
31
|
+
|
32
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
33
|
+
a copy of this software and associated documentation files (the
|
34
|
+
'Software'), to deal in the Software without restriction, including
|
35
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
36
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
37
|
+
permit persons to whom the Software is furnished to do so, subject to
|
38
|
+
the following conditions:
|
39
|
+
|
40
|
+
The above copyright notice and this permission notice shall be
|
41
|
+
included in all copies or substantial portions of the Software.
|
42
|
+
|
43
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
44
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
45
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
46
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
47
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
48
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
49
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
%w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
|
2
|
+
require 'version'
|
3
|
+
|
4
|
+
# Generate all the Rake tasks
|
5
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
6
|
+
$hoe = Hoe.new('darkext', Darkext::VERSION) do |p|
|
7
|
+
p.developer('Daniel Huckstep', 'darkhelmet@darkhelmetlive.com')
|
8
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
9
|
+
p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
|
10
|
+
p.rubyforge_name = p.name # TODO this is default value
|
11
|
+
# p.extra_deps = [
|
12
|
+
# ['activesupport','>= 2.0.2'],
|
13
|
+
# ]
|
14
|
+
p.extra_dev_deps = [
|
15
|
+
['newgem', ">= #{::Newgem::VERSION}"]
|
16
|
+
]
|
17
|
+
|
18
|
+
p.clean_globs |= %w[**/.DS_Store tmp *.log]
|
19
|
+
path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
|
20
|
+
p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
|
21
|
+
p.rsync_args = '-av --delete --ignore-errors'
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'newgem/tasks' # load /tasks/*.rake
|
25
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
26
|
+
|
27
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
28
|
+
# task :default => [:spec, :features]
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'darkext/numeric'
|
2
|
+
require 'darkext/symbol'
|
3
|
+
|
4
|
+
class Array
|
5
|
+
# Rotates the array left by n elements
|
6
|
+
def rotate(n = 1)
|
7
|
+
return if self.size.zero?
|
8
|
+
n.times { self.push(self.shift) }
|
9
|
+
end
|
10
|
+
|
11
|
+
# Rotates the array right by n elements
|
12
|
+
def rotate_reverse(n = 1)
|
13
|
+
return if self.size.zero?
|
14
|
+
n.times { self.unshift(self.pop) }
|
15
|
+
end
|
16
|
+
|
17
|
+
# Sums the array
|
18
|
+
def sum
|
19
|
+
self.inject(:+)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Finds the product of the array
|
23
|
+
def product
|
24
|
+
self.inject(:*)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Collects the squares of each value in the array
|
28
|
+
def squares
|
29
|
+
self.map(&:square)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Finds the sum of squares of the array
|
33
|
+
def sum_of_squares
|
34
|
+
self.squares.sum
|
35
|
+
end
|
36
|
+
|
37
|
+
# Picks a random value from the array
|
38
|
+
def random
|
39
|
+
self[rand(self.size)]
|
40
|
+
end
|
41
|
+
|
42
|
+
# Randomizes the array
|
43
|
+
def randomize
|
44
|
+
self.sort_by { rand }
|
45
|
+
end
|
46
|
+
end
|
data/lib/darkext/hash.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
class Hash
|
2
|
+
# Merges defaults with the hash
|
3
|
+
def with_defaults(defaults)
|
4
|
+
self.merge(defaults) { |key,old,new| old.nil? ? new : old }
|
5
|
+
end
|
6
|
+
|
7
|
+
# Merges defaults with the hash (destructive)
|
8
|
+
def with_defaults!(defaults)
|
9
|
+
self.merge!(defaults) { |key,old,new| old.nil? ? new : old }
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'mathn'
|
2
|
+
require 'darkext/numeric'
|
3
|
+
require 'darkext/array'
|
4
|
+
|
5
|
+
class Array
|
6
|
+
# Finds the mean of the array
|
7
|
+
def mean
|
8
|
+
self.sum / self.size.to_f
|
9
|
+
end
|
10
|
+
|
11
|
+
# Finds the median of the array
|
12
|
+
def median
|
13
|
+
return nil if self.size.zero?
|
14
|
+
case self.size % 2
|
15
|
+
when 0
|
16
|
+
return self.sort[self.size / (2 - 1), 2].mean
|
17
|
+
when 1
|
18
|
+
return self.sort[self.size / 2]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Generates a histogram hash for the array
|
23
|
+
def histogram(bins = nil)
|
24
|
+
self.sort.inject({}) do |a,x|
|
25
|
+
a[x] = a[x].to_i + 1
|
26
|
+
a
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Finds the mode of the array
|
31
|
+
def mode
|
32
|
+
map = self.histogram
|
33
|
+
max = map.values.max
|
34
|
+
map.keys.select{ |x| map[x] == max}
|
35
|
+
end
|
36
|
+
|
37
|
+
# Finds the variance of the array
|
38
|
+
def variance
|
39
|
+
(self.sum_of_squares.to_f / self.size.to_f) - self.mean.square
|
40
|
+
end
|
41
|
+
|
42
|
+
# Finds the standard deviation of the array
|
43
|
+
def deviation
|
44
|
+
self.variance.sqrt
|
45
|
+
end
|
46
|
+
|
47
|
+
# Randomly samples n elements
|
48
|
+
def sample(n = 1)
|
49
|
+
(1..n).collect { self[rand(self.size)] }
|
50
|
+
end
|
51
|
+
|
52
|
+
# Makes a two sided confidence interval for the array
|
53
|
+
# Percent must be 0 < percent < 1 for this to work properly
|
54
|
+
def ci(percent = 0.95, rho = 1)
|
55
|
+
m = self.mean
|
56
|
+
i = ((Statistics.zscore((1 - percent) / 2) * rho) /
|
57
|
+
self.size.sqrt).abs
|
58
|
+
[m - i, m + i]
|
59
|
+
end
|
60
|
+
|
61
|
+
# Standardizes the array
|
62
|
+
def standardize
|
63
|
+
m = self.mean.to_f
|
64
|
+
rho = self.deviation.to_f
|
65
|
+
self.map do |v|
|
66
|
+
(v.to_f - m) / rho
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
module Statistics
|
72
|
+
# Finds the probability of a z-score
|
73
|
+
def self.prob(z)
|
74
|
+
p = Math.erf(z.abs/2.sqrt) / 2
|
75
|
+
return 0.5 + p if 0 < z
|
76
|
+
return 0.5 - p
|
77
|
+
end
|
78
|
+
|
79
|
+
# Finds the zscore of a probability
|
80
|
+
def self.zscore(p, epsilon = 0.00000000000001)
|
81
|
+
return -1 if (1 < p || 0 > p)
|
82
|
+
minz, maxz = -6, 6
|
83
|
+
zscore = 0.5
|
84
|
+
prob = 0
|
85
|
+
while (maxz - minz) > epsilon
|
86
|
+
prob = prob(zscore)
|
87
|
+
if prob > p then maxz = zscore else minz = zscore end
|
88
|
+
zscore = (maxz + minz) * 0.5
|
89
|
+
end
|
90
|
+
return zscore
|
91
|
+
end
|
92
|
+
|
93
|
+
# Finds a two tail p-val for a high/low array
|
94
|
+
def self.p_val(r, n = 30, rho = 1, mu = r.mean)
|
95
|
+
probs = r.map do |x|
|
96
|
+
(x - mu) / (rho / n.sqrt)
|
97
|
+
end.map do |x|
|
98
|
+
Statistics.prob(x)
|
99
|
+
end
|
100
|
+
return 1 - (probs[1] - probs[0])
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class String
|
2
|
+
# Parses a string like "1..10" to a Range
|
3
|
+
def to_range
|
4
|
+
case self.count('.')
|
5
|
+
when 2
|
6
|
+
elements = self.split('..')
|
7
|
+
return Range.new(elements[0].to_i, elements[1].to_i)
|
8
|
+
when 3
|
9
|
+
elements = self.split('...')
|
10
|
+
return Range.new(elements[0].to_i, elements[1].to_i-1)
|
11
|
+
end
|
12
|
+
return nil
|
13
|
+
end
|
14
|
+
|
15
|
+
# Executes the string with system
|
16
|
+
def exec
|
17
|
+
system(self)
|
18
|
+
end
|
19
|
+
end
|
data/lib/darkext.rb
ADDED
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/darkext.rb'}"
|
9
|
+
puts "Loading darkext gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: darkhelmet-darkext
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Huckstep
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-11-23 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: newgem
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.1.0
|
23
|
+
version:
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: hoe
|
26
|
+
version_requirement:
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: 1.8.0
|
32
|
+
version:
|
33
|
+
description: Just some useful Ruby functionality. No particular focus, except usefulness
|
34
|
+
email:
|
35
|
+
- darkhelmet@darkhelmetlive.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- History.txt
|
42
|
+
- Manifest.txt
|
43
|
+
- PostInstall.txt
|
44
|
+
- README.rdoc
|
45
|
+
files:
|
46
|
+
- History.txt
|
47
|
+
- Manifest.txt
|
48
|
+
- PostInstall.txt
|
49
|
+
- README.rdoc
|
50
|
+
- Rakefile
|
51
|
+
- lib/darkext.rb
|
52
|
+
- script/console
|
53
|
+
- script/destroy
|
54
|
+
- script/generate
|
55
|
+
- test/test_darkext.rb
|
56
|
+
- test/test_helper.rb
|
57
|
+
- lib/darkext/array.rb
|
58
|
+
- lib/darkext/boolean.rb
|
59
|
+
- lib/darkext/hash.rb
|
60
|
+
- lib/darkext/numeric.rb
|
61
|
+
- lib/darkext/statistics.rb
|
62
|
+
- lib/darkext/string.rb
|
63
|
+
- lib/darkext/symbol.rb
|
64
|
+
has_rdoc: true
|
65
|
+
homepage: http://github.com/darkhelmet/darkext/
|
66
|
+
post_install_message: PostInstall.txt
|
67
|
+
rdoc_options:
|
68
|
+
- --main
|
69
|
+
- README.rdoc
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "0"
|
77
|
+
version:
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: "0"
|
83
|
+
version:
|
84
|
+
requirements: []
|
85
|
+
|
86
|
+
rubyforge_project: darkext
|
87
|
+
rubygems_version: 1.2.0
|
88
|
+
signing_key:
|
89
|
+
specification_version: 2
|
90
|
+
summary: Just some useful Ruby functionality
|
91
|
+
test_files:
|
92
|
+
- test/test_helper.rb
|
93
|
+
- test/test_darkext.rb
|