kyanite 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. data/Div +6 -32
  2. data/{History.txt → History.rdoc} +8 -0
  3. data/{License.txt → License.rdoc} +0 -0
  4. data/Manifest.txt +6 -21
  5. data/README.rdoc +44 -0
  6. data/Rakefile.rb +19 -68
  7. data/console.rb +2 -4
  8. data/lib/kyanite.rb +151 -14
  9. data/lib/kyanite/array.rb +127 -4
  10. data/lib/kyanite/basics.rb +3 -7
  11. data/lib/kyanite/dictionary.rb +19 -21
  12. data/lib/kyanite/enumerable/enumerable_enumerables.rb +22 -24
  13. data/lib/kyanite/enumerable/enumerable_numerics.rb +51 -49
  14. data/lib/kyanite/enumerable/enumerable_strings.rb +18 -25
  15. data/lib/kyanite/enumerable/structure.rb +46 -58
  16. data/lib/kyanite/general/callerutils.rb +30 -28
  17. data/lib/kyanite/general/classutils.rb +73 -63
  18. data/lib/kyanite/general/kernel.rb +45 -64
  19. data/lib/kyanite/general/object.rb +22 -46
  20. data/lib/kyanite/general/true_false.rb +33 -27
  21. data/lib/kyanite/general/undoable.rb +12 -16
  22. data/lib/kyanite/hash.rb +63 -58
  23. data/lib/kyanite/numeric.rb +14 -1
  24. data/lib/kyanite/numeric/float.rb +32 -22
  25. data/lib/kyanite/numeric/integer.rb +12 -20
  26. data/lib/kyanite/optimizer.rb +41 -24
  27. data/lib/kyanite/range.rb +42 -22
  28. data/lib/kyanite/set.rb +56 -63
  29. data/lib/kyanite/string.rb +1 -2
  30. data/lib/kyanite/string/cast.rb +61 -37
  31. data/lib/kyanite/string/chars.rb +66 -36
  32. data/lib/kyanite/string/chars_const.rb +4 -3
  33. data/lib/kyanite/string/diff.rb +64 -34
  34. data/lib/kyanite/string/include.rb +16 -9
  35. data/lib/kyanite/string/list.rb +38 -8
  36. data/lib/kyanite/string/misc.rb +64 -0
  37. data/lib/kyanite/string/nested.rb +39 -22
  38. data/lib/kyanite/string/random.rb +117 -47
  39. data/lib/kyanite/string/split.rb +84 -45
  40. data/lib/kyanite/symbol.rb +30 -21
  41. data/lib/kyanite/tree.rb +33 -28
  42. data/test/_start_all.rb +18 -15
  43. data/test/array/test_array.rb +5 -7
  44. data/test/enumerable/test_enumerable_enumerables.rb +11 -15
  45. data/test/enumerable/test_enumerable_numerics.rb +50 -54
  46. data/test/enumerable/test_enumerable_strings.rb +8 -11
  47. data/test/enumerable/test_structure.rb +6 -8
  48. data/test/general/test_classutils.rb +9 -9
  49. data/test/general/test_object.rb +4 -5
  50. data/test/general/test_true_false.rb +4 -5
  51. data/test/numeric/test_numeric_integer.rb +6 -6
  52. data/test/string/test_cast.rb +4 -5
  53. data/test/string/test_chars.rb +6 -7
  54. data/test/string/test_diff.rb +4 -5
  55. data/test/string/test_list.rb +4 -5
  56. data/test/string/test_misc.rb +58 -0
  57. data/test/string/test_nested.rb +4 -5
  58. data/test/string/test_split.rb +10 -5
  59. data/test/test_dictionary.rb +4 -6
  60. data/test/test_hash.rb +5 -7
  61. data/test/test_optimizer.rb +4 -5
  62. data/test/test_range.rb +8 -9
  63. data/test/test_set.rb +7 -7
  64. data/test/test_tree.rb +7 -10
  65. data/version.rb +8 -0
  66. metadata +32 -33
  67. data/README.txt +0 -47
  68. data/lib/kyanite/array/array.rb +0 -140
  69. data/lib/kyanite/array/array2.rb +0 -142
  70. data/lib/kyanite/array/matrix2.rb +0 -120
  71. data/lib/kyanite/hoe.rb +0 -2
  72. data/lib/kyanite/matrix2.rb +0 -2
  73. data/lib/kyanite/numeric/numeric.rb +0 -54
  74. data/lib/kyanite/operation.rb +0 -5
  75. data/lib/kyanite/operation/call_tracker.rb +0 -69
  76. data/lib/kyanite/operation/hoe.rb +0 -12
  77. data/lib/kyanite/operation/rake.rb +0 -298
  78. data/lib/kyanite/operation/regexp.rb +0 -28
  79. data/lib/kyanite/operation/unit_test.rb +0 -50
  80. data/lib/kyanite/rake.rb +0 -2
  81. data/lib/kyanite/smart_load_path.rb +0 -6
  82. data/lib/kyanite/string/div.rb +0 -28
  83. data/lib/kyanite/string/mgsub.rb +0 -43
  84. data/lib/kyanite/unit_test.rb +0 -2
  85. data/test/array/test_matrix2.rb +0 -166
  86. data/test/string/test_mgsub.rb +0 -58
data/lib/kyanite/array.rb CHANGED
@@ -1,6 +1,129 @@
1
1
  # ruby encoding: utf-8
2
+ #
3
+ if $0 == __FILE__
4
+ require 'drumherum'
5
+ smart_init
6
+ end
2
7
 
3
- require 'kyanite/array/matrix2' # das ist die oberste Ebene, array2 und array werden ebenfalls required
4
- require 'kyanite/enumerable/enumerable_enumerables'
5
- require 'kyanite/enumerable/enumerable_numerics'
6
- require 'kyanite/enumerable/enumerable_strings'
8
+ require 'facets/array/delete' # Methoden delete_unless, delete_values, delete_values_at
9
+ require 'facets/array/rotate' # Methoden rotate, rotate!
10
+ require 'facets/array/select' # Methode select!
11
+ require 'facets/to_hash' # Methode to_h (in der Facets-Bibliothek falsch einsortiert?)
12
+
13
+ require 'kyanite/enumerable/enumerable_enumerables'
14
+ require 'kyanite/enumerable/enumerable_numerics'
15
+ require 'kyanite/enumerable/enumerable_strings'
16
+ require 'kyanite/enumerable' # ist eigentlich nicht nötig?
17
+ require 'kyanite/general/object' # Methode respond
18
+ require 'kyanite/general/classutils' # <=> für class
19
+ require 'kyanite/symbol' # size
20
+
21
+
22
+
23
+ # @!macro array
24
+ #
25
+ # === Required from {http://rubyworks.github.com/rubyfaux/?doc=http://rubyworks.github.com/facets/docs/facets-2.9.3/core.json#api-class-Array Facets Array}:
26
+ # [+delete_unless(&block)+] Inverse of +delete_if+
27
+ # [+delete_values(*values)+] Delete multiple values from array (findet und löscht bestimmte Werte)
28
+ # [+delete_values_at(*selectors)+] Delete multiple values from array given indexes or index range (löscht Elemente an bestimmten Positionen)
29
+ # [+rotate(n=1)+] Rotates an array's elements from back to front n times (alles rückt n Positionen auf)
30
+ # [+select!{ |obj| block }+] As with +select+ but modifies the Array in place.
31
+ # [+to_h(arrayed=nil)+] Converts a two-element associative array into a hash.
32
+ #
33
+ class Array
34
+
35
+
36
+ # Cuts the front portion, and returns the rest.
37
+ # If the remainder is only one element, it' not returned as an array but as single element.
38
+ # Useful for recursions. Example:
39
+ # [1,2,3].shift_complement => [2,3]
40
+ # [1,2,3].shift_complement.shift_complement => 3
41
+ # [1,2,3].shift_complement.shift_complement.shift_complement => nil
42
+ #
43
+ # See tests and examples {TestKyaniteArray#test_array_shift_complement here}.
44
+ #
45
+ def shift_complement
46
+ if self.size > 2
47
+ self[1..-1]
48
+ else
49
+ self[-1]
50
+ end
51
+ end
52
+
53
+
54
+
55
+
56
+
57
+ # Rephrases the index of an Array pos / neg / inv.
58
+ # [:pos] The index is rephrased as a positive integer
59
+ # [:neg] The index is rephrased as a negative integer
60
+ # [:inv] if it was positive, then it will get negative, and vice versa
61
+ #
62
+ # @return [Integer]
63
+ #
64
+ # See tests and examples {TestKyaniteArray#test_rephrase_index here}
65
+ #
66
+ def rephrase_index(i, style=:inv)
67
+ return i if i > (size-1)
68
+ return i if i < -size
69
+ case style
70
+ when :inv
71
+ if i >=0 # pos >> neg
72
+ (i - size)
73
+ else # neg >> pos
74
+ (i + size)
75
+ end
76
+ when :pos
77
+ if i >= 0 # pos bleibt
78
+ i
79
+ else # neg >> pos
80
+ (i + size)
81
+ end
82
+ when :neg
83
+ if i >= 0 # pos >> neg
84
+ (i - size)
85
+ else # neg bleibt
86
+ i
87
+ end
88
+ end #case
89
+ end #def
90
+
91
+
92
+
93
+
94
+ end # class Array
95
+
96
+
97
+
98
+
99
+ if defined? TransparentNil
100
+ class NilClass
101
+ def shift_complement; nil; end
102
+ end
103
+ end
104
+
105
+
106
+ class Numeric
107
+ # +nil+, Complements {Array#shift_complement}.
108
+ # @return [NilClass]
109
+ def shift_complement
110
+ nil
111
+ end
112
+ end
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+
@@ -6,9 +6,7 @@
6
6
  # innerhalb von Verzeichnissen
7
7
  #
8
8
 
9
- # require 'kyanite/array'
10
- require 'kyanite/array/array' # aber nicht array2 und matrix2
11
-
9
+ require 'kyanite/array'
12
10
  # require 'kyanite/general'
13
11
  # require 'kyanite/general/callerutils'
14
12
  require 'kyanite/general/classutils'
@@ -19,16 +17,14 @@
19
17
  # require 'kyanite/general/undoable'
20
18
 
21
19
  require 'kyanite/numeric'
22
- # require 'kyanite/operation' # nur manuell
23
20
 
24
21
  # require 'kyanite/string'
25
22
  # require 'kyanite/string/cast'
26
23
  # require 'kyanite/string/chars'
27
24
  # require 'kyanite/string/diff'
28
- # require 'kyanite/string/div'
29
25
  require 'kyanite/string/include'
30
26
  # require 'kyanite/string/list'
31
- require 'kyanite/string/mgsub'
27
+ require 'kyanite/string/misc'
32
28
  # require 'kyanite/string/nested'
33
29
  # require 'kyanite/string/random'
34
30
  # require 'kyanite/string/split'
@@ -48,7 +44,7 @@
48
44
  # require 'kyanite/set'
49
45
  require 'kyanite/symbol'
50
46
  # require 'kyanite/tree'
51
- # require 'kyanite/unit_test' # nur für Tests
47
+
52
48
 
53
49
  # ----------------------------------------------------------------
54
50
  # Außerhalb
@@ -1,4 +1,10 @@
1
1
  # ruby encoding: utf-8
2
+ # ü
3
+ if $0 == __FILE__
4
+ require 'drumherum'
5
+ smart_init
6
+ end
7
+
2
8
  require 'hashery' # old: 'facets/dictionary'
3
9
  unless defined? ENUMERABLE_REQUIRED
4
10
  require 'kyanite/enumerable' # is_collection?
@@ -7,16 +13,8 @@ end
7
13
 
8
14
 
9
15
 
10
- # [ | Kyanite | Object | Array | Set | Enumerable | *Hash* | ] | Hash | *Dictionary* |
11
- # ---
12
- #
13
- #
14
- # == *Tools* *For* *Facets* *Dictionary*
15
- # A Dictionary is a ordered Hash.
16
- # Expands {Facets/Dictionary}[http://facets.rubyforge.org/doc/api/more/classes/Dictionary.html].
17
- # See TestKyaniteDictionary for tests and examples.
18
- #
19
- #
16
+
17
+ # @!macro dictionary
20
18
  class Dictionary < Hashery::Dictionary
21
19
 
22
20
  def fetch_by_index(index)
@@ -55,9 +53,11 @@ class Dictionary < Hashery::Dictionary
55
53
  end
56
54
 
57
55
 
56
+
57
+
58
58
  class Hash
59
59
 
60
- # TODO: effizienter
60
+ # @return [Dictionary]
61
61
  def to_dictionary
62
62
  result = Dictionary.new
63
63
  self.each do | key, value |
@@ -71,25 +71,23 @@ end
71
71
 
72
72
  class Array
73
73
 
74
- # :section: Cast
75
-
76
-
77
-
78
- # Liefert ein Dictionary (das ist ein geordneter Hash)
79
- #
80
- # TODO: effizienter
74
+ # @!group Cast
75
+ # Returns {Dictionary}
76
+ # @return [Dictionary]
81
77
  def to_dictionary
78
+ # TODO: effizienter
82
79
  result = Dictionary.new
83
80
  self.each do | zeile |
84
81
  result << zeile
85
82
  end
86
83
  result
87
84
  end
85
+ # @!endgroup
88
86
 
89
87
  #funktioniert nicht
90
- def to_dictionary2 # :nodoc:
91
- Dictionary.new(self.flatten)
92
- end
88
+ # def to_dictionary2 # :nodoc:
89
+ # Dictionary.new(self.flatten)
90
+ # end
93
91
 
94
92
  end
95
93
 
@@ -1,8 +1,14 @@
1
1
  # ruby encoding: utf-8
2
+ # ü
3
+ if $0 == __FILE__
4
+ require 'drumherum'
5
+ smart_init
6
+ end
7
+
2
8
 
3
9
  module Enumerable
4
10
 
5
- # In-place-Variante von transpose.
11
+ # In-place-variant of +transpose+.
6
12
  def transpose!
7
13
  self.replace(self.transpose)
8
14
  end
@@ -10,25 +16,19 @@ module Enumerable
10
16
 
11
17
  end
12
18
 
13
- # [ | Kyanite | Object | Array | Set | *Enumerable* | Hash | ] | Enumerable | EnumerableNumerics | EnumerableStrings | *EnumerableEnumerables* |
14
- # ---
15
- #
16
- #
17
- # == *Enumeration* *Of* *Enumerations*
18
- # Für zweidimensionale Enumerables bzw. Aufzählungen von Objekten, die wiederum aufzählbar sind.
19
- # See TestKyaniteEnumerableEnumerables for tests and examples.
20
- # See ArrayOfEnumerables for an Array with modul EnumerableEnumerables included.
21
- #
22
- #
19
+
20
+
21
+
22
+
23
+ # @!macro enum_of_enums
23
24
  module EnumerableEnumerables
24
25
 
25
- # Macht das Enumerable rechteckig.
26
- # Maßgeblich ist die erste Zeile.
26
+ # Makes the Enumerable rectangular (= strict two-dimensional). The first row is essential.
27
27
  #
28
- # Tests and examples see TestKyaniteEnumerableEnumerables.
28
+ # See tests and examples {TestKyaniteEnumerableEnumerables#test_rectangle1 here}.
29
29
  def rectangle
30
30
  qsize = self[0].size
31
- result = []
31
+ result = ArrayOfEnumerables.new
32
32
  self.each do |zeile|
33
33
  size_diff = qsize - zeile.size
34
34
  # so lassen oder zuschneiden
@@ -46,14 +46,8 @@ end
46
46
 
47
47
 
48
48
 
49
- # [ | Kyanite | Object | *Array* | Set | Enumerable | Hash | ] | Array | ArrayOfNumerics | ArrayOfStrings | *ArrayOfEnumerables* | Range |
50
- # ---
51
- #
52
- #
53
- # == *Array* *Of* *Enumerations*
54
- # An ArrayOfEnumerables is an Array with modul EnumerableEnumerables included.
55
- # See TestKyaniteEnumerableEnumerables for tests and examples.
56
- #
49
+
50
+ # @!macro enum_of_enums
57
51
  class ArrayOfEnumerables < Array
58
52
  include EnumerableEnumerables
59
53
  end
@@ -61,10 +55,14 @@ end
61
55
 
62
56
  class Array
63
57
 
64
- # Liefert ein ArrayOfEnumerables (das ist ein Array mit inkludiertem Modul EnumerableEnumerables)
58
+ # @!group Cast
59
+ # Returns {ArrayOfEnumerables} (this is an {Array} with modul {EnumerableEnumerables} included)
60
+ # @return [ArrayOfEnumerables]
65
61
  def to_array_of_enumerables
66
62
  ArrayOfEnumerables.new(self)
67
63
  end
64
+ #@!endgroup
65
+
68
66
  end
69
67
 
70
68
 
@@ -1,24 +1,26 @@
1
1
  # ruby encoding: utf-8
2
+ # ü
3
+ if $0 == __FILE__
4
+ require 'drumherum'
5
+ smart_init
6
+ end
2
7
 
3
- # [ | Kyanite | Object | Array | Set | *Enumerable* | Hash | ] | Enumerable | *EnumerableNumerics* | EnumerableStrings | EnumerableEnumerables |
4
- # ---
5
- #
6
- #
7
- # == *Enumeration* *Of* *Numerics*
8
- # See TestKyaniteEnumerableNumerics for tests and examples.
9
- # See ArrayOfNumerics for an Array with modul EnumerableNumerics included.
10
- #
11
- #
8
+
9
+
10
+
11
+
12
+ # @!macro enum_of_numerics
12
13
  module EnumerableNumerics
13
14
 
14
15
 
15
16
  # ======================================================================================
16
- # :section: Mittelwerte
17
+ # @!group Mean Values
17
18
  #
18
19
 
19
- # Arithmetrischer Mittelwert
20
- # Tests and examples see TestKyaniteEnumerableNumerics.
21
- #
20
+ # Arithmetic mean
21
+ #
22
+ # Tests and examples {TestKyaniteEnumerableNumerics here}.
23
+ # @return [Float]
22
24
  def mean
23
25
  self.inject(0.0) { |sum, i | sum += i } / self.length.to_f
24
26
  end
@@ -26,15 +28,14 @@ module EnumerableNumerics
26
28
  alias average mean
27
29
  alias mean_arithmetric mean
28
30
 
29
- # Harmonischer Mittelwert
30
- #
31
- # Normalerweise ist der harmonische Mittelwert nur für positive Zahlen sinnvoll definiert.
32
- # Mit der Option <tt>:allow_negative => true </tt>kann man aber auch negative Zahlen mit einbeziehen.
33
- # Dann wird der harmonische Mittelwert aller positiven Elemente mit dem
34
- # harmonische Mittelwert aller negativen Elemente verrechnet (als gewichtetes arithmetisches Mittel).
31
+
32
+ # Harmonic mean
35
33
  #
36
- # Tests and examples see TestKyaniteEnumerableNumerics.
37
- #
34
+ # Usually, the harmonic mean is defined only for positive numbers.
35
+ # The option <tt>:allow_negative => true </tt> can also include negative numbers in the calculation.
36
+ # Then the result will be a weighted arithmetic mean of the harmonic mean of all positive elements
37
+ # and the harmonic mean of all negative elements.
38
+ # @return [Float]
38
39
  def mean_harmonic( options={} )
39
40
  return 0 if self.empty?
40
41
  return self.first if self.size == 1
@@ -45,8 +46,8 @@ module EnumerableNumerics
45
46
  return self.size / summe
46
47
 
47
48
  else
48
- positives = []
49
- negatives = []
49
+ positives = ArrayOfNumerics.new
50
+ negatives = ArrayOfNumerics.new
50
51
  self.each do |e|
51
52
  if e >= 0
52
53
  positives << e
@@ -70,7 +71,9 @@ module EnumerableNumerics
70
71
  end #def
71
72
 
72
73
 
73
- # Geometrischer Mittelwert
74
+
75
+ # Geometric mean
76
+ # @return [Float]
74
77
  def mean_geometric
75
78
  self.prd ** ( 1.0/self.size )
76
79
  end
@@ -78,32 +81,34 @@ module EnumerableNumerics
78
81
 
79
82
 
80
83
  # ======================================================================================
81
- # :section: Summe, Produkt, Parallelschaltung
84
+ # @!group Sum, Product, Parallel
82
85
  #
83
86
 
84
87
 
85
- # Summe
88
+ # Sum
86
89
  #
87
- # Methode darf nicht sum heißen, kollidiert sonst schnell mit ActiveRecord.
88
- # Tests and examples see TestKyaniteEnumerableNumerics.
89
- #
90
+ # Tests and examples {TestKyaniteEnumerableNumerics here}.
91
+ # @return [Numeric]
90
92
  def summation
93
+ # Methode darf nicht sum heißen, kollidiert sonst schnell mit ActiveRecord.
91
94
  self.inject(0.0) { |sum, i | sum += i }
92
95
  end
93
96
 
94
97
 
95
- # Produkt
96
- # Methode darf nicht product heißen, die gibt es nämlich schon.
97
- # Tests and examples see TestKyaniteEnumerableNumerics.
98
- #
98
+ # Product
99
+ #
100
+ # Tests and examples {TestKyaniteEnumerableNumerics here}.
101
+ # @return [Numeric]
99
102
  def prd
103
+ # Methode darf nicht product heißen, die gibt es nämlich schon.
100
104
  self.inject(1.0) { |p, i | p *= i }
101
105
  end
102
106
 
103
-
104
- # Ergebnis entspricht der Parallelschaltung von Widerständen.
105
- # Tests and examples see TestKyaniteEnumerableNumerics.
106
- #
107
+ # Parallel
108
+ #
109
+ # Result is equal to the total resistance of resistors in parallel circuits.
110
+ # Tests and examples {TestKyaniteEnumerableNumerics here}.
111
+ # @return [Float]
107
112
  def parallel
108
113
  mean_harmonic / size
109
114
  end
@@ -114,14 +119,10 @@ end #class
114
119
 
115
120
 
116
121
 
117
- # [ | Kyanite | Object | *Array* | Set | Enumerable | Hash | ] | Array | *ArrayOfNumerics* | ArrayOfStrings | ArrayOfEnumerables | Range |
118
- # ---
119
- #
120
- #
121
- # == *Array* *Of* *Numerics*
122
- # An ArrayOfNumerics is an Array with modul EnumerableNumerics included.
123
- # See TestKyaniteEnumerableNumerics for tests and examples.
124
- #
122
+
123
+
124
+
125
+ # @!macro enum_of_numerics
125
126
  class ArrayOfNumerics < Array
126
127
  include EnumerableNumerics
127
128
  end
@@ -130,10 +131,13 @@ end
130
131
 
131
132
  class Array
132
133
 
133
- # Liefert ein ArrayOfNumerics (das ist ein Array mit inkludiertem Modul EnumerableNumerics)
134
+ # @!group Cast
135
+ # Returns {ArrayOfNumerics} (this is an {Array} with modul {EnumerableNumerics} included)
136
+ # @return [ArrayOfNumerics]
134
137
  def to_array_of_numerics
135
138
  ArrayOfNumerics.new(self)
136
139
  end
140
+ #@!endgroup
137
141
 
138
142
  end
139
143
 
@@ -157,9 +161,7 @@ end
157
161
  # Ausprobieren
158
162
  #
159
163
  if $0 == __FILE__
160
- class Array
161
- include EnumerableNumerics
162
- end
164
+
163
165
 
164
166
 
165
167