cotcube-helpers 0.1.6 → 0.1.7.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0417adfcb0f782bfa9e783dbd1c673a489fca39ce9a0f14fa965eb47aaa64b11
4
- data.tar.gz: 1b14e149cc6b1bd437d1413dfa49b461c798868959cf97b164a0b3a2e1f72889
3
+ metadata.gz: a685d90173074f0ee3490602a9108cec0f8a8ff6e616fce2db1ef3566d0f2ef6
4
+ data.tar.gz: b08e52e6e47a233658765b4fdb3aab72d649aefff91eabb46e8cbcef6542e84e
5
5
  SHA512:
6
- metadata.gz: c2babf733929f4ca0705ac2018b075e3b62df051c4918d01fcc6c11b49a7cad2ee68fd8ef26504edc5678c3057a97092de252a34cec2a953f638cd9262c2e65d
7
- data.tar.gz: 1d9e367613f5b373c73cada786dee68d4e5ccc965573373b39d6419433a5f8131bc553773279d7ba58f2706a63be474448e15cc1e7bce1da63278ad384abeee0
6
+ metadata.gz: b8ced4fffb8f219416f05fe6daf3e891499e788d58d1ca4ea233cfff18998972b3aa9f0be10abdda3081671e534d0346201cfa7f5442b0bc996b75a6f3b1208b
7
+ data.tar.gz: 853ae393f9f3ba54321d82d2dba17e61fd6f85a4609029dee619ddcab10b2c2e5aa5dd6e7105f3f88e1e072b60034f7657a4cb53baed8188ddb67347326bce7a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,20 @@
1
+ ## 0.1.7.4 (March 13, 2021)
2
+ - hotfix on 0.1.7.3
3
+
4
+ ## 0.1.7.3 (March 13, 2021)
5
+ - array_ext: pairwise and triplewise now support saving result in latter members []=
6
+
7
+ ## 0.1.7.2 (February 01, 2021)
8
+ - adding #deep_freeze to Enumerable
9
+ - range_ext: added #mod to modify an (actually) immutable range
10
+ - simple_series_stats: minor fix
11
+
12
+ ## 0.1.7.1 (January 17, 2021)
13
+ - bugfix
14
+
15
+ ## 0.1.7 (January 17, 2021)
16
+ - added new method 'simple_series_stats'
17
+
1
18
  ## 0.1.6 (January 15, 2021)
2
19
  - removing :ranges from Range#select_within
3
20
  - Added Array#select_right_by
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.1.7.4
@@ -16,6 +16,7 @@ require_relative 'cotcube-helpers/datetime_ext'
16
16
  require_relative 'cotcube-helpers/subpattern'
17
17
  require_relative 'cotcube-helpers/parallelize'
18
18
  require_relative 'cotcube-helpers/simple_output'
19
+ require_relative 'cotcube-helpers/simple_series_stats'
19
20
  require_relative 'cotcube-helpers/input'
20
21
  require_relative 'cotcube-helpers/reduce'
21
22
 
@@ -24,6 +25,7 @@ module Cotcube
24
25
  module_function :sub,
25
26
  :parallelize,
26
27
  :reduce,
28
+ :simple_series_stats,
27
29
  :keystroke
28
30
 
29
31
  # please not that module_functions of source provided in private files must be published there
@@ -29,28 +29,32 @@ class Array
29
29
  # This method iterates over an Array by calling the given block on all 2 consecutive elements
30
30
  # it returns a Array of self.size - 1
31
31
  #
32
- def pairwise(&block)
32
+ def pairwise(ret=nil, &block)
33
33
  raise ArgumentError, 'Array.one_by_one needs an arity of 2 (i.e. |a, b|)' unless block.arity == 2
34
+ raise ArgumentError, 'Each element of Array should respond to []=, at least the last one fails.' unless self.last.respond_to?(:[]=)
34
35
  return [] if size <= 1
35
36
 
36
- each_with_index.map do |_, i|
37
+ each_index.map do |i|
37
38
  next if i.zero?
38
39
 
39
- block.call(self[i - 1], self[i])
40
+ r = block.call(self[i - 1], self[i])
41
+ ret.nil? ? r : (self[i][ret] = r)
40
42
  end.compact
41
43
  end
42
44
 
43
45
  alias one_by_one pairwise
44
46
 
45
47
  # same as pairwise, but with arity of three
46
- def triplewise(&block)
48
+ def triplewise(ret=nil, &block)
47
49
  raise ArgumentError, 'Array.triplewise needs an arity of 3 (i.e. |a, b, c|)' unless block.arity == 3
50
+ raise ArgumentError, 'Each element of Array should respond to []=, at least the last one fails.' unless self.last.respond_to?(:[]=)
48
51
  return [] if size <= 2
49
52
 
50
- each_with_index.map do |_, i|
53
+ each_index.map do |i|
51
54
  next if i < 2
52
55
 
53
- block.call(self[i - 2], self[i - 1], self[i])
56
+ r = block.call(self[i - 2], self[i - 1], self[i])
57
+ ret.nil? ? r : (self[i][ret] = r)
54
58
  end.compact
55
59
  end
56
60
 
@@ -61,37 +65,37 @@ class Array
61
65
  unless attr.nil? || first[attr]
62
66
  raise ArgumentError,
63
67
  "At least first element of Array '#{first}' does not contain attr '#{attr}'!"
64
- end
65
- raise ArgumentError, 'Ranges should be an Array or, more precisely, respond_to :map' unless ranges.respond_to? :map
66
- raise ArgumentError, 'Each range in :ranges should respond to .include!' unless ranges.map do |x|
67
- x.respond_to? :include?
68
- end.reduce(:&)
69
-
70
- select do |el|
71
- value = attr.nil? ? el : el[attr]
72
- ranges.map do |range|
73
- range.include?(block.nil? ? value : block.call(value))
74
- end.reduce(:|)
75
- end
76
68
  end
69
+ raise ArgumentError, 'Ranges should be an Array or, more precisely, respond_to :map' unless ranges.respond_to? :map
70
+ raise ArgumentError, 'Each range in :ranges should respond to .include!' unless ranges.map do |x|
71
+ x.respond_to? :include?
72
+ end.reduce(:&)
73
+
74
+ select do |el|
75
+ value = attr.nil? ? el : el[attr]
76
+ ranges.map do |range|
77
+ range.include?(block.nil? ? value : block.call(value))
78
+ end.reduce(:|)
79
+ end
80
+ end
77
81
 
78
- def select_right_by(inclusive: false, exclusive: false, initial: [], &block)
79
- # unless range.is_a? Range and
80
- # (range.begin.nil? or range.begin.is_a?(Integer)) and
81
- # (range.end.nil? or range.end.is_a?(Integer))
82
- # raise ArgumentError, ":range, if given, must be a range of ( nil|Integer..nil|Integer), got '#{range}'"
83
- # end
82
+ def select_right_by(inclusive: false, exclusive: false, initial: [], &block)
83
+ # unless range.is_a? Range and
84
+ # (range.begin.nil? or range.begin.is_a?(Integer)) and
85
+ # (range.end.nil? or range.end.is_a?(Integer))
86
+ # raise ArgumentError, ":range, if given, must be a range of ( nil|Integer..nil|Integer), got '#{range}'"
87
+ # end
84
88
 
85
- raise ArgumentError, 'No block given.' unless block.is_a? Proc
89
+ raise ArgumentError, 'No block given.' unless block.is_a? Proc
86
90
 
87
- inclusive = true unless exclusive
88
- if inclusive && exclusive
89
- raise ArgumentError,
90
- "Either :inclusive or :exclusive must remain falsey, got '#{inclusive}' and '#{exclusive}'"
91
- end
91
+ inclusive = true unless exclusive
92
+ if inclusive && exclusive
93
+ raise ArgumentError,
94
+ "Either :inclusive or :exclusive must remain falsey, got '#{inclusive}' and '#{exclusive}'"
95
+ end
92
96
 
93
- index = find_index { |obj| block.call(obj) }
97
+ index = find_index { |obj| block.call(obj) }
94
98
 
95
- self[((inclusive ? index : index + 1)..)]
96
- end
99
+ self[((inclusive ? index : index + 1)..)]
100
+ end
97
101
  end
@@ -11,3 +11,25 @@ class Enumerator
11
11
  ret
12
12
  end
13
13
  end
14
+
15
+ # Recursively freeze self if it's Enumerable
16
+ # Supports all Ruby versions 1.8.* to 2.2.*+
17
+ module Kernel
18
+ alias deep_freeze freeze
19
+ alias deep_frozen? frozen?
20
+ end
21
+
22
+ # Adding deep_freeze and deep_frozen?
23
+ module Enumerable
24
+ def deep_freeze
25
+ unless @deep_frozen
26
+ each(&:deep_freeze)
27
+ @deep_frozen = true
28
+ end
29
+ freeze
30
+ end
31
+
32
+ def deep_frozen?
33
+ !!@deep_frozen
34
+ end
35
+ end
@@ -2,6 +2,10 @@
2
2
 
3
3
  # Monkey patching the Ruby Core class Range
4
4
  class Range
5
+ def mod(first, last = 0)
6
+ exclude_end? ? ((self.begin + first)...(self.end + last)) : ((self.begin + first)..(self.begin + last))
7
+ end
8
+
5
9
  def to_time_intervals(step:,
6
10
  timezone: Time.find_zone('America/Chicago'),
7
11
  # ranges: nil,
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cotcube
4
+ # Missing top level documentation
5
+ module Helpers
6
+ # if given a block, :ind of base is set by block.call()
7
+ # dim reduces the sample size by top n% and least n%, so dim of 0.5 would remove 100% of the sample
8
+ def simple_series_stats(base:, ind: nil, dim: 0, format: '% 6.2f', prefix: '', print: true, &block)
9
+ raise ArgumentError, 'Need :ind of type integer' if base.first.is_a?(Array) and not ind.is_a?(Integer)
10
+ raise ArgumentError, 'Need :ind to evaluate base' if base.first.is_a?(Hash) and ind.nil?
11
+
12
+ dim = dim.to_f if dim.is_a? Numeric
13
+ dim = 0 if dim==false
14
+
15
+ raise ArgumentError, 'Expecting 0 <= :dim < 0.5' unless dim.is_a?(Float) and dim >= 0 and dim < 0.5
16
+ raise ArgumentError, 'Expecting arity of one if block given' if block_given? and not block.arity==1
17
+
18
+ precision = format[-1] == 'f' ? format[..-2].split('.').last.to_i : 0
19
+ worker = base.
20
+ tap {|b| b.map{|x| x[ind] = block.call(x) } if block.is_a? Proc }.
21
+ map {|x| ind.nil? ? x : x[ind] }.
22
+ compact.
23
+ sort
24
+ unless dim.zero?
25
+ reductor = (base.size * dim).round
26
+ worker = worker[reductor..base.size - reductor]
27
+ end
28
+ result = {}
29
+
30
+ result[:size] = worker.size
31
+ result[:min] = worker.first
32
+ result[:avg] = (worker.reduce(:+) / result[:size]).round(precision+1)
33
+ result[:lower] = worker[ (result[:size] * 1 / 4).round ]
34
+ result[:median] = worker[ (result[:size] * 2 / 4).round ]
35
+ result[:upper] = worker[ (result[:size] * 3 / 4).round ]
36
+ result[:max] = worker.last
37
+
38
+ output = result.
39
+ reject{|k,_| k == :size}.
40
+ map{|k,v| { type: k, value: v, output: "#{k}: #{format(format, v)}".colorize(k==:avg ? :light_yellow : :white) } }.
41
+ sort_by{|x| x[:value]}.
42
+ map{|x| x[:output]}
43
+ result[:output] = "#{format '%20s',(prefix.empty? ? '' : (prefix + ': '))}" +
44
+ "[" +
45
+ " size: #{format '%6d', result[:size]} | ".light_white +
46
+ output.join(' | ') +
47
+ " ]"
48
+
49
+ puts result[:output] if print
50
+ result
51
+ end
52
+ end
53
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cotcube-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin L. Tischendorf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-15 00:00:00.000000000 Z
11
+ date: 2021-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -90,6 +90,7 @@ files:
90
90
  - lib/cotcube-helpers/range_ext.rb
91
91
  - lib/cotcube-helpers/reduce.rb
92
92
  - lib/cotcube-helpers/simple_output.rb
93
+ - lib/cotcube-helpers/simple_series_stats.rb
93
94
  - lib/cotcube-helpers/string_ext.rb
94
95
  - lib/cotcube-helpers/subpattern.rb
95
96
  - lib/cotcube-helpers/swig/date.rb