This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| get_model_by_name = lambda name: next(m for m in __import__('django').db.models.get_models() if m.__name__ == name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| while_not_none = lambda x, f: (lambda fx: [] if fx is None else [fx] + while_not_none(fx, f))(f(x)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- ============================================= | |
| -- 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Good | |
| import Database.Persist.Sql | |
| toSqlKey 1 :: UserId | |
| -- Bad | |
| Prelude.read "UserKey {unUserKey = SqlBackendKey {unSqlBackendKey = 1}}" :: UserId |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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 = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |