Skip to content

Instantly share code, notes, and snippets.

View HallM's full-sized avatar

Matthew Hall HallM

View GitHub Profile
@HallM
HallM / short-prime.c
Last active January 25, 2016 23:39
N primes in 98 bytes of C code
// trivial division
h,i=2;g(j){return j*j>i||i%j++&&g(j);}main(){scanf("%i",&h);for(;h;i++)g(2)&&printf("%i ",i,--h);}
// Wilson's Theory
h,i;m(n){return n?n*m(n-1)%i:1;}main(){scanf("%i",&h);for(;h;)m(i++)+1-i||printf("%i ",i,--h);}
@HallM
HallM / sortable.sql
Created January 25, 2016 17:27
SQL to be able to have a sorting column with category
; removal
UPDATE page
, (SELECT category_id as oldcategory, page_order as oldorder FROM page WHERE id = :pgid) as t2
SET page.page_order = CASE
WHEN id = :pgid THEN -1
WHEN page.category_id = t2.oldcategory
AND page.page_order > t2.oldorder
THEN page.page_order-1
ELSE page.page_order
END
@HallM
HallM / pageorder-atomically-update.sql
Created January 20, 2016 23:46
Can update the order of a page and all pages in between old and new order indices safely
UPDATE page, (SELECT pageorder as oldorder FROM page WHERE id = :pgid) as t2 SET page.pageorder = CASE
WHEN id = :pgid THEN :neworder
WHEN page.pageorder <= :neworder AND page.pageorder > t2.oldorder THEN page.pageorder-1
WHEN page.pageorder >= :neworder AND page.pageorder < t2.oldorder THEN page.pageorder+1
ELSE page.pageorder
END
WHERE page.pageorder >= LEAST(:neworder, t2.oldorder) AND page.pageorder <= GREATEST(:neworder, t2.oldorder);
function privateHandlePut(req, res) {
// do things
}
function publicHandleGet(req, res) {
// do things
}
export.controller = {
publicHandleGet
@HallM
HallM / mvc-evolvution.txt
Created January 4, 2016 22:48
Evolving the MVC pattern for the real world
universe: everything that isn't this software
can only interface with software through the shell
- user
- monitor
- browser
- database
- file system
- external API
- API on the same machine but different piece of software
- sensors
import Bluebird from 'bluebird';
// assume express, promise-ready ORM (Sequelize, Bookshelf, Mongoose)
function wrap(genFn) {
var cr = Bluebird.coroutine(genFn);
return function (req, res, next) {
cr(req, res, next).catch(next);
}
}