53 lines
2.3 KiB
Markdown
53 lines
2.3 KiB
Markdown
|
# wheel-utils
|
||
|
|
||
|
I wanted to write a fast, straight-forward, extensible fizzbuzz program. This is a small utility I wrote to do it. It can generate simple wheels that you can use if you have a recurring sequence (like fizzbuzz).
|
||
|
|
||
|
## documentation
|
||
|
|
||
|
## usage
|
||
|
|
||
|
Put it in your site-dir. Then you should be able to:
|
||
|
|
||
|
(use-modules (wheel-utils wheel))
|
||
|
|
||
|
### every-n in value [n]
|
||
|
|
||
|
(every-n-in "fizz" 3) -> '(#f #f "fizz")
|
||
|
(every-n-in "bugz" 2 3) -> (#f "bugz" #f)
|
||
|
|
||
|
This is a procedure used to generate proto-wheels that are merged together into a larger proto-wheel using generate-wheel.
|
||
|
|
||
|
### generate-wheel combine proto-wheels ...
|
||
|
Combines proto-wheels using the combine procedure and returns a new, larger proto-wheel:
|
||
|
|
||
|
(define fizz '(every-n-in "fizz" 3))
|
||
|
(define buzz '(every-n-in "buzz" 5))
|
||
|
(define (combine . args)
|
||
|
(let ((args (filter values args)))
|
||
|
(if (null? args)
|
||
|
#f
|
||
|
(apply string-append args))))
|
||
|
(define proto-wheel (generate-wheel combine fizz buzz))
|
||
|
|
||
|
Proto-wheel can now be converted to a circular list or a vector depending on your needs. Most people will probably convert it to a circular-list, but wheels _can_ grow prohibitively large and using a vector might fit more into the cpu cache. The fizzbuzz wheel is just 15 elements long. A fizz-buzz-bar (where bar is instead of multiples of 7) is 105 elements long. fizz-buzz-bar-quux (quux instead of 11) means 1155 elements.
|
||
|
|
||
|
## How fast was your fizzbuzz
|
||
|
How does our fizzbuzz fare against a naive implementation? Using the code in example.scm (both procedures generate lists of fizzy and buzzy goodness) these are my numbers:
|
||
|
|
||
|
scheme@(guile-user)> ,time (car (regular-fizzbuzz 10000000))
|
||
|
$26 = 1
|
||
|
;; 1.161756s real time, 1.242915s run time. 0.314005s spent in GC.
|
||
|
scheme@(guile-user)> ,time (car (wheel-fizzbuzz 10000000))
|
||
|
$27 = 1
|
||
|
;; 0.528079s real time, 0.614268s run time. 0.312021s spent in GC.
|
||
|
|
||
|
It can of course be done faster, but not necessarily in such a simple way. The wheel version is also trivially extensible if you are unsure about your future fizzbuzzing needs.
|
||
|
|
||
|
## Todo
|
||
|
|
||
|
It would be fun to be able to generate step wheels, like for example a prime wheel where we can just skip large parts of the number line.
|
||
|
|
||
|
## Licence
|
||
|
|
||
|
a simplified ISC. Non of that big-letter stuff matters in my jurisdiction, so I removed it. You may use it under regular ISC if you please.
|