Skip to content

Instantly share code, notes, and snippets.

View jdcrensh's full-sized avatar

Jon Crenshaw jdcrensh

View GitHub Profile
@jdcrensh
jdcrensh / gist:459508
Created July 1, 2010 03:11
Extends UIColor for hex color values
@interface UIColor (Hex)
+ (UIColor *)colorWithHex:(uint)rgbValue;
@end
@implementation UIColor (Hex)
+ (UIColor *)colorWithHex:(uint)rgbValue {
float redValue = ((rgbValue & 0xFF0000) >> 16) / 255.0;
@jdcrensh
jdcrensh / gist:558549
Created August 31, 2010 04:34
Produces UIColor snippet from any given 6-char hex color value
int main (int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
NSString *hexValue = [args stringForKey:@"hex"];
if (hexValue && [hexValue length] == 6) {
NSString *hexPart;
float red, green, blue;
for (int i=0; i<[hexValue length]; i++) {
@jdcrensh
jdcrensh / collatz.py
Last active September 5, 2015 22:24
Collatz conjecture
#!/usr/bin/python
def collatz(n,i):
while n > 1:
n = n % 2 == 0 and n / 2 or n * 3 + 1
i += 1
return i
def main():
n = 1 # starting number
@jdcrensh
jdcrensh / replicate.py
Last active May 2, 2018 14:20
Self-replicating python script
#!/usr/bin/python
import os, sys, time, uuid
# get self code
self_content = file(sys.argv[0]).read()
while True:
# wait 10 seconds
time.sleep(10)
@jdcrensh
jdcrensh / gist:1547390
Created January 1, 2012 13:54
Find extensions in directory and print their total line count
find src -type f \( -name '*.java' -o -name '*.jspx' \) \
-exec wc -l {} \; | awk '{sum+=$1} END {print sum}'
@jdcrensh
jdcrensh / Base62.java
Last active January 28, 2024 12:25
Class to encode a string into base 62 (character set [a-zA-Z0-9]) with Java. A common use case is URL shortening.
public class Base62 {
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
public static final int BASE = ALPHABET.length();
private Base62() {}
public static String fromBase10(int i) {
StringBuilder sb = new StringBuilder("");
@jdcrensh
jdcrensh / example.js
Created May 2, 2016 23:21
Using SweetAlert2 with Webpack
var sweetAlert = require('sweetalert2');
sweetAlert('Hello World!', 'It works!', 'success');
@jdcrensh
jdcrensh / StringBuilding.cls
Created May 6, 2016 17:51
Profiling of String concatenation and the StringBuilder pattern in Apex (Salesforce)
/**
* Compares String concatenation performance vs StringBuilder.
* At around 21000 iterations, actual time spent is nearly identical. At higher iterations,
* StringBuilder comes out ahead in less actual time spent and with lower CPU usage. However in
* lower iterations, concatenation wins both measurements.
*
* Another consideration is that the StringBuilder pattern does not seem to affect the heap
* size limit at all, which could be a bug with Apex profiling.
*/
@jdcrensh
jdcrensh / zipdir.js
Created May 27, 2016 23:06
[node.js] Archive (zip) files within directory
const streamBuffers = require('stream-buffers');
const archiver = require('archiver');
const zipDir = (dir) => {
const output = new streamBuffers.WritableStreamBuffer({
initialSize: (100 * 1024), // start at 100 kilobytes.
incrementAmount: (10 * 1024), // grow by 10 kilobytes each time buffer overflows.);
});
const archive = archiver('zip');
archive.on('end', () => {
@jdcrensh
jdcrensh / LimitProfilingCallout.cls
Last active July 21, 2016 00:07
[apex] Same-instance callouts provide separate transactions
@RestResource(urlMapping = '/LimitProfiling/*')
global with sharing class LimitProfilingCallout {
@HttpPost
global static void callMeMaybe() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
Map<String, Object> data = (Map<String, Object>) JSON.deserializeUntyped(req.requestBody.toString());