Skip to content

Instantly share code, notes, and snippets.

@carymrobbins
carymrobbins / run-gist.sh
Created October 30, 2014 22:05
Execute the latest gist by its FILENAME, GIST id, with PROGRAM.
(export PROGRAM=runhaskell FILENAME=hello.hs GIST=6e4540c5d1970d8ce825 ; curl -s -k -H "Accept: application/vnd.github.v3+json" "https://api.github.com/gists/$GIST" | python -c "import os, sys, json ; print json.loads(sys.stdin.read())['files'][os.environ['FILENAME']]['content']" | $PROGRAM)
@carymrobbins
carymrobbins / get_model_by_name.py
Last active August 29, 2015 14:09
Get Django model by name
get_model_by_name = lambda name: next(m for m in __import__('django').db.models.get_models() if m.__name__ == name)
@carymrobbins
carymrobbins / funloops.py
Created November 24, 2014 21:21
Functional looping for Python
while_not_none = lambda x, f: (lambda fx: [] if fx is None else [fx] + while_not_none(fx, f))(f(x))
@carymrobbins
carymrobbins / tree_depth.sql
Created November 24, 2014 23:14
Recursive SQL query to find all nodes in a tree beyond a certain depth. This is for PostgreSQL specifically; however, other RDBMS that support recursive CTEs (e.g. Oracle, SQL Server) should be able to do this as well with a slight modification to the syntax.
with recursive base as (
select * from categories_category
), tree as (
select id, parent_id, 1 as level, name :: text
from base as root
where parent_id is null
union all
select child.id, child.parent_id, parent.level + 1, (parent.name || ' > ' || child.name) :: text
from tree as parent
join base as child
@carymrobbins
carymrobbins / fk_refs.py
Created November 25, 2014 15:16
Find all model classes that reference another via a foreign key.
from django.db.models import get_models
def fk_refs(klass):
"""Find all model classes that reference another via a foreign key.
:type Model
:rtype dict[Model, list[Field]]
"""
result = {}
for m in get_models():
for f in m._meta.fields:
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
module DBJoins where
import Data.Tuple (swap)
import Test.QuickCheck (quickCheck)
innerJoin :: [a] -> [b] -> (a -> b -> Bool) -> [(a, b)]
innerJoin xs ys p = [(x, y) | x <- xs, y <- ys, p x y]
-- =============================================
-- Author: Cary Robbins
-- Create date: 3/9/2012
-- Description: Returns a pivoted resultset from the table passed
-- =============================================
CREATE PROCEDURE [cmr].[sp_do_pivot]
@table sysname, -- Name of the table with source data to pivot
@column_field sysname, -- Field used to generate new columns
@value_field sysname = '', -- Values to group within new columns
@order_by sysname = NULL, -- Optional order by field
@carymrobbins
carymrobbins / ManualKey.hs
Last active May 2, 2017 23:58
Yesod - manually creating a Key
-- Good
import Database.Persist.Sql
toSqlKey 1 :: UserId
-- Bad
Prelude.read "UserKey {unUserKey = SqlBackendKey {unSqlBackendKey = 1}}" :: UserId
{-# LANGUAGE FlexibleInstances #-}
module Handler.JSRoutes where
import Import
import qualified Data.Text as T
import Database.Persist.Sql (toSqlKey)
import Control.Monad.Random
jsRoutes :: (MonadRandom m, Applicative m) => [m Text]
jsRoutes =
@carymrobbins
carymrobbins / MonoGroupBy.hs
Created December 19, 2014 05:37
An implementation of groupBy which returns a list of mono traversable
import ClassyPrelude
groupBy' :: IsSequence seq => (Element seq -> Element seq -> Bool) -> seq -> [MinLen (Succ Zero) seq]
groupBy' eq s = case uncons s of
Nothing -> mempty
Just (x, xs) -> mlcons x (toMinLenZero ys) : groupBy' eq zs
where
(ys,zs) = span (eq x) xs