Skip to content

Instantly share code, notes, and snippets.

@marcojetson
marcojetson / route_compress.py
Created September 19, 2018 08:13
Compress a route by removing collinear points
def compress_route(route):
compressed = []
for point in route:
compressed.append(point)
if len(compressed) >= 3 and is_collinear(*compressed[-3:]):
del compressed[-2]
return compressed
def is_collinear(a, b, c):
SELECT
TO_CHAR(NOW(), 'YYYY-MM-DD') ||
'T' ||
TO_CHAR(NOW(), 'HH:MM:SS') ||
'+' ||
LPAD(EXTRACT(timezone_h from NOW())::TEXT, 2, '0') ||
':' ||
LPAD(EXTRACT(timezone_m from NOW())::TEXT, 2, '0')
;
/**
* Executes a function with a given interval
* @arg {function} fn - Function to execute,
* @arg {integer} interval - Interval in milliseconds
*/
function poll(fn, interval) {
fn(() => setTimeout(() => poll(fn, interval), interval));
}
poll(retry => $.get('/status').success(res => res.status === 'pending' && retry()));
@marcojetson
marcojetson / concept.php
Last active October 19, 2016 13:17
Array support for Phalcon forms
<?php
class Form extends \Phalcon\Forms\Form
{
/**
* @inheritdoc
*/
public function bind(array $data, $entity, $whitelist = null)
{
foreach ($this->getElements() as $element) {
@marcojetson
marcojetson / shortcuts.js
Created October 8, 2016 09:51
Key shortcut configuration macOS alike
$('input').keydown(function (e) {
e.preventDefault();
var keys = [];
e.altKey && keys.push('⌥');
e.ctrlKey && keys.push('⌃');
e.metaKey && keys.push('⌘');
e.shiftKey && keys.push('⇧');
@marcojetson
marcojetson / example.php
Created July 5, 2016 15:57
Extract and reduce measurement units from strings
<?php
require __DIR__ . '/strtounits.php';
assert(strtometers('25000.23 meters') === 25000.23);
assert(strtometers('3km, 25.5 meters') === 3025.5);
<?php
trait FlagTrait
{
/** @var int */
private $flags = 0;
/**
* @param int $flag
* @return bool
@marcojetson
marcojetson / packpub.sh
Created December 8, 2015 17:22
PacktPub free book of the day
curl https://www.packtpub.com/packt/offers/free-learning | grep -A7 dotd-title | tail -1 | xargs -0 osascript sendmail.applescript nobody@domain.com "Today's free eBook"
@marcojetson
marcojetson / emojis.html
Last active November 24, 2015 17:55
CSS emojis w/o images
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
.emoji {
display: inline-block;
font-style: normal;
position: relative;
top: 1px;
<?php
class PoolManagerException extends Exception {}
class PoolManagerInvalidWeightException extends PoolManagerException {}
class PoolManagerNodeNotFoundException extends PoolManagerException {}
class PoolManagerEmptyException extends PoolManagerException {}
/**
* Generic pool manager with horizontal scaling in mind
*