the-wheel
Usage
npm install the-wheel
For
Let's say you wanted to write this for
loop without iteration:
let sum = 0;forlet i = 0; i < 10; i = i + 1 sum = sum + i;
Now you can write this "clearer" implementation instead:
const For = ; let sum = 0; { return iterator < 10;} { return iterator + 1;} { sum = sum + iterator;} ;
Or if you want to write it more compactly in a form similar to an actual for
loop:
const For = ; let sum = 0;;
What if you want to reflect the break
and continue
implemented in native loops?
Given that the body
functions don't need return anything, you take advantage of
this by return
-ing special cases.
A simple return
is the same as a native continue
:
// only log even numbers forlet i = 0; i < 10; i ++ ifi % 2 != 0 continue; console; ;
You can also implement a break
in this way by returning something from
your body
function.
// linear searchconst target = 'Carmen Sandiego';let where; forlet i = 0; i < worldlength; i ++ ifworldi === target where = i; break; ;
While
While
has a very similar usage to For
let sum = 0;let i = 0; whilei < 10 sum += i;
Can in turn be written as follows
const While = ; let sum = 0;let i = 0; { return i < 10;} { sum += i; i += 1;} ;
Or more succinctly as
;
You can implement break
and continue
in the same manner as a For
loop
by performing return
in your body
function.
Tests
npm test