magick 0.1.1 → 0.1.2

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: 4826f852b57d6f69bbf1d1d1c4671174e3b3d68dae520b5fd09f43a7c2ba2bac
4
- data.tar.gz: 0d56f6b3d4c9a6ec5fe45759695375700aacdedc28dcd9b1dccc23c06e9bff37
3
+ metadata.gz: 5ae8b9cd9f9526ea3adac12fd63ab30cd38895c035493a3f4866c83d25df015b
4
+ data.tar.gz: d60e88dbedd1c8d9c7bedf778beeefed2c3012678ce5022a741c02471ba76abd
5
5
  SHA512:
6
- metadata.gz: ebe917fea532e9cd78d94e247120278f9f155cb857ff0daaea8f9538342a33fefff6e8a3c813180763845fd822efababcfaa0d70a0101d5b665becded5815a46
7
- data.tar.gz: 88573678ce6d983164280c784f14dd285f1136c0354cc1c40a3c92df1538637ea909b5f9ad4d1098a32eb067ec34a2b6a08a5058839d4d3a17aa0080115a09fb
6
+ metadata.gz: 9f033baf4e66b21b0c542d7f99ab95a717fb864acb48e5f51ba06e3c7eec9cd8e68f3d659272942fe4ffb487c618bb563e2b6c6765f4e016e196826cfd6cc45b
7
+ data.tar.gz: c967a3d6c062ee01b0f804cae609d8f281d4a33f66ba7449edaf2a08f8c505f52607090f769811c1db205745bf458bebdcd506cacfadc2f29b6295b3acf74bf9
data/lib/magick/loops.rb CHANGED
@@ -31,6 +31,7 @@ module Magick
31
31
  }
32
32
  end
33
33
 
34
+ # sum and length both assume they will be given a list
34
35
  def sum = fold.(Magick::Maths::add).(0)
35
36
 
36
37
  def length = fold.(->(acc) { ->(elem) { acc + 1 }}).(0)
data/lib/magick/maths.rb CHANGED
@@ -11,7 +11,7 @@ module Magick
11
11
  def subtract = ->(x){ ->(y) { add.(x).(negate.(y)) }}
12
12
 
13
13
  def multiply
14
- power.(add)
14
+ power_2.(add)
15
15
  end
16
16
  alias :mult :multiply
17
17
 
@@ -48,7 +48,7 @@ module Magick
48
48
  end
49
49
 
50
50
  def exp
51
- power.(multiply)
51
+ power_2.(multiply)
52
52
  end
53
53
 
54
54
  def power
@@ -73,6 +73,60 @@ module Magick
73
73
  }
74
74
  end
75
75
 
76
+ # power, as written, will have a stack trace error if we try to
77
+ # do something like power.(mult).(2).(32)
78
+ # If we eliminate the recursion, we can avoid this, but it means we need
79
+ # to use some regular Ruby constructs (eg, `while`, instead of the Y combinator)
80
+ # The following is essentially how Stepanov & McJones define `power` in
81
+ # Elements of Programming
82
+ def power_accumulate_positive
83
+ ->(op){
84
+ ->(r){
85
+ ->(a){
86
+ ->(n){
87
+ while (true)
88
+ if n % 2 != 0
89
+ r = op.(r).(a)
90
+ return r if n == 1
91
+ end
92
+ a = op.(a).(a)
93
+ n = n/2
94
+ end
95
+ }
96
+ }
97
+ }
98
+ }
99
+ end
100
+
101
+ def power_accumulate
102
+ ->(op){
103
+ ->(r){
104
+ ->(n){
105
+ ->(a){
106
+ return r if n == 0
107
+ power_accumulate_positive.(op).(r).(n).(a)
108
+ }
109
+ }
110
+ }
111
+ }
112
+ end
113
+
114
+ def power_2
115
+ ->(op){
116
+ ->(b){
117
+ ->(e){
118
+ while (e % 2 == 0)
119
+ b = op.(b).(b)
120
+ e = e/2
121
+ end
122
+ e = e / 2
123
+ return b if e == 0
124
+ power_accumulate.(op).(b).(op.(b).(b)).(e)
125
+ }
126
+ }
127
+ }
128
+ end
129
+
76
130
  def average
77
131
  Smullyan::Birds::Phi.
78
132
  (Magick::Maths::div_to_f).
@@ -80,19 +134,24 @@ module Magick
80
134
  (Magick::Loops::length)
81
135
  end
82
136
 
83
- def powers_of_two = power.(multiply).(2)
137
+ def powers_of_two = exp.(2)
84
138
 
85
139
  # x and y are pairs of ints
86
140
  def fibonacci_matrix_multiply = ->(x){
87
141
  ->(y){
88
- [x[0] * (y[1] + y[0]) + x[1] * y[0], x[0] * y[0] + x[1] * y[1]]
142
+ [
143
+ x[0] * (y[1] + y[0]) +
144
+ x[1] * y[0],
145
+ x[0] * y[0] +
146
+ x[1] * y[1]
147
+ ]
89
148
  }
90
149
  }
91
150
 
92
151
  def fibonacci
93
152
  ->(n){
94
153
  n == 0 ? 0 :
95
- power.(fibonacci_matrix_multiply).([1,0]).(n)[0]
154
+ power_2.(fibonacci_matrix_multiply).([1,0]).(n)[0]
96
155
  }
97
156
  end
98
157
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Magick
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Phil Crissman