Skip to content

Instantly share code, notes, and snippets.

View sycue's full-sized avatar

sycue

View GitHub Profile
@sycue
sycue / Math.js
Created September 14, 2016 01:37
(function() {
/**
* Math utility.
* @static
* @constructor
*/
tracking.Math = {};
/**
* Euclidean distance between two points P(x0, y0) and P(x1, y1).
* @param {number} x0 Horizontal coordinate of P0.
@sycue
sycue / NextHammingWeight
Created September 14, 2016 01:35 — forked from crsaracco/NextHammingWeight
NextHammingWeight - a function for finding the next int with the same hamming weight
/* I was searching for an algorithm that gave you the next integer (counting
* upwards) that has the same hamming weight as the current number, and I
* couldn't find anything. So here's a function that does that for you. If
* there is no 'next number' (i.e. this is the largest number that has this
* hamming weight), it simply returns the same number again.
*
* This can probably be optimized; I whipped it up pretty quickly. Relatedly, I
* only tested a few edge cases I could think of, so if you use this, be sure
* to test it more thoroughly.
*
class ApplicationController < ActionController::Base
#...
# this method expects a class to be passed with the attributes @@per_page,
# @@:per_page_max defined
def prepare_pagination(paginable_class)
# STEP 1: Prepare values
# STEP 1a: Prepare page
# what the client wants or 1 per default
page = params[:page].nil??
1 : params[:page].to_i
@sycue
sycue / ProceduralVsOop.php
Last active November 8, 2023 08:38
PHP Procedural vs. Object-Oriented Programming (OOP)
<?php
// Procedural
function example_new() {
return array(
'vars' => array()
);
}
function example_set($example, $name, $value) {
$example['vars'][$name] = $value;
@sycue
sycue / queue.php
Created October 23, 2013 08:22
PHP5 in Practice  (U.S.)Elliott III & Jonathan D.Eisenhamer
<?php
// A library to implement queues in PHP via arrays
// The Initialize function creates a new queue:
function &queue_initialize() {
// In this case, just return a new array
$new = array();
return $new;
}
// The destroy function will get rid of a queue
function queue_destroy(&$queue) {
@sycue
sycue / gist:6590284
Last active December 23, 2015 05:58
# Regular Expression To Parse Ruby Log Messages
# parse ruby log message
# customize as needed
LOG_EXPRESSION = /([\w]+),\s+\[([^\]\s]+)\s+#([^\]]+)\]\s+(\w+)\s+--\s+(\w+)?:\s+(.+)/
# sample log output from this call:
# logger.info("Ubiquitously") { "[dequeud] #{JSON.generate(params)}"}
string = 'I, [2010-08-15T16:16:46.142801 #81977] INFO -- Ubiquitously: {"title":"Google","url":"google.com","tags":"search, google, api","services":["meta_filter","mixx"],"description":"a search engine!"}'
string.gsub(LOG_EXPRESSION) do |match|
@sycue
sycue / lithp.rb
Created January 5, 2013 05:24 — forked from p8/lithp.rb
class Lisp
Fs = {
:label => lambda {|name,value| Fs[name] = lambda { value } },
:car => lambda {|sexp| sexp.first },
:cdr => lambda {|sexp| sexp.slice(1, sexp.size) },
:cons => lambda {|head,tail| [head] + tail },
:atom => lambda {|sexp| !sexp.is_a?(Array) },
:eq => lambda {|a,b| a == b },
:if => lambda {|cnd,thn,els| cnd ? thn : els }
}
@sycue
sycue / uri_host.rb
Created May 3, 2012 10:09
Ruby code to extract host from URL string
require 'uri'
myUri = URI.parse( 'http://www.mglenn.com/directory' )
print myUri.host