Skip to content

Instantly share code, notes, and snippets.

View Filename extension list
.php
.html
.txt
.htm
.aspx
.asp
.js
.css
.pgsql.txt
.mysql.txt
@cowboy
cowboy / bind-vs-variable.js
Created January 28, 2015 21:10
JavaScript: Why use an explicit variable when you can just bind and use this instead?
View bind-vs-variable.js
// Why use an explicit variable...
function lame() {
var args = arguments;
return function(fn) {
console.group.apply(console, args);
fn();
console.groupEnd();
};
}
@cowboy
cowboy / why.js
Last active June 4, 2023 05:30
JavaScript: why do we write async code the way we do?
View why.js
// This is an UBER simplified example, but I hope you get the idea...
function thisIsHowWeWriteSyncCode(arg) {
var foo = doSomething(arg);
var bar = doSomethingElse(foo);
var baz = doSomethingWith("test", bar, 123);
return doLastThing(baz);
}
function soThisSeemsSensibleForAsyncCode(arg) {
@cowboy
cowboy / delayify.js
Created June 11, 2015 12:19
JavaScript: Does My Async Abstraction Work? Per https://twitter.com/ryanflorence/status/608841603956903937
View delayify.js
Function.prototype.doesMyAsyncAbstractionWork = function() {
var fn = this;
var args = arguments;
setTimeout(function() {
fn.apply(null, args);
}, Math.random() * 5000);
};
@cowboy
cowboy / promise-batch-arrays.js
Created June 22, 2016 16:18
javascript / bluebird: promise batch arrays
View promise-batch-arrays.js
import Promise from 'bluebird';
function getBatches(arr, length) {
let i = 0;
const result = [];
while (i < arr.length) {
result.push(arr.slice(i, i + length));
i += length;
}
return result;
@cowboy
cowboy / objects-vs-tuples.js
Last active June 4, 2023 05:29
javascript / es2015: should functions returning multiple, separate values return an object or tuple (array)?
View objects-vs-tuples.js
// This function returns 2 separate values via an object with 2 properties.
function returnTwoValuesObj(str) {
const length = str.length;
const hasSpaces = str.indexOf(' ') !== -1;
return {length, hasSpaces};
}
// This function returns 2 separate values via an array (effectively a tuple) with 2 items.
function returnTwoValuesTuple(str) {
const length = str.length;
@cowboy
cowboy / mixin.js
Last active June 4, 2023 05:29
JavaScript ES6 - mixins with super
View mixin.js
// This mixin might be used to extend a class with or without its
// own "foo" method
const mixin = Base => class extends Base {
foo() {
// Only call super.foo() if it exists!
if (super.foo) {
super.foo();
}
console.log('mixin');
@shahnCM
shahnCM / GoLangCrash.md
Created June 1, 2023 21:30
Some GoLang programs to get you started
View GoLangCrash.md
package main

import (
	"fmt"
	"math"
	"strings"
)

func main() {
@cowboy
cowboy / paperclips.js
Last active June 4, 2023 05:29
Universal Paperclips: stuff to paste into console http://www.decisionproblem.com/paperclips
View paperclips.js
_toggles = {}
makeToggle = (id, fn, delay = 250) => {
const elem = document.querySelector('#' + id)
elem.onclick = () => {
if (_toggles[id]) {
clearInterval(_toggles[id])
_toggles[id] = null
} else {
_toggles[id] = setInterval(() => elem.disabled || fn(), delay)
}
@jackygu2006
jackygu2006 / ordinals...Inscription.sol
Created May 31, 2023 09:50
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.18+commit.87f61d96.js&optimize=false&runs=200&gist=
View ordinals...Inscription.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "./Logarithm.sol";
import "./TransferHelper.sol";
// This is common token interface, get balance of owner's token by ERC20/ERC721/ERC1155.
interface ICommonToken {
function balanceOf(address owner) external returns(uint256);