Skip to content

Instantly share code, notes, and snippets.

View simon-tiger's full-sized avatar

Simon Tiger simon-tiger

View GitHub Profile
// Super simple pathfinding library
class Node {
constructor(userData=null) {
this.connected = [];
this.userData = userData;
}
connect(node, weight=1, oneWay=false) {
this.connected.push({ node, weight });

Generator Functions

What are generator functions?

Generator functions are basically functions that you can pause at some point. You can define a generator function with function*:

function* Generator() {
  // code goes here
}

I like to named my generator functions with a capital, for reasons that will become apparent later, but generally they are named with a lowercase letter.

  • map => Takes an array, and then applies a function to every element of that array.
  • filter => Takes an array, and passes every element through a function. It only keeps the element if the function returns true.
  • sort => By default, it sorts the elements of an array in alphabetical order. But, if you give it a function, it will use it to sort the array in the following way:
    • The function takes two arguments to compare, a and b.
    • The function returns a negative number if a is lower than b.
    • The function returns zero if a has the same value as b.
    • The function returns a positive number is a is higher than b.
  • reduce => Starts with an accumulator. It starts with the first element of the array. Then it goes through the rest of the elements, and for each one, it applies a function to the accumulator and the element and puts the result back into the accumulator. The function returns what the accumulator is at the end.

Examples

This is an array of configurations. Each configuration is another array. The numbers are the vertices of the cube. Every triplet of numbers represents a triangle.
{
{},
{0, 8, 3},
{0, 1, 9},
{1, 8, 3, 9, 8, 1},
{1, 2, 10},
{0, 8, 3, 1, 2, 10},
{9, 2, 10, 0, 2, 9},
const Discord = require("discord.js");
const client = new Discord.Client();
const fs = require("fs");
require("dotenv").config();
const prefix = "!";
const commandFiles = fs.readdirSync("commands", "utf8");
client.commands = new Discord.Collection();

Number Balance Puzzle

The puzzle starts by defining a "number balance". It's a balance, where you put numbers on both sides. You start on the left side by counting up from 1, and then stopping somewhere. So, in this example, we have 1, 2, 3, 4, 5 and we stop there. Keeping going, we draw the next next number (6 in this case) in the base of the balance. We call this number the "middle number" of the balance. We then keep counting on the right side, so that the balance, well, balances. In this case, 7 & 8 will work.

Example Number Balance

The Puzzle: Is this example the only possible number balance? If not, what are the others, and what is the pattern behind them?

// If you don't like to go into callback hell and have lots of nested callbacks, here's a neat little program that will turn any function that takes a callback into a promise.
// NOTE: This assumes that the callback is the last argument
function promisify(func) {
return (...args) => {
return new Promise((resolve, reject) => func(...args, resolve));
}
}
function promisifyWithError(func) {
// First you have to install NeDB. For this, you can just use `npm install nedb`.
// To create a database, do this:
const Datastore = require("nedb");
const db = new Datastore("database.db");
// Then, to query it, do this:
db.find({ property: requiredValue }, (err, results) => {
// results will hold the result
});
$red: #FF0000
$green: #00FF00
$blue: #0000FF
$black: #000000
$white: #FFFFFF
$gray: #808080
$cyan: #00FFFF
$magenta: #FF00FF
$yellow: #FFFF00
$brown: #996633