Skip to content

Instantly share code, notes, and snippets.

View LapysDev's full-sized avatar
🌙
Reinventing the wheel…

Funto Oshodi LapysDev

🌙
Reinventing the wheel…
View GitHub Profile
@LapysDev
LapysDev / power
Created June 2, 2024 11:59
📈 Exponentiation: Power of integers using an alternative divide-and-conquer algorithm
ipow(base, exponent) {
var power = 1;
// …
for (count = 1, multipliers = list(base); exponent; ) {
var multiplier = multipliers[multipliers.length - 1];
// …
if (count != exponent) {
count *= 2;
@LapysDev
LapysDev / nil.hpp
Last active April 19, 2023 06:07
0️⃣ Null: C++ null metafunction/ trait
#include <cstdlib>
/* ... */
template <typename base>
struct nil final {
private:
template <typename subbase, typename = subbase>
struct valueof final {
friend struct nil;
@LapysDev
LapysDev / lambda.hpp
Last active April 19, 2023 06:05
📦 Lambda: C++ shorthand syntax for creating lambda functors
/* Pending updates… */
#include <cstddef>
#include <type_traits>
#include <utility>
#include <version>
#ifdef __clang__
# pragma clang diagnostic ignored "-Wdollar-in-identifier-extension"
#endif
@LapysDev
LapysDev / properties.hpp
Last active April 19, 2023 06:05
⚙️ Properties: C++ accessor/ mutator
/* Pending updates… */
#include <cstdarg>
#include <type_traits>
#include <utility>
/* ... */
namespace {
struct halt final {
constexpr inline halt(...) noexcept = delete;
};
@LapysDev
LapysDev / reflenum.cpp
Last active April 19, 2023 06:11
🪞 Reflective Enumeration: A take on statically reflective C++ enums.
#include <cstdio>
/* ... */
#define concatenate(argument1, argument2) argument1 ## argument2
#define count(...) ( \
count_arity(__VA_ARGS__, 255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 237, 236, 235, 234, 233, 232, 231, 230, 229, 228, 227, 226, 225, 224, 223, 222, 221, 220, 219, 218, 217, 216, 215, 214, 213, 212, 211, 210, 209, 208, 207, 206, 205, 204, 203, 202, 201, 200, 199, 198, 197, 196, 195, 194, 193, 192, 191, 190, 189, 188, 187, 186, 185, 184, 183, 182, 181, 180, 179, 178, 177, 176, 175, 174, 173, 172, 171, 170, 169, 168, 167, 166, 165, 164, 163, 162, 161, 160, 159, 158, 157, 156, 155, 154, 153, 152, 151, 150, 149, 148, 147, 146, 145, 144, 143, 142, 141, 140, 139, 138, 137, 136, 135, 134, 133, 132, 131, 130, 129, 128, 127, 126, 125, 124, 123, 122, 121, 120, 119, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75
@LapysDev
LapysDev / define.js
Last active April 19, 2023 06:12
🦆 Typed Property Definition: Defines type-enforced properties on JavaScript objects
Object.defineProperty(Object.prototype, "def", (function() {
// ...
var UNINITIALIZED = {};
// ...
function assert(identifier, value, constraints) {
for (var iterator = constraints.length - 1; ~iterator; --iterator)
switch (constraints[iterator]) {
case null: if (null === value) return; break;
case "bigint": if ("bigint" === typeof value) return; break;
@LapysDev
LapysDev / gradient-search.cpp
Last active April 19, 2023 06:04
🔍 Gradient Search: Algorithm to search through an array from multiple evenly spread points (or gradient stops) simultaneously
#include <cstddef>
/* ... --> search<𝓍>(…) */
template <std::size_t count, typename type>
bool search(type const& key, type const array[], std::size_t length) noexcept {
union {
type const *stop; // ->> for elements missed via `0u != count % length`
type const *stops[count];
};
@LapysDev
LapysDev / line-to-coordinates
Last active April 19, 2023 06:12
〰️ Line-to-Coordinates: Algorithm to draw a line between two coordinates
draw_line(x1, x2, y1, y2) {
x = 0, y = 0; // where each pixel of the line is drawn
xLength = abs(x1 - x2), yLength = abs(y1 - y2);
xRatio = 1, yRatio = 1; // Asserts how the line is weighted between the x and y axis
if (xLength > yLength) yRatio = yLength / xLength;
else if (xLength < yLength) xRatio = xLength / yLength;
// draw the line
xIterator = 0; // for progressing through the iteration
@LapysDev
LapysDev / line-at-angle
Last active April 19, 2023 06:12
〰️ Line-at-Angle: Algorithm to draw a unit length at angle angle
draw_line(angle, length) {
x = 0, y = 0;
xRatio, yRatio; // Asserts how the line is weighted between the x and y axis
xRate = 0, yRate = 0; // Sets up the ratios
// Set up the rates
{
iterator = 0;
for (; iterator <= angle; ++iterator) {
if (iterator >= 0 and iterator < 90) { ++xRate; ++yRate; }
@LapysDev
LapysDev / robot.js
Last active April 19, 2023 06:12
🤖 Robot: Translates ambiguous sequences of text (a custom “language”) into another sequence of text (another custom “language”)
// The code should be compatible with IE5, expect some bugs in jank JavaScript versions though...
(function RobotAPI(global) {
// The class of a Robot
function Robot(name, language, dictionary) {
// Default arguments not in IE, sorry for the oncoming code clutter... -_-
if (arguments.length < 3) {
dictionary = new RobotDictionary(Robot.getTestDictionary());
if (arguments.length < 2) {
language = Robot.getTestLanguage().filter(Boolean);