This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const log = console.log; | |
function map(fn) { | |
return function* (arr) { | |
for (const el of arr) { | |
yield fn(el); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* as (typeof []) is "object" | |
*/ | |
function MyArray() { | |
const array = Object.create(MyArray.prototype); | |
Object.defineProperty(array, 'length', { | |
value: 0, | |
enumerable: false, | |
writable: true, | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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"> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Try { | |
constructor(val) { | |
this._val = val; | |
} | |
static of(fn) { | |
try { | |
return new Success(fn()); | |
} catch (err) { | |
return new Failure(err); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Ref: https://monet.github.io/monet.js/ | |
*/ | |
const { Some, None } = Monet; | |
function map(fn) { | |
return function (v) { | |
return Some(fn(v)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* function ListNode(val, next) { | |
* this.val = (val===undefined ? 0 : val) | |
* this.next = (next===undefined ? null : next) | |
* } | |
*/ | |
function length(head) { | |
if (head) { | |
let count = 1; |