You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -188,7 +188,7 @@ So imagine moving down this list from left to right with `coins = 5` until the t
188
188
189
189
If you were to count the number of steps you took moving left toright you would get 3 as you should.
190
190
191
-
This seems to just be a simple loop in an iterator
191
+
When considered in that way, this is just be a simple loop in an iterator. I love the loop facility so lets use that, and the nice thing about emacs generators is that because closures aren't "real" (everything is just a list after all) you can yield from pretty much anywhere - even from inside what in other languages would be higher order functions!
192
192
193
193
(iter-make
194
194
(cl-loop for c in sorted-costs
@@ -199,7 +199,11 @@ This seems to just be a simple loop in an iterator
199
199
do (iter-yield c)
200
200
finally return nil))
201
201
202
-
And we can test that by passing in the example tables above and evaluating things.
202
+
This will generate an iteration of all costs we have encountered before the runninig total was exceeded. Note that because we are passing back values with `iter-yield`, the `nil` value returned in the above code is immaterial, it is just necessary for the `cl-loop` syntax.
203
+
204
+
While we do not actually want the costs, we want just the total amount of costs that we passed, because we are using a generator here, there is no extra memory usage, we are simply streaming back values so adding this bit of functionality is effectively "free". To get the total number of ice cream bars the boy can get, we simply count the number (not total) of costs that are in the stream.
205
+
206
+
We can test that by passing in the example tables above and evaluating things.
203
207
204
208
(require 'generator)
205
209
(let* ((expected (car (cdaddr data)))
@@ -213,21 +217,21 @@ And we can test that by passing in the example tables above and evaluating thing
213
217
count 1)))
214
218
(princ (if (equalp actual expected) "PASS" "FAIL")))
215
219
216
-
So call this against [Example 1](#orgd537517)
220
+
So call this against [Example 1](#org09b5613)
217
221
218
222
1
219
223
1
220
224
2
221
225
3
222
226
PASS
223
227
224
-
Oh wow that seemeds to work. What about some others?
228
+
Cool that seemeds to work. What about some others?
Copy file name to clipboardExpand all lines: maximum-ice-cream-bars/README.org
+7-3
Original file line number
Diff line number
Diff line change
@@ -86,7 +86,7 @@ So imagine moving down this list from left to right with ~coins = 5~ until the t
86
86
87
87
If you were to count the number of steps you took moving left toright you would get 3 as you should.
88
88
89
-
This seems to just be a simple loop in an iterator
89
+
When considered in that way, this is just be a simple loop in an iterator. I love the loop facility so lets use that, and the nice thing about emacs generators is that because closures aren't "real" (everything is just a list after all) you can yield from pretty much anywhere - even from inside what in other languages would be higher order functions!
90
90
91
91
#+name: iterate-sorted-costs-you-could-afford
92
92
#+begin_src emacs-lisp :eval no
@@ -100,7 +100,11 @@ This seems to just be a simple loop in an iterator
100
100
finally return nil))
101
101
#+end_src
102
102
103
-
And we can test that by passing in the example tables above and evaluating things.
103
+
This will generate an iteration of all costs we have encountered before the runninig total was exceeded. Note that because we are passing back values with ~iter-yield~, the ~nil~ value returned in the above code is immaterial, it is just necessary for the ~cl-loop~ syntax.
104
+
105
+
While we do not actually want the costs, we want just the total amount of costs that we passed, because we are using a generator here, there is no extra memory usage, we are simply streaming back values so adding this bit of functionality is effectively "free". To get the total number of ice cream bars the boy can get, we simply count the number (not total) of costs that are in the stream.
106
+
107
+
We can test that by passing in the example tables above and evaluating things.
0 commit comments