Skip to content

Instantly share code, notes, and snippets.

@chpatrick
chpatrick / marshal.hs
Last active August 29, 2015 13:57
Automatic type-safe binding generation sketch (GHC HEAD)
{-# LANGUAGE TypeFamilies, GADTs, DataKinds, TypeOperators, FlexibleInstances, OverlappingInstances, ConstraintKinds, MultiParamTypeClasses, ForeignFunctionInterface #-}
import Control.Applicative
import Foreign
import Foreign.C
-- heterogenous list
data HList (ts :: [ * ]) where
E :: HList '[]
(:.) :: t -> HList ts -> HList (t ': ts)
@fumieval
fumieval / sprite.hs
Created February 22, 2014 06:16
Lens-based sprite
{-# LANGUAGE MultiWayIf, ExistentialQuantification, OverloadedStrings #-}
import FreeGame
import qualified Data.IntMap as IM
import Control.Lens
import Control.Monad.State
import Control.Applicative
import Control.Monad
import Control.Monad.Identity
import qualified Data.Traversable as T
@taiki45
taiki45 / maybe_monad.hs
Last active December 29, 2015 01:39
How monads work for OOProgrammers. Monad in object-oriented-language is useless unless for leaning.
import Control.Monad
bind :: (Monad m, Functor m) => m a -> (a -> m b) -> m b
bind m f = join (fmap f m)
unit :: Monad m => a -> m a
unit = return
compose :: (b -> c) -> (a -> b) -> (a -> c)
compose = (.)
@tomjaguarpaw
tomjaguarpaw / lensesForArrows.lhs
Last active June 19, 2018 21:14
Lenses for Arrows describes how the Lens datatype from Control.Lens can be generalise to support arrows.
> {-# LANGUAGE Rank2Types #-}
>
> import Prelude hiding (id)
> import Data.Functor.Constant (Constant(Constant), getConstant)
> import Control.Arrow (Arrow, arr, first, Kleisli(Kleisli), runKleisli, (&&&))
> import Control.Category ((<<<))
> import Control.Lens (sequenceAOf)
> import qualified Control.Lens as L
> import qualified Control.Lens.Internal.Setter as LS
> import Data.Profunctor (Profunctor, rmap)
@merijn
merijn / gist:6130082
Last active January 2, 2019 10:07
Type family that disallows certain types for type variables.
{-# LANGUAGE ConstraintKinds, DataKinds, PolyKinds, TypeFamilies, TypeOperators #-}
import GHC.Exts (Constraint)
type family Restrict (a :: k) (as :: [k]) :: Constraint where
Restrict a (a ': as) = ("Error!" ~ "Tried to apply a restricted type!")
Restrict x (a ': as) = Restrict x as
Restrict x '[] = ()
foo :: Restrict a [(), Int] => a -> a
@alexeiz
alexeiz / gcharts.html
Created March 20, 2013 06:00
An example of using Google Chart Tools from CoffeeScript. This is a self-contained html page that displays a table and a column chart. It uses coffeescript code to prepare and display the data. The code is translated into js on the fly in the browser.
<html>
<head>
<title>CoffeeScript charts</title>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.load("visualization", "1", {packages:["table"]});
</script>
@nebuta
nebuta / haskell_advent_2012_26.md
Last active July 31, 2022 17:32
HaskellでWebスクレイピング - Haskell Advent Calendar 2012

HaskellでWebスクレイピング

この記事はHaskell Advent Calendar 2012の26日目の記事です。Haskell Advent Calendar初参加です。コメントなどお待ちしております。

前提知識 - Haskellでの文字列処理

HaskellではString([Char]の別名)が文字列の基本型で、これはリストであるためにパターンマッチ・再帰やPreludeやData.Listにあるリスト用の関数を使って処理ができるという利点があります。ただし、実用上はパフォーマンスが低いため、TextByteStringが代わりに用いられます。Textはいわゆるユニコード文字列、ByteStringはバイト列という区別です。この使い分けに関してはちょうどAdvent Calendarの@brain_appleさんのこの記事で解説されました。StackOverflowでの質問も参考になります。

HaskellでのHTMLパーシング用ライブラリの概観

Hackageを見ると、

@fumieval
fumieval / gloss_load_picture.hs
Created September 15, 2012 05:37
Glossで任意の画像を読み込む
import Graphics.Gloss.Data.Picture
import Codec.Picture.Repa
import Data.Array.Repa as R hiding (map)
import qualified Data.Array.Repa.Repr.ForeignPtr as RF
loadImage :: FilePath -> IO Picture
loadImage path = do
img <- imgData <$> either error id <$> readImageRGBA path
let Z :. h :. w :. _ = R.extent img
return $! bitmapOfForeignPtr w h (RF.toForeignPtr $ flipVertically img) True
@nullkal
nullkal / lifegame.kn
Created August 1, 2012 12:08
Life Game for Kuin
func Init(cfg : Kuin@CCfg)
do cfg.Title :: "Life Game for Kuin"
do cfg.FullScr :: false
do cfg.PadNum :: 1
do cfg.WaitTime :: 60
end func
func Main()
var WIDTH : int :: 168
var HEIGHT : int :: 96
@technoweenie
technoweenie / github_oauth_busy_developer_guide.md
Created May 30, 2010 18:34
GitHub OAuth Busy Developer's Guide

GitHub OAuth Busy Developer's Guide

This is a quick guide to OAuth2 support in GitHub for developers. This is still experimental and could change at any moment. This Gist will serve as a living document until it becomes finalized at Develop.GitHub.com.

OAuth2 is a protocol that lets external apps request authorization to private details in your GitHub account without getting your password. All developers need to register their application before getting started.

Web Application Flow

  • Redirect to this link to request GitHub access: