Skip to content

Instantly share code, notes, and snippets.

View rcdilorenzo's full-sized avatar
🙌
Working in His Kingdom (Col 3:17)

Christian Di Lorenzo rcdilorenzo

🙌
Working in His Kingdom (Col 3:17)
View GitHub Profile
defmodule Standard do
def fib(0), do: 0
def fib(1), do: 1
def fib(n), do: fib(n-1) + fib(n-2)
end
defmodule Optimized do
def fib(0), do: 0
def fib(1), do: 1
def fib(1, current, _), do: current
@rcdilorenzo
rcdilorenzo / Gulpfile.js
Created April 10, 2015 01:00
An extremely readable yet powerful gulpfile that converts an angular app to dist folder that can be served as static assets in production while having sourcemaps and live reloading in the development environment. This gulpfile was a conversion from a stock grunt yeoman config.
var gulp = require('gulp');
var concat = require('gulp-concat');
var print = require('gulp-print');
var bowerFiles = require('main-bower-files');
var filter = require('gulp-filter');
var sass = require('gulp-sass');
var flatten = require('gulp-flatten');
var jsFilter = filter('**/*.js');
var cssFilter = filter('**/*.css');
@rcdilorenzo
rcdilorenzo / macro_fun.exs
Created January 30, 2015 03:27
Macro fun in Elixir mimicking Ruby's attr_accessor
defmodule MacroExp do
defmacro attr_accessor(atom) do
getter = String.to_atom("get_#{atom}")
setter = String.to_atom("set_#{atom}")
quote do
def unquote(getter)(data) do
data |> Map.from_struct |> Map.get(unquote(atom))
end
def unquote(setter)(data, value) do
data |> Map.put(unquote(atom), value)
import Foundation
extension Int {
func timesMap(closure: (Int) -> AnyObject) -> Array<AnyObject> {
var array: AnyObject[] = []
for x in 1...self {
array += closure(x)
}
return array;
}
defmodule Controller.Main do
use Weber.Controller
render_when_raise :unauthorized, {:text, 401, "Unauthorized.", []}
layout false
def action(_, conn) do
{:render, [project: "simpleTodo"], []}
@rcdilorenzo
rcdilorenzo / json_map_builder.ex
Last active October 25, 2021 08:19
Convert a elixir json-decoded object to a map no matter how deep
defmodule JSONMapBuilder do
def to_map(list) when is_list(list) and length(list) > 0 do
case list |> List.first do
{_, _} ->
Enum.reduce(list, %{}, fn(tuple, acc) ->
{key, value} = tuple
Map.put(acc, binary_to_atom(key), to_map(value))
end)
_ ->
list
@rcdilorenzo
rcdilorenzo / CentroidCalculator.m
Created February 1, 2014 18:40
Simple Obj-C function to calculate centroid of points
CGPoint centroidOfPoints(const CGPoint* points, int pointCount) {
CGPoint centroid = CGPointMake(0, 0);
double x0, y0, x1, y1, partialArea;
double signedArea = 0.0;
for (int index = 0; index < pointCount; index++) {
int nextIndex = (index == pointCount-1) ? 0 : index+1;
x0 = points[index].x;
y0 = points[index].y;
@rcdilorenzo
rcdilorenzo / KIFUITestActor+Generic.m
Created December 12, 2013 16:11
A KIF extension to allow tapping a cell based on its name
- (void)tapCellWithName:(NSString *)name inTableViewWithAccessibilityLabel:(NSString *)accessibilityLabel {
[tester runBlock:^KIFTestStepResult(NSError *__autoreleasing *error) {
UITableView *tableView = (UITableView *)[tester waitForViewWithAccessibilityLabel:accessibilityLabel];
NSIndexPath *foundIndexPath = nil;
for (int section = 0; section < tableView.numberOfSections; section++) {
for (int row = 0; row < [tableView numberOfRowsInSection:section]; row++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell.textLabel.text isEqualToString:name]) {
foundIndexPath = indexPath;
@rcdilorenzo
rcdilorenzo / comments.txt
Created December 11, 2013 20:37
Unique programmer comments
long long ago; /* in a galaxy far far away */
// Replaces with spaces the braces in cases where braces in places cause stasis
$str = str_replace(array("\{","\}")," ",$str);
#define TRUE FALSE //Happy debugging suckers
// sometimes I believe compiler ignores all my comments
// I am not sure if we need this, but too scared to delete.
@rcdilorenzo
rcdilorenzo / gist:7169910
Created October 26, 2013 14:14
Animating constraints in the method "updateConstraints"
- (void)animateConstraints {
[self setNeedsUpdateConstraints];
[UIView animateWithDuration:0.4 animations:^{
[self layoutIfNeeded];
}];
}