range_classifier 0.0.1
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 +7 -0
- data/lib/range_classifier.rb +44 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e896957b65f6426652dde1b945d894ccfe130f2c
|
4
|
+
data.tar.gz: 4d494959c9745dd3661eab21ea8d652d7fd5b531
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6f15c9990a6e8124df2d6e74b6d1ce5d069c7d4b1bb5aa64bb2b697e7b3f5474013b03b20bac9c445c18a6aadfb4ff6bc1d8ddb1ef9164733d535744d24cdaf7
|
7
|
+
data.tar.gz: 1e9f6cac1bbd4233fd372f5ea005fe254a80247ae70248686f68c8870387555018a3df44c062476e016b17a3b20d547bf0c326286a961edb7ad4c1cc4b0f9714
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class RangeClassifier
|
2
|
+
attr_reader :projection
|
3
|
+
|
4
|
+
def initialize(projection=RangeClassifier.id, ranges: [])
|
5
|
+
@projection = projection
|
6
|
+
@ranges = ranges
|
7
|
+
end
|
8
|
+
|
9
|
+
def append_ranges(ranges)
|
10
|
+
@ranges.concat(ranges)
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def append_lower_stream(stream, low: nil)
|
15
|
+
stream.each do |high, result|
|
16
|
+
@ranges << {
|
17
|
+
lower: [low, high],
|
18
|
+
result: result
|
19
|
+
}
|
20
|
+
low = high
|
21
|
+
end
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
def classify(element, *args)
|
26
|
+
lkey, ukey = @projection.call(element, *args)
|
27
|
+
selected = @ranges.find { |range|
|
28
|
+
matches_section(range[:lower], lkey) && matches_section(range[:upper], ukey)
|
29
|
+
}
|
30
|
+
selected[:result]
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def self.id
|
36
|
+
-> (v) {[v, v]}
|
37
|
+
end
|
38
|
+
|
39
|
+
def matches_section(range, key)
|
40
|
+
return true if range.nil?
|
41
|
+
l, u = range
|
42
|
+
(l.nil? || l <= key) && (u.nil? || u >= key)
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: range_classifier
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Fowler
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Because sometimes you need a width as well, and this gives you that.
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/range_classifier.rb
|
20
|
+
homepage: http://rubygems.org/gems/range_classifier
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.4.5
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Ranges to values, with a handy twist
|
44
|
+
test_files: []
|