fzbz is a fizz-buzz solution
Since before the dawn of civilization around (time_t) -1 the question of which numbers fizz, which of them buzz, and whatever can we say about the ones that do both, has commanded the attention of many a computer scientist. It is said that Silicon could be an analogue for Carbon as a chemical basis for life elsewhere in the Universe, we however, must find ways of demeaning the element other than having it be part of such a sorry state of affairs like organic life... which leads us to this package.
In the interest of solving the general fizzbuzz question, I have forcefully convinced the Silicon-demons in your think-box to partake in a fittingly cursed implementation of the famous problem.
There are essentially two things that you might care in this package: the generic_engine_factory and the Agent. The latter is a straightforward wrapper of the former, which merely proxies access through the [] operator to the engine, possibly with some caching if a cache factory was provided. The engine factory, however, requires more concepts to be described.
If _๐ โฃ ๐ค : ๐๐๐ญ โ ๐๐จ๐ง_ is the adjoint functor pair comprised of the FreeMonoid and Forget functors and _๐_ is the set of Unicode characters that can comprise an identifier, then one can roughly think of an _unordered fizzpecification_ as the subobject of ๐๐๐ญ(๐ค๐๐, ๐คโ โจฟ {โฅ}) comprised of the monic partial functions of finite support, which we'll denote _Fแตคโ_.
Parallel to that, one has the following scenario: _๐ค๐๐ ร ๐คโ โ ๐ค(๐๐ ร โ)_, on which we we apply the monad _๐ค๐_ yielding _๐ค๐๐ค(๐๐ ร โ)_. For the sake of simplicity one may assume _๐ค๐๐ค(๐๐ ร โ)_ is given by โ(m:โ) m โ โ(n:โ) (n โ ๐)รโ; We say that _(m, f)_ is โniceโ whenever _ฯโf_, and _ฯโf_ are monic (we disregard the monicity of _ฯโf_). It is thus possible to describe what we should call _Fโแตฃ_ in terms of limits of some sort or another, suffice to say _Fโแตฃ_ is merely the subobject of _๐ค๐๐ค(๐๐ ร โ)_ containing only the nice ones out of the lot.
It shouldn't be hard to see how to establish a relation between _Fโแตฃ_ and _Fแตคโ_, but an isomorphism cannot be established since _Fโแตฃ_ has order information because each _m:โ_ is ordered, whereas _Fแตคโ_ disregards that information. This is the crux of the matter as to why we allow these two representations. One could rectify the situation and obtain an isormorphism by providing total order on the support of each function but that is beyond the scope of this package.
Therefore, a Fizzpecification is an instance of the following type: _Fโแตฃ โจฟ Fแตคโ_.
Less precisely, it's either a dictionary like {"fizz" : 3, "buzz" : 5}, or a list of pairs like [["fizz", 3], ["buzz", 5]], with the list ensuring "fizz" comes before "buzz" when the number is a fizzer and a buzzer, while the dictionary just ensures fizzing and buzzing will happen and won't opine on which happens first.
An engine is merely a function that exposes numbers that fizz or buzz according to a fizzpecification, the generic engine factory implements a cutting edge, fully ethereal, branching minimal, general fizz-buzz classifying algorithm given a fizzpecification:
``js`
function generic_fb_engine(๐s, y = (๐ฆ, ฮณ) => (ฮณ % ๐ฆ) == 0) {
let ฮป = (๐ฆ, ๐) => (ฮณ, ั) => y(๐ฆ, ฮณ)? ๐ฆ => ั("") + ๐: ั;
let ๊ญ = 0;
let าฏ = [แปฟ => ๐ฆ => ๐ฆ];
let าฏาฏ = (๐, สธ, ั) => าฏ.push(แปฟ => ฮป(สธ, ๐)(แปฟ, าฏั));
if (Array.isArray(๐s)) for (let [๐, สธ] of ๐s)
าฏาฏ(๐, สธ, ๊ญ++);
else for (const ๐ in ๐s)
าฏาฏ(๐, ๐s[๐], ๊ญ++);
return แปฟ => าฏาฏ.length - 1(แปฟ);
}
Most of the time you will probably want the default engine, which can be obtained by talking to the default_engine_provider, which constructs a default fizz-buzz engine for you. Don't forget to thank him.
As mentioned, an Agent merely wraps a potential cache + an engine. Typically you could integrate caching with the solution itself, but the solution is pure and this way is far more extensible and customizable for the user, amirite.
`js``
const engie = require('./fzbz.js').default_engine_provider
const fzbz = engie.provide();
engie.thank();
for (let i of Array.from(Array(30).keys()))
console.log(fzbz(i))
See dedicated file