Skip to content

Instantly share code, notes, and snippets.

"A beginning programmer writes her programs like an ant builds her hill, one piece at a time, without thought for the bigger structure. Her programs will be like loose sand. They may stand for a while, but growing too big they fall apart.

Realizing this problem, the programmer will start to spend a lot of time thinking about structure. Her programs will be rigidly structured, like rock sculptures. They are solid, but when they must change, violence must be done to them.

The master programmer knows when to apply structure and when to leave things in their simple form. Her programs are like clay, solid yet malleable."

-- Master Yuan-Ma, The Book of Programming

@guyellis
guyellis / renameMongoField.js
Last active November 30, 2021 23:36
How to rename a field in all documents in a collection in MongoDB
// Params
// 1. {} add a query in here if you don't want to select all records (unlikely)
// 2. $rename one or more fields by setting the keys of the object to the old field name and their values to the new field name.
// 3. Set multi to true to force it to operate on all documents otherwise it will only operate on the first document that matches the query.
//
// Run this in the MongoDB shell.
db.<collectionName>.update( {}, { $rename: { '<oldName1>': '<newName1>', '<oldName2>': '<newName2>' } }, {multi: true} )
@jremmen
jremmen / mm.js
Last active May 7, 2024 01:39
js: matrix multiplication using dot product and transposition
mmultiply = function(a,b) {
return a.map(function(x,i) {
return transpose(b).map(function(y,k) {
return dotproduct(x, y)
});
});
}
dotproduct = function(a,b) {
return a.map(function(x,i) {
@CrowderSoup
CrowderSoup / find_replace.js
Created February 19, 2014 16:40
Replace all instances of a substring without Regex in JavaScript
var str = 'This is a test string.';
// Let's replace all spaces with ','
str = str.split(' ').join(',');
@mikedugan
mikedugan / sky_gradients.css
Created November 7, 2013 14:27
various sky color gradients
.sky-gradient-00, .sky-gradient-24 { background: #00000c; }
.sky-gradient-01 { background: linear-gradient(to bottom, #020111 85%,#191621 100%); }
.sky-gradient-02 { background: linear-gradient(to bottom, #020111 60%,#20202c 100%); }
.sky-gradient-03 { background: linear-gradient(to bottom, #020111 10%,#3a3a52 100%); }
.sky-gradient-04 { background: linear-gradient(to bottom, #20202c 0%,#515175 100%); }
.sky-gradient-05 { background: linear-gradient(to bottom, #40405c 0%,#6f71aa 80%,#8a76ab 100%); }
.sky-gradient-06 { background: linear-gradient(to bottom, #4a4969 0%,#7072ab 50%,#cd82a0 100%); }
.sky-gradient-07 { background: linear-gradient(to bottom, #757abf 0%,#8583be 60%,#eab0d1 100%); }
.sky-gradient-08 { background: linear-gradient(to bottom, #82addb 0%,#ebb2b1 100%); }
.sky-gradient-09 { background: linear-gradient(to bottom, #94c5f8 1%,#a6e6ff 70%,#b1b5ea 100%); }
//JS QuickSort
Array.prototype.quickSort = function() {
var r = this;
if(this.length <= 1) {
return this;
}
var less = [], greater = [];
@luoxiaoxun
luoxiaoxun / LeetCode-Multiply Strings
Created June 29, 2013 02:53
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative.
C++:
class Solution {
public:
string multiply(string num1, string num2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(num1.size()==0||num2.size()==0) return "";
string ret(num1.size()+num2.size()+1,'0');
reverse(num1.begin(),num1.end());
reverse(num2.begin(),num2.end());
@Ocramius
Ocramius / User.php
Last active February 16, 2024 14:57
Doctrine 2 ManyToMany - the correct way
<?php
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity()
* @ORM\Table(name="user")
*/
class User
@aheckmann
aheckmann / storeImgInMongoWithMongoose.js
Created April 17, 2012 19:14
store/display an image in mongodb using mongoose/express
/**
* Module dependencies
*/
var express = require('express');
var fs = require('fs');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// img path