mug 0.5.2 → 0.5.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
  SHA1:
3
- metadata.gz: 62f380b7208bcca73fc03c01f9e768635cd86db6
4
- data.tar.gz: 5c6fda3e95d3b1cf91a433e598bcccbe435647a2
3
+ metadata.gz: c0edc2446a16fda4757caa14b0fda411f56cd579
4
+ data.tar.gz: 7282d21792083c5b6702933c248be85b188bdf4e
5
5
  SHA512:
6
- metadata.gz: ecb3583f89035c101ef30645cd188c2d1927901dbb4e0ab6f3e3db8d924e053b28de09ffc82834fa3300b18ff158958515c0c45a6240d039294006abf53c01b5
7
- data.tar.gz: 55c48683d37e24b974982e5bd4f039e63df00b04fe7427b172932385f1df94001e829407f87446d29c8272b682fb4dc3946aa253761d85ab0265f1201df647bc
6
+ metadata.gz: 6287a2a374fffb41434813eaeeebf7f5e8336e2d9f099fda1d81842ff94fbe0f0b0bae9816dfeb2cedb72e3982a0058237964e8743b6841c1710a43fc75f94f7
7
+ data.tar.gz: eaa5258cba1132392718fcfaa5982396d8ef0a646c537c8f383643cd3720fbaa69ecca97adc8e7fa3b53b21152ce86c6d9161169b3ad41090d135d89bb211293
data/lib/mug.rb CHANGED
@@ -4,6 +4,7 @@ require_relative 'mug/any-and-all'
4
4
  require_relative 'mug/apply'
5
5
  require_relative 'mug/array/extend'
6
6
  require_relative 'mug/array/minus'
7
+ require_relative 'mug/array/samples'
7
8
  require_relative 'mug/bool'
8
9
  require_relative 'mug/clamp'
9
10
  require_relative 'mug/counts'
@@ -1,2 +1,3 @@
1
1
  require_relative 'array/extend'
2
2
  require_relative 'array/minus'
3
+ require_relative 'array/samples'
@@ -0,0 +1,56 @@
1
+
2
+ class Array
3
+
4
+ #
5
+ # Choose a random subset of elements from the array.
6
+ #
7
+ # The elements are chosen by using random and unique indices into the
8
+ # array in order to ensure that an element doesn't repeat itself
9
+ # unless the array already contained duplicate elements.
10
+ #
11
+ # If the array is empty, always returns an empty array.
12
+ #
13
+ # The optional +min+ and +max+ arguments restrict the size of the
14
+ # returned array. +min+ must be >= 0, and +max+ must be >= +min+.
15
+ # (Both values are clamped to the size of the array.)
16
+ #
17
+ # The optional +random+ argument will be used as the random number
18
+ # generator.
19
+ #
20
+ def samples min: nil, max: nil, random: nil
21
+ min = 1 if min.nil?
22
+ min = length if min > length
23
+
24
+ max = length if max.nil?
25
+ max = length if max > length
26
+
27
+ raise ArgumentError, "min must be >= 0 (#{min})" if min < 0
28
+ raise ArgumentError, "min (#{min}) must be <= max (#{max})" if min > max
29
+
30
+ if random
31
+ n = random.rand(min..max)
32
+ sample n, random: random
33
+ else
34
+ n = rand(min..max)
35
+ sample n
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+ =begin
42
+ Copyright (c) 2016, Matthew Kerwin <matthew@kerwin.net.au>
43
+
44
+ Permission to use, copy, modify, and/or distribute this software for any
45
+ purpose with or without fee is hereby granted, provided that the above
46
+ copyright notice and this permission notice appear in all copies.
47
+
48
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
49
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
50
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
51
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
52
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
53
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
54
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
55
+ =end
56
+
@@ -0,0 +1,92 @@
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ $ARRAY = proc { 8.times.to_a }
5
+ $TRIES = 40320 # 8 factorial
6
+ $MIN = 4
7
+ $MAX = 6
8
+
9
+ class MyPRNG
10
+ def rand n
11
+ case n
12
+ when Integer
13
+ n - 1
14
+ when Range
15
+ n.max
16
+ end
17
+ end
18
+ end
19
+
20
+ require_relative '../lib/mug/array/samples'
21
+ class Test_array_extend < Test::Unit::TestCase
22
+ def test_samples
23
+ a = $ARRAY.call
24
+ $TRIES.times do
25
+ s = a.samples
26
+ assert( s.length >= 1 )
27
+ assert( s.length <= a.length )
28
+ assert( s.uniq == s )
29
+ assert( s.all? {|n| a.include? n } )
30
+ end
31
+ end
32
+ def test_samples_min
33
+ a = $ARRAY.call
34
+ # successes
35
+ $TRIES.times do
36
+ s = a.samples( :min => $MIN )
37
+ assert( s.length >= $MIN )
38
+ assert( s.length <= a.length )
39
+ assert( s.uniq == s )
40
+ assert( s.all? {|n| a.include? n } )
41
+ end
42
+ # failures
43
+ assert_raise( ArgumentError ) { a.samples( :min => -1 ) }
44
+ end
45
+ def test_samples_max
46
+ a = $ARRAY.call
47
+ # successes
48
+ $TRIES.times do
49
+ s = a.samples( :max => $MAX )
50
+ assert( s.length >= 1 )
51
+ assert( s.length <= $MAX )
52
+ assert( s.uniq == s )
53
+ assert( s.all? {|n| a.include? n } )
54
+ end
55
+ # failures
56
+ assert_raise( ArgumentError ) { a.samples( :max => -1 ) }
57
+ end
58
+ def test_samples_minmax
59
+ a = $ARRAY.call
60
+ # successes
61
+ $TRIES.times do
62
+ s = a.samples( :min => $MIN, :max => $MAX )
63
+ assert( s.length >= $MIN )
64
+ assert( s.length <= $MAX )
65
+ assert( s.uniq == s )
66
+ assert( s.all? {|n| a.include? n } )
67
+ end
68
+ # failures
69
+ assert_raise( ArgumentError ) { a.samples( :min => $MAX, :max => $MIN ) }
70
+ end
71
+ def test_samples_random
72
+ a = $ARRAY.call
73
+ $TRIES.times do
74
+ s = a.samples( :random => Random.new )
75
+ assert( s.length >= 1 )
76
+ assert( s.length <= a.length )
77
+ assert( s.uniq == s )
78
+ assert( s.all? {|n| a.include? n } )
79
+ end
80
+ end
81
+ def test_samples_random2
82
+ a = $ARRAY.call
83
+ $TRIES.times do
84
+ s = a.samples( :random => MyPRNG.new )
85
+ assert( s.length >= 1 )
86
+ assert( s.length <= a.length )
87
+ assert( s.uniq == s )
88
+ assert( s.all? {|n| a.include? n } )
89
+ end
90
+ end
91
+ end
92
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mug
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Kerwin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-19 00:00:00.000000000 Z
11
+ date: 2016-09-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  == MUG: Matty's Ultimate Gem
@@ -29,6 +29,7 @@ files:
29
29
  - lib/mug/array.rb
30
30
  - lib/mug/array/extend.rb
31
31
  - lib/mug/array/minus.rb
32
+ - lib/mug/array/samples.rb
32
33
  - lib/mug/bool.rb
33
34
  - lib/mug/clamp.rb
34
35
  - lib/mug/counts.rb
@@ -61,6 +62,7 @@ files:
61
62
  - test/test-apply.rb
62
63
  - test/test-array-extend.rb
63
64
  - test/test-array-minus.rb
65
+ - test/test-array-samples.rb
64
66
  - test/test-bool.rb
65
67
  - test/test-clamp.rb
66
68
  - test/test-counts.rb
@@ -126,6 +128,7 @@ test_files:
126
128
  - test/test-hashmap.rb
127
129
  - test/test-loop-with.rb
128
130
  - test/test-maybe.rb
131
+ - test/test-array-samples.rb
129
132
  - test/test-hashop.rb
130
133
  - test/test-iterator-method.rb
131
134
  - test/test-apply.rb