Skip to content

Instantly share code, notes, and snippets.

View 0mega28's full-sized avatar
🤯

Saksham Gupta 0mega28

🤯
View GitHub Profile
@0mega28
0mega28 / CancellableFunction.js
Created March 11, 2026 14:41
[LeetCode 2650] Design Cancellable Function
/**
* @param {Generator} generator
* @return {[Function, Promise]}
*/
const CANCELLED = "Cancelled";
var cancellable = function (generator) {
const {
promise : resultPromise,
resolve : resolveResultPromise,
reject : rejectResultPromise
@0mega28
0mega28 / my_variant.hpp
Created January 23, 2026 12:21
C++ Variant implementation with variadic types
#pragma once
#include <algorithm>
#include <utility>
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////// Type Index /////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename T, typename... Types>
struct type_index;
@0mega28
0mega28 / my_variant_two_types.hpp
Last active January 23, 2026 12:21
Simple C++ 17 Variant Implementation with two types
#pragma once
#include <cstddef>
#include <utility>
template<typename T1, typename T2>
class My_Variant {
public:
explicit My_Variant(const T1 &value) : index_(0) {
new(&value_.t1) T1(value);
}
@0mega28
0mega28 / generator.cpp
Created January 21, 2026 09:18
Naive Generator Co-Routine Implementation (from https://www.youtube.com/watch?v=lm10Cj-HNKQ)
#include <coroutine>
#include <iostream>
#include <print>
#include <thread>
template<typename T>
struct Generator {
struct promise_type;
explicit Generator(promise_type &promise) : handle(std::coroutine_handle<promise_type>::from_promise(promise)) {
@0mega28
0mega28 / MyPromise.js
Last active May 9, 2024 18:39
Simple Promise Polyfill Implementation
const MyPromise = function (executor) {
const PENDING = "Pending";
const RESOLVED = "Resolved";
const REJECTED = "Rejected";
this.state = PENDING
this.val = null;
this.err = null;
const successPromisesCb = [];
const failPromisesCb = [];
this.then = (successCallback) => {
@0mega28
0mega28 / DivideMonad.java
Created May 8, 2023 16:32
Monad implementation for dividing two numbers represented in comma separated String
public class DivideMonad {
static Optional<List<String>> split(String commaSeparatedString) {
final Long commaCount = commaSeparatedString.chars().filter(ch -> (ch == ',')).count();
if (commaCount != 1) {
return Optional.empty();
}
String[] splitString = commaSeparatedString.split(",");
return Optional.of(List.of(splitString[0], splitString[1]));
}
@0mega28
0mega28 / typehack.js
Last active May 8, 2023 16:29
typeracer hack
async function hackTypercer(wpm) {
const textSelector =".inputPanel span";
const timerSelector = ".countdownPopup";
const inputSelector = "input";
if (!document.querySelector(timerSelector)) {
return;
}
while (document.querySelector(timerSelector) !== null) {