Skip to content

Instantly share code, notes, and snippets.

View formido's full-sized avatar
🥰

Michael Terry formido

🥰
View GitHub Profile
@danielberkompas
danielberkompas / scheduler.ex
Created October 26, 2016 17:59
A simple mix task scheduler for Elixir apps
defmodule MyApp.Scheduler do
@moduledoc """
Schedules a Mix task to be run at a given interval in milliseconds.
## Options
- `:task`: The name of the Mix task to run.
- `:args`: A list of arguments to pass to the Mix task's `run/1` function.
- `:interval`: The time interval in millisconds to rerun the task.
@logrusorgru
logrusorgru / mysql.sql
Last active January 16, 2024 07:01
SQL: uniqueness, automatic created_at, updated_at refresh + soft delete. SQLite, PostgreSQL, MySQL
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- --
-- mysql --
-- --
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--
-- mysql <http://sqlfiddle.com/#!9/91afb5/2>
-- note: sqlfiddle is very stupid
@JamesMGreene
JamesMGreene / phantomjs-xhr.js
Last active June 11, 2018 10:53
Example of (a) successfully executing a cross-domain XHR from the PhantomJS outer context to a wildcard CORS-enabled site; and (b) failing to execute a cross-domain XHR from the PhantomJS outer context to a site that is not CORS-enabled.
var urls = [
/* Wildcard CORS enabled - Works in PhantomJS 1.9.0 */
'http://updates.html5rocks.com',
/* CORS disabled - Fails in PhantomJS 1.9.0 (and every other version) */
'http://www.google.com',
/* Hack workaround? */
/*
function(httpGet, callback) {
phantom.page.settings = (phantom.page.settings || {});
phantom.page.settings.webSecurityEnabled = false;
function get(url, mimeType, successCallback, errorCallback) {
var page = require("webpage").create(),
basePageMatch = /^(https?:)?\/\/[^\/]+/.exec(url),
basePage = basePageMatch ? basePageMatch[0] : url;
// HACK for relative protocol support
if (basePage.indexOf("//") === 0) {
basePage = "http:" + basePage;
}
@jcomellas
jcomellas / lists_join.erl
Created March 4, 2013 18:46
Join a list of elements in Erlang, adding a separator between them
%% @doc Join a a list of elements adding a separator between
%% each of them.
-spec join(iolist(), Sep :: term()) -> iolist().
join([Head | Tail], Sep) ->
join_list_sep(Tail, Sep, [Head]);
join([], _Sep) ->
[].
-spec join_list_sep(iolist(), Sep :: term(), Acc :: iolist()) -> iolist().
join_list_sep([Head | Tail], Sep, Acc) ->
@cpjolicoeur
cpjolicoeur / gist:3590737
Created September 1, 2012 23:15
Ordering a query result set by an arbitrary list in PostgreSQL

I'm hunting for the best solution on how to handle keeping large sets of DB records "sorted" in a performant manner.

Problem Description

Most of us have work on projects at some point where we have needed to have ordered lists of objects. Whether it be a to-do list sorted by priority, or a list of documents that a user can sort in whatever order they want.

A traditional approach for this on a Rails project is to use something like the acts_as_list gem, or something similar. These systems typically add some sort of "postion" or "sort order" column to each record, which is then used when querying out the records in a traditional order by position SQL query.

This approach seems to work fine for smaller datasets, but can be hard to manage on large data sets with hundreds (or thousands) of records needing to be sorted. Changing the sort position of even a single object will require updating every single record in the database that is in the same sort group. This requires potentially thousands of wri

@trevordixon
trevordixon / Routes.js
Last active May 9, 2016 16:59
Very simple Express-like routing for PhantomJS
var Routes = (function() {
var _ = {}, ctor = function(){};
_.bind = function bind(func, context) {
var bound, args, slice = Array.prototype.slice;
args = slice.call(arguments, 2);
return bound = function() {
if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
ctor.prototype = func.prototype;
var self = new ctor;
var result = func.apply(self, args.concat(slice.call(arguments)));
@jlouis
jlouis / item.erl
Created January 21, 2011 23:28
RPG/MMORPG/MUD Item handling ideas
-module(item).
-export([gen_shield/0]).
-export([transmogrify/2, as_item/1]).
-export([weapon_damage/1, look/1]).
-record(item, {
%% Rather arbitrary identity
id :: term(),
%% Proplist of items properties