Skip to content

Instantly share code, notes, and snippets.

@harbirchahal
harbirchahal / array_generators_util.js
Last active February 26, 2020 18:18
Implementing array functions as generators
const log = console.log;
function map(fn) {
return function* (arr) {
for (const el of arr) {
yield fn(el);
}
}
}
@harbirchahal
harbirchahal / my_array.js
Last active February 26, 2020 18:19
Naive native array implementation in JavaScript
/**
* as (typeof []) is "object"
*/
function MyArray() {
const array = Object.create(MyArray.prototype);
Object.defineProperty(array, 'length', {
value: 0,
enumerable: false,
writable: true,
});
@harbirchahal
harbirchahal / event-bubble.component.html
Last active April 20, 2021 18:20
Event Bubbling in Angular
<div (click)="handleAction($event)">
<div *ngFor="let item of listing">
<div>{{ item.label }}</div>
<div>
<button [attr.data-id]="item.id"
data-action="modify">
Modify
</button>
<button [attr.data-id]="item.id"
data-action="delete">
@harbirchahal
harbirchahal / heapify.py
Last active April 26, 2021 13:27
Algo: Heap -> Heapify
def heapify(array):
last_index = len(array) - 1
for index in range(last_index, -1, -1):
bubble_down(array, index)
def bubble_down(array, index):
index_limit = len(array) - 1
lchild_index = (index + 1) * 2 - 1
rchild_index = lchild_index + 1
@harbirchahal
harbirchahal / merge_sort.py
Last active April 28, 2021 10:33
Algo: Merge Sort
def merge_two(arr1, arr2):
merged = []
arr1_index = 0
arr2_index = 0
arr1_size = len(arr1)
arr2_size = len(arr2)
while (arr1_index < arr1_size and arr2_index < arr2_size):
if (arr1[arr1_index] < arr2[arr2_index]):
@harbirchahal
harbirchahal / quick_sort.py
Created April 28, 2021 10:36
Algo: Quick Sort
def partition (array, start, end):
pivot = array[start]
low = start + 1
high = end
while (low <= high):
while (low <= end and array[low] <= pivot):
low += 1
while (high > start and array[high] > pivot):
high -= 1
@harbirchahal
harbirchahal / buffer_iterator.js
Created May 17, 2021 08:11
ES6: Buffer Iterator
function BufferIterator(collection, bufferSize) {
this[Symbol.iterator] = function () {
let nextIndex = 0;
return {
next: () => {
if (nextIndex < collection.length) {
const buffer = new Array(bufferSize);
for (let i = 0; i < bufferSize; i++) {
buffer[i] = collection[nextIndex++];
@harbirchahal
harbirchahal / try.js
Last active May 22, 2021 14:06
Try functional data type
class Try {
constructor(val) {
this._val = val;
}
static of(fn) {
try {
return new Success(fn());
} catch (err) {
return new Failure(err);
@harbirchahal
harbirchahal / array_fp_util.js
Created June 22, 2021 11:31
Composable array functions that avoid intermediate creation of resultant array.
/*
Ref: https://monet.github.io/monet.js/
*/
const { Some, None } = Monet;
function map(fn) {
return function (v) {
return Some(fn(v));
}
}
@harbirchahal
harbirchahal / single_linked_list.js
Last active May 2, 2022 10:26
Single LL Operations
/**
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
function length(head) {
if (head) {
let count = 1;