jrf 0.1.8 → 0.1.9

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0a1ab5bb43a602346df298f2827aaa9de7d418fcc7f090517ccb5c47f66081c6
4
- data.tar.gz: 79181c2bb2689146e5b48d13bb1cdf3c740e86f61067862112f3fb2e81821b28
3
+ metadata.gz: e826b340d1d48d7e8cdd3de9b619e5b019f1b568794d0d306b4001f93b5478b8
4
+ data.tar.gz: 061102212643ccfd8654c31e3c8b7e48b6f1bc0131dcac77badb0295a7074d84
5
5
  SHA512:
6
- metadata.gz: 0b57e5b2f895420eb7c6a3a59d40a3f3213768eba4bef8125f3e8c6b77a4d6fa818c803a174ea37fa6dc12bca594f52949a9ba623205e089acb5cc1993d9d7ac
7
- data.tar.gz: 6c2a48e7db8074eac45a24bdcdcc41f304c2997e03e8ba43f1a28bf152195ef2ce3515ca9a3609cd17cd840849e91ef3eb4b5b9115282ae2c7187f0186a9475e
6
+ metadata.gz: '09883c9cd3ba7f52190df336553cfd45156a90f400a4b87099a86c7e753dab2e07dbe73cfea1c45fb37ca88cb06ebee7f15e286988c0ec42fe526599d897b355'
7
+ data.tar.gz: 5d8d0cc2d749c647a3587d2cf8659876a55e8fe8a41e38a9183ad3551b6cca2f16f99ba7c806aebbe0f82afcd5b2c4e026f81fb781f338a6898d4d807f15c347
@@ -136,25 +136,31 @@ module Jrf
136
136
  end
137
137
 
138
138
  define_reducer(:percentile) do |ctx, value, percentage, block: nil|
139
- scalar = !percentage.is_a?(Enumerable)
140
- percentages = scalar ? [percentage] : percentage.to_a
141
- percentages.each { |p| ctx.send(:validate_percentile!, p) }
142
-
143
- finish =
144
- if scalar
145
- ->(values) { [ctx.send(:percentile_value, values.sort, percentages.first)] }
146
- else
147
- ->(values) {
148
- sorted = values.sort
149
- [percentages.map { |p| ctx.send(:percentile_value, sorted, p) }]
150
- }
151
- end
152
-
153
139
  {
154
140
  value: value,
155
- initial: -> { [] },
156
- finish: finish,
157
- step: ->(acc, v) { v.nil? ? acc : (acc << v) }
141
+ initial: {config: -> {
142
+ scalar = !percentage.is_a?(Enumerable)
143
+ percentages = scalar ? [percentage] : percentage.to_a
144
+ percentages.each { |p| ctx.send(:validate_percentile!, p) }
145
+ [scalar, percentages]
146
+ }, values: []},
147
+ finish: ->(state) {
148
+ scalar, percentages = state.fetch(:config)
149
+ sorted = state.fetch(:values).sort
150
+ if scalar
151
+ [ctx.send(:percentile_value, sorted, percentages.first)]
152
+ else
153
+ [percentages.map { |p| ctx.send(:percentile_value, sorted, p) }]
154
+ end
155
+ },
156
+ step: ->(state, v) {
157
+ config = state.fetch(:config)
158
+ state[:config] = config.call if config.respond_to?(:call)
159
+ return state if v.nil?
160
+
161
+ state.fetch(:values) << v
162
+ state
163
+ }
158
164
  }
159
165
  end
160
166
 
data/lib/jrf/stage.rb CHANGED
@@ -51,8 +51,9 @@ module Jrf
51
51
  (@mode == :reducer) ? Control::DROPPED : result
52
52
  end
53
53
 
54
- def step_reduce(value, initial:, finish: nil, &step_fn)
54
+ def step_reduce(value, initial:, finish: nil, step_fn: nil, &step_block)
55
55
  idx = @cursor
56
+ step_fn ||= step_block
56
57
 
57
58
  if @reducers[idx].nil?
58
59
  finish_rows = finish || ->(acc) { [acc] }
data/lib/jrf/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Jrf
4
- VERSION = "0.1.8"
4
+ VERSION = "0.1.9"
5
5
  end
data/test/jrf_test.rb CHANGED
@@ -950,6 +950,30 @@ assert_equal([[5, 7]], j.call([{"a" => 1, "b" => 2}, {"a" => 2, "b" => 3}]), "li
950
950
  j = Jrf.new(proc { group_by(_["k"]) { count() } })
951
951
  assert_equal([{"x" => 2, "y" => 1}], j.call([{"k" => "x"}, {"k" => "x"}, {"k" => "y"}]), "library group_by")
952
952
 
953
+ # reducer configuration is fixed by the first row
954
+ j = Jrf.new(proc { percentile(_["a"], _["p"]) })
955
+ assert_equal([2], j.call([{"a" => 1, "p" => 0.5}, {"a" => 2, "p" => [0.5, 1.0]}, {"a" => 3, "p" => [0.5, 1.0]}]), "library percentile configuration fixed by first row")
956
+
957
+ counting_percentiles = Class.new do
958
+ include Enumerable
959
+
960
+ attr_reader :each_calls
961
+
962
+ def initialize(values)
963
+ @values = values
964
+ @each_calls = 0
965
+ end
966
+
967
+ def each(&block)
968
+ @each_calls += 1
969
+ @values.each(&block)
970
+ end
971
+ end.new([0.25, 0.5, 1.0])
972
+
973
+ j = Jrf.new(proc { percentile(_["a"], counting_percentiles) })
974
+ assert_equal([[1, 2, 3]], j.call([{"a" => 1}, {"a" => 2}, {"a" => 3}]), "library percentile enumerable values")
975
+ assert_equal(1, counting_percentiles.each_calls, "library percentile materializes enumerable once")
976
+
953
977
  # reducer then passthrough
954
978
  j = Jrf.new(
955
979
  proc { sum(_["a"]) },
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jrf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - kazuho