1.1.0 โ€ข Published 8 months ago

fzbz v1.1.0

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
8 months ago

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