Skip to content

Instantly share code, notes, and snippets.

View dukuo's full-sized avatar
🎯
Focusing

Dilip dukuo

🎯
Focusing
View GitHub Profile
@dukuo
dukuo / array_iteration_thoughts.md
Created June 16, 2020 13:07 — forked from ljharb/array_iteration_thoughts.md
Array iteration methods summarized

While attempting to explain JavaScript's reduce method on arrays, conceptually, I came up with the following - hopefully it's helpful; happy to tweak it if anyone has suggestions.

Intro

JavaScript Arrays have lots of built in methods on their prototype. Some of them mutate - ie, they change the underlying array in-place. Luckily, most of them do not - they instead return an entirely distinct array. Since arrays are conceptually a contiguous list of items, it helps code clarity and maintainability a lot to be able to operate on them in a "functional" way. (I'll also insist on referring to an array as a "list" - although in some languages, List is a native data type, in JS and this post, I'm referring to the concept. Everywhere I use the word "list" you can assume I'm talking about a JS Array) This means, to perform a single operation on the list as a whole ("atomically"), and to return a new list - thus making it much simpler to think about both the old list and the new one, what they contain, and

@dukuo
dukuo / binarySearch.js
Last active April 10, 2020 07:02
Binary Search - Javascript
const binarySearch = (arr, val) => {
let min = 0
let max = arr.length - 1
while(min <= max ) {
const mid = Math.floor((min+max)/2)
if(arr[mid] === val) {
return mid
} else if(arr[mid] < val) {
min = mid + 1
} else {
@dukuo
dukuo / sorting.js
Created April 10, 2020 02:06
Sorting Algorithms
const insertionSort = (arr) => {
for( let i = 1; i < arr.length; i++) {
const tmp = arr[i]
let j = i - 1
while( j >= 0 && arr[j] > tmp ) {
arr[j + 1] = arr[j]
i--
}
arr[j + 1] = tmp
@dukuo
dukuo / nearestSlotIn2DArray.js
Last active April 3, 2020 22:49
Find the first slot to insert a 1D array inside a 2D array
/*
@author @dukuo
Sample usage:
const size_X = 10;
const size_Y = 10;
const sampleArray = new Array(size_X).fill(new Array(size_Y).fill(null));
// Fill array with random occurrence of values or null.
for(var j = 0; j < size_Y; j++) {
@dukuo
dukuo / faceTimer.js
Last active March 9, 2020 23:40
Spark Face Tracker Timer
// padStart polyfill
if (!String.prototype.padStart) {
String.prototype.padStart = function (n,str){
return Array(n-String(this).length+1).join(str||'0')+this;
}
}
const states = {
PAUSE: 1,
@dukuo
dukuo / kb-config-ctx.sh
Last active November 5, 2019 12:24
Kubernetes Context Configuration command line utility
#!/bin/bash
############################################################################################
#
# MIT License
#
# Copyright (c) 2019 Dilip J. Ramírez (https://github.com/dukuo)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
@dukuo
dukuo / permissions.ts
Created June 12, 2018 06:50
GraphQL Shield Rules class
import { shield, and, or, not } from 'graphql-shield'
import {rules} from './rules'
export const permissions = shield({
Mutation: {
createResource: and( rules.isAuthenticated(), rules.hasRoles(["ADMIN", "USER"]), rules.hasScopes(["admin:god", "create:resource"]) ),
deleteResource: and( rules.isAuthenticated(), or( rules.hasRole("ADMIN"), rules.hasRole("EDITOR") ) )
}
}, {
debug:true