Skip to content

Instantly share code, notes, and snippets.

View alexnoz's full-sized avatar
💡

Alex Nozdriukhin alexnoz

💡
  • Third Stone from the Sun
View GitHub Profile
@alexnoz
alexnoz / list.cpp
Last active August 30, 2018 22:01
A linked list implementation written in C++
#include <iostream>
#include <stdexcept>
#include <functional>
struct Node {
int data;
Node *next;
};
using fn = std::function<void(Node*)>;
@alexnoz
alexnoz / postfix.cpp
Last active June 7, 2018 21:17
A dead-simple postfix expression evaluator
#include <stack>
#include <string>
#include <cctype>
#include <cstdlib>
using std::stack;
using std::string;
int evaluateExpr(const char *op, stack<int> &tokens) {
int b = tokens.top(); tokens.pop();
@alexnoz
alexnoz / queue-circular.cpp
Last active June 27, 2018 20:58
A queue implementation using a circular array
#include <iostream>
#include <stdexcept>
class Queue {
private:
int length = 0;
int front = -1;
int rear = -1;
int *queue = nullptr;
@alexnoz
alexnoz / abortable.js
Created June 13, 2018 14:05
Abortable fetch helper
function getAbortable () {
let controller
return {
fetch: (url, opts) => {
controller = new AbortController()
return fetch(url, Object.assign(opts, {
signal: controller.signal
}))
@alexnoz
alexnoz / debounce-abort-fetch.js
Created June 13, 2018 20:11
Abort fetching data in a function when debouncing it
function debounceFetch (fn, delay, onCancel = () => {}) {
let timer = 0, isFetching = false
return function (...args) {
clearTimeout(timer)
if (isFetching) {
onCancel()
isFetching = false
}
@alexnoz
alexnoz / queue-linked.cpp
Created June 16, 2018 21:16
A queue implementation using linked lists
#include <iostream>
#include <stdexcept>
struct Node {
int data;
Node* next;
};
class Queue {
private:
function createElement (name, attrs, ...children) {
name = name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
const $el = document.createElement(name)
if (attrs) {
for (const [attr, val] of Object.entries(attrs))
$el.setAttribute(attr, val)
}
@alexnoz
alexnoz / rename-chunk-plugin.js
Created July 17, 2018 17:47
A webpack plugin for renaming individual chunks
const id = 'RenameChunkPlugin'
module.exports = class RenameChunkPlugin {
constructor (filenameMap) {
this.filenameMap = filenameMap
}
apply (compiler) {
compiler.hooks.thisCompilation.tap(id, compilation => {
compilation.hooks.optimizeChunkModules.tap(id, chunks => {
@alexnoz
alexnoz / mergesort.cpp
Created August 18, 2018 16:28
Mergesort c++
#include <iostream>
void merge(const int l, const int m, const int h, int* arr) {
const int len = h - l + 1;
int* const temp = new int[len];
int i = l;
int j = m + 1;
int k = 0;
@alexnoz
alexnoz / heapsort.cpp
Created August 20, 2018 10:29
Heapsort c++
#include <iostream>
void swap (int* arr, int i, int j) {
const int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void heapify (int* arr, int max, int root) {
const int left = root * 2;