Skip to content

Instantly share code, notes, and snippets.

@yuga
yuga / gist:5598199
Last active December 17, 2015 10:58
haskell-relational-record #1 2013-05-17時点のものを使用。Exprが推論されるようになったので expr や toExpr を書かなくてすむ。
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonadComprehensions #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Database.Test.Query where
import Data.Int (Int32)
import Data.Time (Day)
import Database.HDBC.Record.Query (runQuery)
import Database.HDBC.Session (withConnectionIO)
@yuga
yuga / Query.hs
Last active December 17, 2015 07:39
haskell-hdbc-tool #2: ghc-modがplaceholderの型を決められなかったのでpのままだけど、この例の場合は (Date.Time.Day, Date.Time.Day)。 (Github 2013-05-11時点のhaskell-hdbc-toolを使用)
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonadComprehensions #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Database.Test.Query where
import Data.Int (Int32)
import Data.Time (Day, UTCTime(utctDay), getCurrentTime)
import Database.HDBC.Record.Query (runQuery)
import Database.HDBC.Session (withConnectionIO)
@yuga
yuga / PostgreSQL.hs
Created May 13, 2013 05:00
haskell-hdbc-tool #1: とりあえず簡単なクエリを動かすことができた。 (Github 2013-05-11時点のhaskell-hdbc-toolを使用)
{-# LANGUAGE ScopedTypeVariables #-}
module Database.Test.PostgreSQL
(
connect
, defineTable
) where
import Language.Haskell.TH (Dec, Q)
import Language.Haskell.TH.Name.CamelCase (ConName)
@yuga
yuga / Tables.md
Last active December 15, 2015 15:09

軽い気持ちで tables を使ってみました

準備

{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
@yuga
yuga / SortKey.hs
Created March 14, 2013 08:38
GNU sort コマンドの -k オプションの引数を Parsecでパースして、テキトウなデータ型 SortKey に格納するコード。 この結果を使用するプログラムをあとで書こうと思うのだけど、フラットなデータ型で持ちまわるのが便利か、それともASTのままでよかったのか、僕には未知数。 たしかに n と r のフラグだけはASTのままと不便だし...
module SortKey where
import Control.Applicative ( (<$>), (<*>), (<*), (*>) )
import Control.Monad ( void )
import Text.Parsec ( ParsecT, Stream, char, digit, eof, many, many1
, option, runParser, space, (<|>) )
import Text.Parsec.String ( Parser )
data SortKeyAST = SortKeyAST StartF EndF
data StartF = SF Int StartC SortOpt
@yuga
yuga / sort_bnf.txt
Created March 12, 2013 02:00
An unserious BNF of the GNU sort command. Redundant separators are placed deliberately.
<key> := <startf> <separator>*
(<dot> <separator>* <startc> <separator>*)?
(<option> <separator>*)*
((<comma> <separator>* <endf> <separator>*)?
(<dot> <separator>* <endc> <seprator>*)?
(<option> <separator>*)*
)?
<startf> := <num>+
<startc> := <num>+
-- |
-- Create patterns to divide.
--
-- >>> divide 4
-- [[1,1,1,1],[1,1,2],[1,2,1],[1,3],[2,1,1],[2,2],[3,1],[4]]
--
divide :: Int -> [[Int]]
divide 0 = [[]]
divide n = [[x]:y | x <- [1..n], y <- divide (n-x)]
@yuga
yuga / haskelldb_hdbc_lazy_query.patch
Created February 7, 2013 07:05
HaskellDBのquery関数がstrictなので無理やりlazyにしてみるpatch。HDBC専用。HSQLはどうするのがよいのやら。
--- haskelldb-2.2.2/src/Database/HaskellDB/Database.hs 2012-10-26 02:19:38.000000000 +0900
+++ haskelldb-2.2.2/src/Database/HaskellDB/Database.hs 2013-01-17 09:21:06.501451400 +0900
@@ -24,7 +24,7 @@
(!.)
-- * Type declarations
, Database(..)
- , GetRec(..), GetInstances(..)
+ , GetRec(..), GetInstances(..), GetInstancesIO
, GetValue(..)
-- * Function declarations
@yuga
yuga / [読書メモ] #2 A surpassing problem from Pearls of Functional Algorithm Design.lhs
Last active December 11, 2015 15:18
[読書メモ] #2 A surpassing problem from Pearls of Functional Algorithm Design
[読書メモ][PFAD]
Pearls of Functional Algorithm Design
2 A surpassing problem
======================================
Introduction
--------------------------
@yuga
yuga / Lines.hs
Created January 20, 2013 15:21
改行でstring分割
lines' :: String -> [String]
lines' s = go id s
where
go ac [] = [ac ""]
go ac ('\r':'\n':cs) = ac "" : go id cs
go ac ('\r' :cs) = ac "" : go id cs
go ac ( '\n':cs) = ac "" : go id cs
go ac ( c:cs) = go (ac . (c:)) cs