Skip to content

Instantly share code, notes, and snippets.

@ryancole
ryancole / fibonacci.js
Created September 25, 2014 03:29
fibonacci
function fibonacci (n) {
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
};
var fibonacci_memo = (function () {
var memo = [0,1];
function fib (n) {
var result = memo[n];
if (typeof result !== 'number') {
@ryancole
ryancole / foo.js
Created September 25, 2014 03:12
array-based stack and queue, without using built in push pop
function stack () {
var content = [];
return {
push: function (v) {
content[content.length] = v;
},
pop: function () {
var result = content.splice(-1, 1);
if (result.length === 1) {
@ryancole
ryancole / codecombat.js
Created June 19, 2014 20:19
gold rush mode human
// get all items in this frame split into regions
function getRegionItems (items) {
var result = [];
items.forEach(function (item) {
var x = Math.floor(item.pos.x / 28);
var y = Math.floor(item.pos.y / 28);
var region = (x * 4) + y;
if (result[region] === undefined) {
result[region] = [item];
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Content Include="App.config">
<SubType>Designer</SubType>
</Content>
<Content Include="App.Base.config">
<DependentUpon>App.config</DependentUpon>
</Content>
<Content Include="App.Debug.config">
(defn append-user-data [handler]
(fn [request]
(clutch/with-db database-address
(let [access-token (get-in request [:headers "token"])
view-data (clutch/get-view "api" "users-by-token" {:keys [access-token]})]
(if (= 1 (count view-data))
(handler (assoc request :api-user (:value (first view-data))))
{:status 403
:body "Invalid access token"})))))
@ryancole
ryancole / foo.sql
Last active December 11, 2015 20:59
get a count of items for each user, accounting for duplicates and nulls / empties
SELECT v.custodian_id, COUNT(DISTINCT NVL(LOWER(TRIM(v.serial_numberev)), v.evidence_id)) AS esi_count
FROM evidence_to_custodian e
LEFT OUTER JOIN evidence_main v ON v.evidence_id = e.evidence_id
WHERE v.purged != 1 AND LOWER(v.source) = 'ac' AND v.inventory_class = 1 AND LOWER(v.source_mediaev) = 'hdd' AND e.custodian_id IN (:custodians)
GROUP BY v.custodian_id
@ryancole
ryancole / foreign_key_count.sql
Created January 9, 2013 17:58
Query for records with more than X number of foreign key matches.
SELECT t.emplid
FROM term_file t
WHERE t.emplid IN (SELECT v.custodian_id FROM evidence_main v GROUP BY v.custodian_id HAVING COUNT(v.custodian_id) > 50)
#include <node.h>
#include <v8.h>
#ifndef UNIX
#define UNIX
#define UNIX_64
#endif
#include <scctype.h>
#include <sccex.h>
@ryancole
ryancole / TitleField.h
Created August 14, 2012 02:27
A subclass of `UITextField` that has a prefixed label.
//
// TitleField.h
// mustached-bear
//
// Created by Ryan Cole on 8/12/12.
// Copyright (c) 2012 Ryan Cole. All rights reserved.
//
#import <UIKit/UIKit.h>
@ryancole
ryancole / foo.m
Created August 7, 2012 01:06
Convert NSPredicate into NSDictionary for use as a query string in an AFNetworking HTTP request
- (NSURLRequest *)requestForFetchRequest:(NSFetchRequest *)fetchRequest
withContext:(NSManagedObjectContext *)context {
// init the query string dictionary
NSMutableDictionary *queryString = nil;
// if we're given a predicate, convert it to a dictionary
if (fetchRequest.predicate) {
if ([fetchRequest.predicate isKindOfClass:[NSCompoundPredicate class]]) {