Skip to content

Instantly share code, notes, and snippets.

View pmn4's full-sized avatar

Pat Newell pmn4

View GitHub Profile
@pmn4
pmn4 / server*startup*db-timing.js
Created October 30, 2018 09:36
Add Query Logging to Reaction***
// Log DB Queries
// *** Warning, this does not capture calls to the Meteor-defined
// /<Collection>/<action> methods, which don't seem to be heavily utilized in
// this project.
import { Mongo } from "meteor/mongo";
// import { MongoInternals } from "meteor/mongo";
import { Logger } from "/server/api";
export const Colors = {
@pmn4
pmn4 / Object.setPrototypeOf.js
Last active June 26, 2018 19:31
Object.setPrototypeOf(newData, sourceData) instead of Object.assign({}, sourceData, newData)
// It happens all the time: you have some data, you want to update
// a key without modifying the original object. You typically end
// cloning the original with Object.assign({}, originalObject),
// then adding your new data. This is probably pretty fast, even
// for objects with many keys, but doesn't it seem unnecessary?
function applyNewData(sourceData = { abc: 123, def: 456 }) {
const newData = { abc: 789 };
// Previously, we'd write:
@pmn4
pmn4 / min_change_input_pair.py
Created January 1, 2018 20:08
Suppose you are given a list of possible Bitcoin that you control (inputs). You need to pay someone exactly 0.71 BTC. How would you select exactly 2 inputs in such a way as to minimize the change output if you could ignore fees? Write a python function that selects the two inputs.
# simple class to contain the id and value of a transaction
class Transaction:
def __init__(self, id, value):
self.id = id
self.value = value
def __add__(self, other):
return self.value + other.value
# readable output
@pmn4
pmn4 / Complex Conditionals
Created August 23, 2017 14:34
Breaking complex conditional statements into more a readable/commentable form yields Code Climate warnings.
// to increase readability and to allow for more sane and detailed
// commenting, I prefer to break up complex conditional statements
// into separate statements in a separate function, using `return`
// to short-circuit when a condition has failed
/////////////////////////////////////////////////////////////////
// as an alternative to combining the condition and the behavior:
// ❌❌❌
function render() {
if (
@pmn4
pmn4 / 1-original-impementation.rb
Last active August 29, 2015 14:15
Modular Sinatra Refactor vs Code Complexity
class App < Sinatra::Base
get '/users' do
User.all.to_json
end
post '/users' do
User.create!(params)
204
end
@pmn4
pmn4 / gist:189aa7558aec17989c00
Created February 19, 2015 17:01
Ruby/Sinatra Refactor for Code Climate
# Original Code
class App < Sinatra::Base
get '/users' do
User.all.to_json
end
post '/users' do
User.create!(params)
204
end
@pmn4
pmn4 / gist:7e4897bbb020201fc10a
Created February 19, 2015 17:01
Ruby/Sinatra Refactor for Code Climate
# Original Code
class App < Sinatra::Base
get '/users' do
User.all.to_json
end
post '/users' do
User.create!(params)
204
end
@pmn4
pmn4 / gist:a8d9547bbf08d23d893e
Created February 19, 2015 17:01
Ruby/Sinatra Refactor for Code Climate
# Original Code
class App < Sinatra::Base
get '/users' do
User.all.to_json
end
post '/users' do
User.create!(params)
204
end
@pmn4
pmn4 / gist:1c977e32b39f5bba7432
Last active August 29, 2015 14:15
AngularJS Ordinal Filter
// convert numbers to ordinal versions of themselves
// {{ 1 | ordinal }} : "1st"
// {{ 2 | ordinal }} : "2nd"
// {{ 3 | ordinal }} : "3rd"
// etc.
angular
.module("app", [])
.filter("ordinal", function () {
var suffix = ["th", "st", "nd", "rd"];
@pmn4
pmn4 / gist:ccb5bef02743052e14e7
Last active August 29, 2015 14:13
TIL: Backbone.Collection#parse
/*
let's say you are trying to inflate a Backbone Collection
using some endpoint which returns data in non-array form.
maybe,
*/
{
meta: {
total: 25,
pageSize: 3,