Skip to content

Instantly share code, notes, and snippets.

@calvinfroedge
calvinfroedge / f.py
Created January 25, 2012 01:14
Fibonacci implementation (python) - Can you find a more efficient way?
#Using a loop:
def f(n):
if n == 1 or n == 0 : return n
else:
x = 0
y = 1
r = 0
for i in range(n - 1):
@calvinfroedge
calvinfroedge / sort.py
Created January 25, 2012 04:28
List sort: Python
#using list functions
def sort(a):
i = 1
while(i < len(a)):
if i < 1: i = i + 1
else:
p = i - 1
if a[i] < a[p]:
@calvinfroedge
calvinfroedge / payments static interface
Created January 28, 2012 18:30
payments static interface
/**
* This method is here so the class can be called statically per FuelPHP standards, without dramatically changing the approach for the entire library. This is to make support (since this is a multi-framework payments package) and documentation easier.
*/
public static function __callStatic($method, $params)
{
$method = $method;
$gateway = $params[0];
$params = $params[1];
$payments = new self();
@calvinfroedge
calvinfroedge / gist:1696760
Created January 29, 2012 02:05
Multiple interfaces (forge / instance)
<?php
abstract class Fuel_Payments
{
//Array for storing class instances
private static $instances = array();
/**
* Create a new instance
**/
//This technique was invented by Kai Jaeger (http://kaijaeger.com/articles/the-singleton-design-pattern-in-javascript.html), I'm just showing an implementation
var Singleton = (function()
{
var instance = false;
/*
* Because this is in a closure, it cannot be accessed
*/
var member = 'test';
@calvinfroedge
calvinfroedge / bitwise.c
Created February 20, 2012 02:53
Bitwise = mind blown
#include <stdio.h>
int printSomething()
{
printf("%s", "Something");
return 1;
}
int main()
{
@calvinfroedge
calvinfroedge / XOR
Created February 20, 2012 03:07
XOR
#include "stdio.h"
int main()
{
int x = 1, y = 2, z = 3;
if(y > z ^ x > y ^ x > 0) printf("%s \n \n", "True!"); //Only one condition is true, statement evaluates to boolean true
if(z > y ^ x > 0) printf("%s \n \n", "Also True!"); //Both conditions are true. Statement evaluates to boolean false and "Also True!" is not printed
@calvinfroedge
calvinfroedge / shift.c
Created February 20, 2012 03:24
Shift operators
#include "stdio.h"
int main()
{
int x, z, y = 2, a = 3, r;
x = y << 2; //Binary operation. Bits are shifted two places to the left (001 becomes 100). This evalutes to 8
printf("%d \n \n", x);
@calvinfroedge
calvinfroedge / chartoint.c
Created February 20, 2012 03:49
Char to int conversion
#include "stdio.h"
int main()
{
char c = 'a';
int d, e;
d = c - 'a';
@calvinfroedge
calvinfroedge / dependencyresolver.php
Last active December 26, 2015 15:39
Dependency resolver I wired up that recursively injects dependencies at run time. Explanation and examples included!
<?php
/*
* Dependency injection can help you reduce coupling and increase testability, making your applications more maintainable.
*
* DependencyResolver gives you the best of both worlds by mixing a registry with call time dependency resolution. In addition, it provides significant resource
* usage advantages over other patterns, such as an object pool, because dependencies are only resolved when they are actually needed.
*
* It supports both constructor injection for class wide dependencies, as well as method injection for method specific dependencies.
*