dry-effects 0.1.2 → 0.1.3

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: 2b27bb0be01cba09f3ce2957a46f61c89ab25f3ff06cfc4ecea30b20ebaa9817
4
- data.tar.gz: d9066e6045bdf2c57f4102d17aefdd4733d4c97698740eafc463881f675770b7
3
+ metadata.gz: 06b5f9b8d829b3fc846dfe06e838492c4022a164406a3371039e0d258206bb10
4
+ data.tar.gz: 3963e477f1036e4b3657a1cf2ba1ebad879334069c152910e365541b2cd9a809
5
5
  SHA512:
6
- metadata.gz: 27e4ad1dbdb3dca88b20e8da65828060961319d1b16e21144c5361df94078018c5a2626ddc5387efe70cfc6e4d580da3c856ea703ba8b26be7c02d9943c8d579
7
- data.tar.gz: 1eb0f585c19f17cf75c9411a297e460107654af280b414bb9be582054de33e09117ff6d52b429cce0df221e7bb9dc6e90ee8df53a84536b26cf6b36186077a73
6
+ metadata.gz: 8d1259fe383b29301b2beb6b5b08af66d0161abeb4bc6c3884885b4860c3fbda38b1c6cb95c61f08780d40aacd6be3be4d96553ac694735021ed612f62823f77
7
+ data.tar.gz: 1466e9485f008b74d4c4a00f9d2dd2fa5f241bc31c569dd871b1aec73dcc12aee072f71530a01f54c1a55c9578ff350d84ee12420216125ef0329b2ed3cd42ec
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ # v0.1.3 - 2019-12-20
2
+
3
+ ## Added
4
+
5
+ - Options for the random provider. You can pass a `seed` or a proc that will be used to generate random values. It is expected the value returned from the proc is within the `0.0...1.0` range (flash-gordon)
6
+ ```ruby
7
+ with_random(seed: 123) { ... }
8
+ with_random(proc {|prev = 0.0| (prev + 0.1) % 1 }) { ... }
9
+ ```
10
+
11
+ [Compare v0.1.2...v0.1.3](https://github.com/dry-rb/dry-effects/compare/v0.1.2...v0.1.3)
12
+
1
13
  # v0.1.2 - 2019-12-15
2
14
 
3
15
  ## Fixed
@@ -10,7 +10,7 @@ module Dry
10
10
 
11
11
  def initialize
12
12
  module_eval do
13
- define_method(:rand) { |n| ::Dry::Effects.yield(Read.(n)) }
13
+ define_method(:rand) { |n = nil| ::Dry::Effects.yield(Read.(n)) }
14
14
  end
15
15
  end
16
16
  end
@@ -6,7 +6,78 @@ module Dry
6
6
  module Effects
7
7
  module Providers
8
8
  class Random < Provider[:random]
9
- public :rand
9
+ DEFAULT = -> _ { ::Random::DEFAULT.rand }
10
+
11
+ DEFAULT_RANGE = 0.0...1.0
12
+
13
+ def rand(range_or_limit = nil)
14
+ range_or_limit ||= DEFAULT_RANGE
15
+
16
+ if range_or_limit.is_a?(::Range)
17
+ low = range_or_limit.begin
18
+ high = range_or_limit.end
19
+ exclude_end = range_or_limit.exclude_end?
20
+ elsif range_or_limit >= 1
21
+ low = 0
22
+ high = range_or_limit.floor
23
+ exclude_end = true
24
+ else
25
+ low = 0.0
26
+ high = range_or_limit.to_f
27
+ exclude_end = true
28
+ end
29
+
30
+ if low.is_a?(::Integer)
31
+ next_integer(low, high, exclude_end)
32
+ else
33
+ next_float(low, high)
34
+ end
35
+ end
36
+
37
+ def call(options = Undefined)
38
+ @prev = nil
39
+ @generator = build_generator(options)
40
+ yield
41
+ end
42
+
43
+ def next_integer(low, high, exclude_end)
44
+ @prev = @generator.(@prev) % 1
45
+
46
+ value = low + (@prev * (high - low)).round
47
+
48
+ if value.eql?(high) && exclude_end
49
+ low
50
+ else
51
+ value
52
+ end
53
+ end
54
+
55
+ def next_float(low, high)
56
+ @prev = @generator.(@prev) % 1
57
+
58
+ low + (@prev * (high - low))
59
+ end
60
+
61
+ def build_generator(options)
62
+ case options
63
+ when Undefined
64
+ DEFAULT
65
+ when ::Hash
66
+ if options.key?(:seed)
67
+ random = ::Random.new(options[:seed])
68
+ else
69
+ random = ::Random::DEFAULT
70
+ end
71
+
72
+ -> _ { random.rand }
73
+ else
74
+ generator = options
75
+
76
+ lambda do |prev|
77
+ prev.nil? ? generator.() : generator.(prev)
78
+ end
79
+ end
80
+ end
10
81
  end
11
82
  end
12
83
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dry
4
4
  module Effects
5
- VERSION = '0.1.2'
5
+ VERSION = '0.1.3'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-effects
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nikita Shilnikov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-15 00:00:00.000000000 Z
11
+ date: 2019-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby