fzbz v1.1.0
FZBZ
A generic Fizz Buzz Solution package.
Installation
npm i fzbz
Introduction
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.
Usage
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.
Fizzpecification
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.
Usage (cont.)
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:
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.
Examples
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))
License
See dedicated file