jrf 0.1.7 → 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: 7ac8b4b0fe2489c04dcba49752df7143f7e218de9f21b0496e2c3fdd2f732088
4
- data.tar.gz: 2787cc4714d0e99909c4430fe23aca1fcaae1c25a079f15b2092861b53c4f5ea
3
+ metadata.gz: e826b340d1d48d7e8cdd3de9b619e5b019f1b568794d0d306b4001f93b5478b8
4
+ data.tar.gz: 061102212643ccfd8654c31e3c8b7e48b6f1bc0131dcac77badb0295a7074d84
5
5
  SHA512:
6
- metadata.gz: 61f498f33e794258ebed00a468aa779ece52eff4c29d0538f7bc1601391d0a6948c32ed5dfbd76439e55a283ad4c59dc8312254711341dae2b7e79bf45b8a0a0
7
- data.tar.gz: 92e1c46977cf3d841c8469fcf7e757cfcb4b6c60e800b063771bed3cc88eac7622e7d9a0c4aab906cefd60d046fe77a1f4e2f932d37687c952db3a598a0f3b1c
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
- percentages = percentage.is_a?(Array) ? percentage : [percentage]
140
- percentages.each { |p| ctx.send(:validate_percentile!, p) }
141
- scalar = !percentage.is_a?(Array)
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.7"
4
+ VERSION = "0.1.9"
5
5
  end
data/test/jrf_test.rb CHANGED
@@ -469,6 +469,14 @@ assert_equal(
469
469
  "array percentile output"
470
470
  )
471
471
 
472
+ stdout, stderr, status = run_jrf('percentile(_["foo"], 0.25.step(1.0, 0.25))', input_sum)
473
+ assert_success(status, stderr, "enumerable percentile")
474
+ assert_equal(
475
+ ['[1,2,3,4]'],
476
+ lines(stdout),
477
+ "enumerable percentile output"
478
+ )
479
+
472
480
  input_with_nil = <<~NDJSON
473
481
  {"foo":1}
474
482
  {"foo":null}
@@ -942,6 +950,30 @@ assert_equal([[5, 7]], j.call([{"a" => 1, "b" => 2}, {"a" => 2, "b" => 3}]), "li
942
950
  j = Jrf.new(proc { group_by(_["k"]) { count() } })
943
951
  assert_equal([{"x" => 2, "y" => 1}], j.call([{"k" => "x"}, {"k" => "x"}, {"k" => "y"}]), "library group_by")
944
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
+
945
977
  # reducer then passthrough
946
978
  j = Jrf.new(
947
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.7
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - kazuho