Discuss Scratch

Redstone1080
Scratcher
1000+ posts

What's the best code you've seen?

For me it's this:
async function* asyncGenerator() {
  let i = 0;
  while (i < 3) {
    yield i++;
  }
}
(async () => {
  for await (const num of asyncGenerator()) {
    console.log(num);
  }
})();
// 0
// 1
// 2

Idk why, but this code is so satisfying. It's so clean you can use it as a plate and eat off of it.

Edit: Okay, fine, you can post stupidly bad code too.

Last edited by Redstone1080 (Oct. 12, 2022 00:10:38)

MagicCrayon9342
Scratcher
1000+ posts

What's the best code you've seen?

// Script to check if the script uses disallowed methods and throws error if it does.
var content;
function checkPlugin(namespace) {
    let content = fetch("../plugins/" + namespace + "/plugin.js").then(res => res.text);
    var ret = Boolean(1);
    function testCondition(condition, errcod) {
        if (condition) {
            console.err(`Could not load plugin ${namespace} because of error: ${errcod}`)
            return false;
        }
  }
}
Redstone1080
Scratcher
1000+ posts

What's the best code you've seen?

MagicCrayon9342 wrote:

(#2)
// Script to check if the script uses disallowed methods and throws error if it does.
var content;
function checkPlugin(namespace) {
    let content = fetch("../plugins/" + namespace + "/plugin.js").then(res => res.text);
    var ret = Boolean(1);
    function testCondition(condition, errcod) {
        if (condition) {
            console.err(`Could not load plugin ${namespace} because of error: ${errcod}`)
            return false;
        }
  }
}
Literally Catalyst's plugin code lolll
MagicCrayon9342
Scratcher
1000+ posts

What's the best code you've seen?

Redstone1080 wrote:

MagicCrayon9342 wrote:

(#2)
// Script to check if the script uses disallowed methods and throws error if it does.
var content;
function checkPlugin(namespace) {
    let content = fetch("../plugins/" + namespace + "/plugin.js").then(res => res.text);
    var ret = Boolean(1);
    function testCondition(condition, errcod) {
        if (condition) {
            console.err(`Could not load plugin ${namespace} because of error: ${errcod}`)
            return false;
        }
  }
}
Literally Catalyst's plugin code lolll
yep
ISTILLMAKESTUFF
Scratcher
500+ posts

What's the best code you've seen?

const pastWeek = [...Array(7).keys()].map(days => new Date(Date.now() - 86400000 * days))
Get an array of the past seven days

Last edited by ISTILLMAKESTUFF (Sept. 26, 2022 02:13:11)

Maximouse
Scratcher
1000+ posts

What's the best code you've seen?

completeness wrote:

// No return value? Function did nothing? What is the function for?
Apparently it used to do something but that part was removed in a commit named “fix bugs”.
DifferentDance8
Scratcher
1000+ posts

What's the best code you've seen?


gilbert_given_189
Scratcher
1000+ posts

What's the best code you've seen?

゚ω゚ノ=/`m´)ノ~┻━┻//*´∇`*/['_'];o=(゚ー゚)=_=3;c=(゚Θ゚)=(゚ー゚)-(゚ー゚);(゚Д゚)=(゚Θ゚)=(o^_^o)/(o^_^o);(゚Д゚)={゚Θ゚:'_',゚ω゚ノ:((゚ω゚ノ==3)+'_')[゚Θ゚],゚ー゚ノ:(゚ω゚ノ+'_')[o^_^o-(゚Θ゚)],゚Д゚ノ:((゚ー゚==3)+'_')[゚ー゚]};(゚Д゚)[゚Θ゚]=((゚ω゚ノ==3)+'_')[c^_^o];(゚Д゚)['c']=((゚Д゚)+'_')[(゚ー゚)+(゚ー゚)-(゚Θ゚)];(゚Д゚)['o']=((゚Д゚)+'_')[゚Θ゚];(o)=(゚Д゚)['c']+(゚Д゚)['o']+(゚ω゚ノ+'_')[゚Θ゚]+((゚ω゚ノ==3)+'_')[゚ー゚]+((゚Д゚)+'_')[(゚ー゚)+(゚ー゚)]+((゚ー゚==3)+'_')[゚Θ゚]+((゚ー゚==3)+'_')[(゚ー゚)-(゚Θ゚)]+(゚Д゚)['c']+((゚Д゚)+'_')[(゚ー゚)+(゚ー゚)]+(゚Д゚)['o']+((゚ー゚==3)+'_')[゚Θ゚];(゚Д゚)['_']=(o^_^o)[o][o];(゚ε゚)=((゚ー゚==3)+'_')[゚Θ゚]+(゚Д゚).゚Д゚ノ+((゚Д゚)+'_')[(゚ー゚)+(゚ー゚)]+((゚ー゚==3)+'_')[o^_^o-゚Θ゚]+((゚ー゚==3)+'_')[゚Θ゚]+(゚ω゚ノ+'_')[゚Θ゚];(゚ー゚)+=(゚Θ゚);(゚Д゚)[゚ε゚]='\\';(゚Д゚).゚Θ゚ノ=(゚Д゚+゚ー゚)[o^_^o-(゚Θ゚)];(o゚ー゚o)=(゚ω゚ノ+'_')[c^_^o];(゚Д゚)[o]='\"';(゚Д゚)['_']((゚Д゚)['_'](゚ε゚+(゚Д゚)[o]+(゚Д゚)[゚ε゚]+(゚Θ゚)+(゚ー゚)+(゚Θ゚)+(゚Д゚)[゚ε゚]+(゚Θ゚)+((゚ー゚)+(゚Θ゚))+(゚ー゚)+(゚Д゚)[゚ε゚]+(゚Θ゚)+(゚ー゚)+((゚ー゚)+(゚Θ゚))+(゚Д゚)[゚ε゚]+(゚Θ゚)+((o^_^o)+(o^_^o))+((o^_^o)-(゚Θ゚))+(゚Д゚)[゚ε゚]+(゚Θ゚)+((o^_^o)+(o^_^o))+(゚ー゚)+(゚Д゚)[゚ε゚]+((゚ー゚)+(゚Θ゚))+(c^_^o)+(゚Д゚)[゚ε゚]+(゚ー゚)+((o^_^o)-(゚Θ゚))+(゚Д゚)[゚ε゚]+(゚Θ゚)+(゚Θ゚)+(c^_^o)+(゚Д゚)[゚ε゚]+(゚Θ゚)+(゚ー゚)+((゚ー゚)+(゚Θ゚))+(゚Д゚)[゚ε゚]+(゚Θ゚)+((゚ー゚)+(゚Θ゚))+(゚ー゚)+(゚Д゚)[゚ε゚]+(゚Θ゚)+((゚ー゚)+(゚Θ゚))+(゚ー゚)+(゚Д゚)[゚ε゚]+(゚Θ゚)+((゚ー゚)+(゚Θ゚))+((゚ー゚)+(o^_^o))+(゚Д゚)[゚ε゚]+(゚ー゚)+(c^_^o)+(゚Д゚)[゚ε゚]+(゚Θ゚)+((o^_^o)-(゚Θ゚))+((゚ー゚)+(o^_^o))+(゚Д゚)[゚ε゚]+(゚Θ゚)+((゚ー゚)+(゚Θ゚))+((゚ー゚)+(o^_^o))+(゚Д゚)[゚ε゚]+(゚Θ゚)+((o^_^o)+(o^_^o))+((o^_^o)-(゚Θ゚))+(゚Д゚)[゚ε゚]+(゚Θ゚)+((゚ー゚)+(゚Θ゚))+(゚ー゚)+(゚Д゚)[゚ε゚]+(゚Θ゚)+(゚ー゚)+(゚ー゚)+(゚Д゚)[゚ε゚]+(゚ー゚)+((o^_^o)-(゚Θ゚))+(゚Д゚)[゚ε゚]+((゚ー゚)+(゚Θ゚))+(゚Θ゚)+(゚Д゚)[o])(゚Θ゚))('_');
Source
You can see that I like obfuscation.
-EmeraldThunder-
Scratcher
1000+ posts

What's the best code you've seen?

Part of a React chess game I'm working on.

Last edited by -EmeraldThunder- (Oct. 9, 2022 06:47:10)

MagicCrayon9342
Scratcher
1000+ posts

What's the best code you've seen?

-EmeraldThunder- wrote:

Part of a React chess game I'm working on.
I'm super confused how you used ‘window’ in React, as both that and document are undefined.
Maximouse
Scratcher
1000+ posts

What's the best code you've seen?

MagicCrayon9342 wrote:

I'm super confused how you used ‘window’ in React, as both that and document are undefined.
React is a library. It doesn't remove any built-in JavaScript objects. (You're probably thinking about Next.js, which renders the DOM on the server.)

Last edited by Maximouse (Oct. 9, 2022 13:16:31)

PkmnQ
Scratcher
1000+ posts

What's the best code you've seen?

const helpfulAdvice = 'This solution does not work!';
const rand = Math.random();
window.ܝ = 065432123456654321234560 * rand;
function myPlanetNeedsMe() {
    answerToLifeTheUniverseAndEverything = 42;
    func = {undefined = !function(){throw helpfulAdvice}(), toString = let `func`} =
		    function answerToLifeTheUniverseAndEverything(){ return 42; };
    return toString != 'undefined' && answerToLifeTheUniverseAndEverything == 493921719446642400000 * rand;
}
TrustyRoo
New Scratcher
49 posts

What's the best code you've seen?

This joke of an array got brought up in this CS group I'm in. Utterly useless practically, but helpful for understanding in what order C syntax is read.
void (*(*x[])()) ();

This defines x as an array of unspecified size of pointers to functions that return pointers to functions that return void.

If we remove some of the parenthesis to make it easier to read it will looks like this:
void * * x[] () ()
then you just read it in a spiral, starting from x[], to the first *, to the first (), then to the first *, then to the last (), then to void
so the “true order” (in terms of how you read it) looks like this:
x[] * () * () void
I think this is a neat bit of code that helps people new to C to understand in what order what sort of things are read. I believe there are images on the internet which display the code with a spiral over it you can follow if you want to use that to help you .
ISTILLMAKESTUFF
Scratcher
500+ posts

What's the best code you've seen?

Did you know you can use variables to access objects?

const person1 = {
  eyes: "blue",
  hair: "blonde"
}
const person2 = {
  eyes: "brown",
  hair: "black"
}
const people = [
  person1, person2
]
let person = 1
eyeColor = people[person].eyes;

Found this out on my own btw
TrustyRoo
New Scratcher
49 posts

What's the best code you've seen?

ISTILLMAKESTUFF wrote:

Did you know you can use variables to access objects?

const person1 = {
  eyes: "blue",
  hair: "blonde"
}
const person2 = {
  eyes: "brown",
  hair: "black"
}
const people = [
  person1, person2
]
let person = 1
eyeColor = people[person].eyes;

Found this out on my own btw
Well at the end of the day, they are all referencing back to the object.
uwv
Scratcher
1000+ posts

What's the best code you've seen?

abstract class Packet<T extends Packet<any> = any> {
    private serializableFields: Array<
        {
            type: SerializableFieldType,
            key: keyof T
        } | {
            type: "custom",
            key: keyof T,
            serialize: PacketSerializeFun,
            deserialize: PacketDeserializeFun
        }> = [];
    private __ctorValues: Partial<Record<keyof T, T[keyof T]>> = {};
    public id: number = -1;
    constructor(values: Partial<Record<keyof T, T[keyof T]>> = {}) {
        this.__ctorValues = values;
    }
    ...
}

as far as code that i have written this is some of the best, not because its very good code but just because its so stupid. however for code that isn't by me i can't really think of anything off the top of my head right now unfortunately

Last edited by uwv (Oct. 10, 2022 06:14:22)

mybearworld
Scratcher
1000+ posts

What's the best code you've seen?

gilbert_given_189 wrote:

(#9)
゚ω゚ノ=/`m´)ノ~┻━┻//*´∇`*/['_'];o=(゚ー゚)=_=3;c=(゚Θ゚)=(゚ー゚)-(゚ー゚);(゚Д゚)=(゚Θ゚)=(o^_^o)/(o^_^o);(゚Д゚)={゚Θ゚:'_',゚ω゚ノ:((゚ω゚ノ==3)+'_')[゚Θ゚],゚ー゚ノ:(゚ω゚ノ+'_')[o^_^o-(゚Θ゚)],゚Д゚ノ:((゚ー゚==3)+'_')[゚ー゚]};(゚Д゚)[゚Θ゚]=((゚ω゚ノ==3)+'_')[c^_^o];(゚Д゚)['c']=((゚Д゚)+'_')[(゚ー゚)+(゚ー゚)-(゚Θ゚)];(゚Д゚)['o']=((゚Д゚)+'_')[゚Θ゚];(o)=(゚Д゚)['c']+(゚Д゚)['o']+(゚ω゚ノ+'_')[゚Θ゚]+((゚ω゚ノ==3)+'_')[゚ー゚]+((゚Д゚)+'_')[(゚ー゚)+(゚ー゚)]+((゚ー゚==3)+'_')[゚Θ゚]+((゚ー゚==3)+'_')[(゚ー゚)-(゚Θ゚)]+(゚Д゚)['c']+((゚Д゚)+'_')[(゚ー゚)+(゚ー゚)]+(゚Д゚)['o']+((゚ー゚==3)+'_')[゚Θ゚];(゚Д゚)['_']=(o^_^o)[o][o];(゚ε゚)=((゚ー゚==3)+'_')[゚Θ゚]+(゚Д゚).゚Д゚ノ+((゚Д゚)+'_')[(゚ー゚)+(゚ー゚)]+((゚ー゚==3)+'_')[o^_^o-゚Θ゚]+((゚ー゚==3)+'_')[゚Θ゚]+(゚ω゚ノ+'_')[゚Θ゚];(゚ー゚)+=(゚Θ゚);(゚Д゚)[゚ε゚]='\\';(゚Д゚).゚Θ゚ノ=(゚Д゚+゚ー゚)[o^_^o-(゚Θ゚)];(o゚ー゚o)=(゚ω゚ノ+'_')[c^_^o];(゚Д゚)[o]='\"';(゚Д゚)['_']((゚Д゚)['_'](゚ε゚+(゚Д゚)[o]+(゚Д゚)[゚ε゚]+(゚Θ゚)+(゚ー゚)+(゚Θ゚)+(゚Д゚)[゚ε゚]+(゚Θ゚)+((゚ー゚)+(゚Θ゚))+(゚ー゚)+(゚Д゚)[゚ε゚]+(゚Θ゚)+(゚ー゚)+((゚ー゚)+(゚Θ゚))+(゚Д゚)[゚ε゚]+(゚Θ゚)+((o^_^o)+(o^_^o))+((o^_^o)-(゚Θ゚))+(゚Д゚)[゚ε゚]+(゚Θ゚)+((o^_^o)+(o^_^o))+(゚ー゚)+(゚Д゚)[゚ε゚]+((゚ー゚)+(゚Θ゚))+(c^_^o)+(゚Д゚)[゚ε゚]+(゚ー゚)+((o^_^o)-(゚Θ゚))+(゚Д゚)[゚ε゚]+(゚Θ゚)+(゚Θ゚)+(c^_^o)+(゚Д゚)[゚ε゚]+(゚Θ゚)+(゚ー゚)+((゚ー゚)+(゚Θ゚))+(゚Д゚)[゚ε゚]+(゚Θ゚)+((゚ー゚)+(゚Θ゚))+(゚ー゚)+(゚Д゚)[゚ε゚]+(゚Θ゚)+((゚ー゚)+(゚Θ゚))+(゚ー゚)+(゚Д゚)[゚ε゚]+(゚Θ゚)+((゚ー゚)+(゚Θ゚))+((゚ー゚)+(o^_^o))+(゚Д゚)[゚ε゚]+(゚ー゚)+(c^_^o)+(゚Д゚)[゚ε゚]+(゚Θ゚)+((o^_^o)-(゚Θ゚))+((゚ー゚)+(o^_^o))+(゚Д゚)[゚ε゚]+(゚Θ゚)+((゚ー゚)+(゚Θ゚))+((゚ー゚)+(o^_^o))+(゚Д゚)[゚ε゚]+(゚Θ゚)+((o^_^o)+(o^_^o))+((o^_^o)-(゚Θ゚))+(゚Д゚)[゚ε゚]+(゚Θ゚)+((゚ー゚)+(゚Θ゚))+(゚ー゚)+(゚Д゚)[゚ε゚]+(゚Θ゚)+(゚ー゚)+(゚ー゚)+(゚Д゚)[゚ε゚]+(゚ー゚)+((o^_^o)-(゚Θ゚))+(゚Д゚)[゚ε゚]+((゚ー゚)+(゚Θ゚))+(゚Θ゚)+(゚Д゚)[o])(゚Θ゚))('_');
Source
You can see that I like obfuscation.
…That code…
Formatting makes things more readable, right?
゚ω゚ノ = /`m´)ノ~┻━┻/ /*´∇`*/["_"];
o = ゚ー゚ = _ = 3;
c = ゚Θ゚ = ゚ー゚ - ゚ー゚;
゚Д゚ = ゚Θ゚ = (o ^ _ ^ o) / (o ^ _ ^ o);
゚Д゚ = {
    ゚Θ゚: "_",
    ゚ω゚ノ: ((゚ω゚ノ == 3) + "_")[゚Θ゚],
    ゚ー゚ノ: (゚ω゚ノ + "_")[o ^ _ ^ (o - ゚Θ゚)],
    ゚Д゚ノ: ((゚ー゚ == 3) + "_")[゚ー゚],
};
゚Д゚[゚Θ゚] = ((゚ω゚ノ == 3) + "_")[c ^ _ ^ o];
゚Д゚["c"] = (゚Д゚ + "_")[゚ー゚ + ゚ー゚ - ゚Θ゚];
゚Д゚["o"] = (゚Д゚ + "_")[゚Θ゚];
o =
    ゚Д゚["c"] +
    ゚Д゚["o"] +
    (゚ω゚ノ + "_")[゚Θ゚] +
    ((゚ω゚ノ == 3) + "_")[゚ー゚] +
    (゚Д゚ + "_")[゚ー゚ + ゚ー゚] +
    ((゚ー゚ == 3) + "_")[゚Θ゚] +
    ((゚ー゚ == 3) + "_")[゚ー゚ - ゚Θ゚] +
    ゚Д゚["c"] +
    (゚Д゚ + "_")[゚ー゚ + ゚ー゚] +
    ゚Д゚["o"] +
    ((゚ー゚ == 3) + "_")[゚Θ゚];
゚Д゚["_"] = (o ^ _ ^ o)[o][o];
゚ε゚ =
    ((゚ー゚ == 3) + "_")[゚Θ゚] +
    ゚Д゚.゚Д゚ノ +
    (゚Д゚ + "_")[゚ー゚ + ゚ー゚] +
    ((゚ー゚ == 3) + "_")[o ^ _ ^ (o - ゚Θ゚)] +
    ((゚ー゚ == 3) + "_")[゚Θ゚] +
    (゚ω゚ノ + "_")[゚Θ゚];
゚ー゚ += ゚Θ゚;
゚Д゚[゚ε゚] = "\\";
゚Д゚.゚Θ゚ノ = (゚Д゚ + ゚ー゚)[o ^ _ ^ (o - ゚Θ゚)];
o゚ー゚o = (゚ω゚ノ + "_")[c ^ _ ^ o];
゚Д゚[o] = '"';
゚Д゚["_"](
    ゚Д゚["_"](
        ゚ε゚ +
            ゚Д゚[o] +
            ゚Д゚[゚ε゚] +
            ゚Θ゚ +
            ゚ー゚ +
            ゚Θ゚ +
            ゚Д゚[゚ε゚] +
            ゚Θ゚ +
            (゚ー゚ + ゚Θ゚) +
            ゚ー゚ +
            ゚Д゚[゚ε゚] +
            ゚Θ゚ +
            ゚ー゚ +
            (゚ー゚ + ゚Θ゚) +
            ゚Д゚[゚ε゚] +
            ゚Θ゚ +
            ((o ^ _ ^ o) + (o ^ _ ^ o)) +
            ((o ^ _ ^ o) - ゚Θ゚) +
            ゚Д゚[゚ε゚] +
            ゚Θ゚ +
            ((o ^ _ ^ o) + (o ^ _ ^ o)) +
            ゚ー゚ +
            ゚Д゚[゚ε゚] +
            (゚ー゚ + ゚Θ゚) +
            (c ^ _ ^ o) +
            ゚Д゚[゚ε゚] +
            ゚ー゚ +
            ((o ^ _ ^ o) - ゚Θ゚) +
            ゚Д゚[゚ε゚] +
            ゚Θ゚ +
            ゚Θ゚ +
            (c ^ _ ^ o) +
            ゚Д゚[゚ε゚] +
            ゚Θ゚ +
            ゚ー゚ +
            (゚ー゚ + ゚Θ゚) +
            ゚Д゚[゚ε゚] +
            ゚Θ゚ +
            (゚ー゚ + ゚Θ゚) +
            ゚ー゚ +
            ゚Д゚[゚ε゚] +
            ゚Θ゚ +
            (゚ー゚ + ゚Θ゚) +
            ゚ー゚ +
            ゚Д゚[゚ε゚] +
            ゚Θ゚ +
            (゚ー゚ + ゚Θ゚) +
            (゚ー゚ + (o ^ _ ^ o)) +
            ゚Д゚[゚ε゚] +
            ゚ー゚ +
            (c ^ _ ^ o) +
            ゚Д゚[゚ε゚] +
            ゚Θ゚ +
            ((o ^ _ ^ o) - ゚Θ゚) +
            (゚ー゚ + (o ^ _ ^ o)) +
            ゚Д゚[゚ε゚] +
            ゚Θ゚ +
            (゚ー゚ + ゚Θ゚) +
            (゚ー゚ + (o ^ _ ^ o)) +
            ゚Д゚[゚ε゚] +
            ゚Θ゚ +
            ((o ^ _ ^ o) + (o ^ _ ^ o)) +
            ((o ^ _ ^ o) - ゚Θ゚) +
            ゚Д゚[゚ε゚] +
            ゚Θ゚ +
            (゚ー゚ + ゚Θ゚) +
            ゚ー゚ +
            ゚Д゚[゚ε゚] +
            ゚Θ゚ +
            ゚ー゚ +
            ゚ー゚ +
            ゚Д゚[゚ε゚] +
            ゚ー゚ +
            ((o ^ _ ^ o) - ゚Θ゚) +
            ゚Д゚[゚ε゚] +
            (゚ー゚ + ゚Θ゚) +
            ゚Θ゚ +
            ゚Д゚[o]
    )(゚Θ゚)
)("_");
TheSecondGilbert
Scratcher
100+ posts

What's the best code you've seen?

completeness wrote:

uwv wrote:

abstract class Packet<T extends Packet<any> = any> {
    private serializableFields: Array<
        {
            type: SerializableFieldType,
            key: keyof T
        } | {
            type: "custom",
            key: keyof T,
            serialize: PacketSerializeFun,
            deserialize: PacketDeserializeFun
        }> = [];
    private __ctorValues: Partial<Record<keyof T, T[keyof T]>> = {};
    public id: number = -1;
    constructor(values: Partial<Record<keyof T, T[keyof T]>> = {}) {
        this.__ctorValues = values;
    }
    ...
}

as far as code that i have written this is some of the best, not because its very good code but just because its so stupid. however for code that isn't by me i can't really think of anything off the top of my head right now unfortunately
If we're comparing weird Typescript code, I think this piece of code I wrote takes the cake:
type True = 'true';
type False = 'false';
type Bool = True | False;
type Not<T extends Bool> = T extends True ? False : True;
type MaxNum = 30;
type IncMaxNum = 31;
type Numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30];
type NAN = 'NaN';
type Num = Numbers[number] | NAN;
type PositiveNum = Exclude<Num, NAN>;
type IncNumbers = AppendArray<IncMaxNum, ArrayRest<Numbers>>;
type Inc<T extends Num> = IncNumbers[Extract<T, number>];
type DecNumbers = PrependArray<NAN, ReverseArray<ArrayRest<ReverseArray<Numbers>>>>;
type Dec<T extends Num> = DecNumbers[Extract<T, number>];
type Sub<T extends PositiveNum, U extends PositiveNum> = T extends U
  ? 0
  : Dec<T> extends PositiveNum
    ? Dec<U> extends PositiveNum
      ? Sub<Dec<T>, Dec<U>>
      : T
    : NAN;
type Mod<T extends PositiveNum, U extends PositiveNum> = Sub<T, U> extends PositiveNum
  ? Mod<Sub<T, U>, U>
  : T;
type List = Array<any>;
type ArrayHead<Arr extends List> = Arr[0];
type ArrayLength<Arr extends List> = Arr["length"];
type ArrHasItems<Arr extends List> = ArrayLength<Arr> extends 0 ? False : True;
type ArrEmpty<Arr extends List> = Not<ArrHasItems<Arr>>;
type ArrayRest<Arr extends List> = ((...a: Arr) => {}) extends (b: any, ...c: infer R) => {} ? R : never;
type PrependArray<Val, Arr extends List> = [Val, ...Arr]
type ReverseArrayIter<Arr extends List, Curr extends List> = ArrEmpty<Arr> extends True ? Curr : ReverseArrayIter<ArrayRest<Arr>, PrependArray<ArrayHead<Arr>, Curr>>;
type ReverseArray<Arr extends List> = ReverseArrayIter<Arr, []>;
type ConcatArrays<Arr1 extends List, Arr2 extends List> = [...Arr1, ...Arr2];
type AppendArray<Val, Arr extends List> = ConcatArrays<Arr, [Val]>;
type ArraySliceEnd<Arr extends List, End extends Num> = Dec<End> extends NAN ? [] : PrependArray<ArrayHead<Arr>, ArraySliceEnd<ArrayRest<Arr>, Dec<End>>>;
type ArraySliceBegin<Arr extends List, Start extends Num> = Start extends 0 ? Arr : ArraySliceBegin<ArrayRest<Arr>, Dec<Start>>; 
type ArraySlice<Arr extends List, Start extends PositiveNum, End extends PositiveNum> = ArraySliceEnd<ArraySliceBegin<Arr, Start>, Sub<End, Start>>;
type Fizzbuzz<Arr extends List> = ArrEmpty<Arr> extends True ? [] : PrependArray<
  Mod<ArrayHead<Arr>, 15> extends 0
    ? "FizzBuzz"
    : Mod<ArrayHead<Arr>, 3> extends 0
      ? "Fizz"
      : Mod<ArrayHead<Arr>, 5> extends 0
        ? "Buzz"
        : ArrayHead<Arr>
, Fizzbuzz<ArrayRest<Arr>>>;
let a: Fizzbuzz<ArraySlice<Numbers, 1, 30>>;
// In most editors you can hover over 'a' to see its type
Can't wait to try this in Haskell
Too bad you don't use Church encoding to encode numbers

Last edited by TheSecondGilbert (Oct. 11, 2022 08:30:11)

Redstone1080
Scratcher
1000+ posts

What's the best code you've seen?

[removed]

Last edited by Redstone1080 (Oct. 12, 2022 00:11:00)

Powered by DjangoBB