Skip to content

Instantly share code, notes, and snippets.

View fffej's full-sized avatar

Jeff Foster fffej

View GitHub Profile
@fffej
fffej / fp.dot
Created May 2, 2012 13:51 — forked from fogus/fp.dot
early influence graph of fp languages
digraph G {
KRC -> Miranda;
ML -> Miranda;
KRC -> SASL;
SASL -> Miranda;
Miranda -> Haskell;
Hope -> Haskell;
ML -> Caml;
Caml -> FSharp;
Clean -> Haskell;
@fffej
fffej / Root.hs
Created June 2, 2011 20:25
Controller code.
{-# LANGUAGE TemplateHaskell, QuasiQuotes, OverloadedStrings #-}
module Handler.Root where
import FortuneSearch
import Search
import Data.Text (Text)
import qualified Data.Text as T
import Yesod.Json
@fffej
fffej / Search.hs
Created June 2, 2011 20:14
Search "engine" for simple keyword search.
module Search (
getKeyValues
, parseQuery
, query
) where
import FortuneIndexer (getTerm)
import qualified Data.Text as T
import Database.Redis.Redis
@fffej
fffej / FortuneIndexer.hs
Created June 2, 2011 06:40
Index some fortune cookies
{-# LANGUAGE OverloadedStrings #-}
module FortuneIndexer (
getTerm
) where
import Control.Monad (forM_)
import Data.List.Split (splitOn)
import Data.Char (isLetter, isSpace)
import Data.Text.Encoding as E
@fffej
fffej / FortuneParser.hs
Created June 1, 2011 21:50
Some code to parse the fortune files
import qualified Data.Text.IO as T
import Data.List.Split (splitOn)
splitOnPercent :: T.Text -> [T.Text]
splitOnPercent = T.splitOn "%"
splitOnBlankLines :: T.Text -> [T.Text]
splitOnBlankLines t = (init . map T.concat) (splitOn [" "] (T.lines t))
fortunes :: [(FilePath,T.Text -> [T.Text])]
@fffej
fffej / gist:998182
Created May 29, 2011 22:26
Simple expression parser
import qualified Data.Text as T
import Text.ParserCombinators.Parsec
import Text.ParserCombinators.Parsec.Combinator
import Text.ParserCombinators.Parsec.Char
import Text.ParserCombinators.Parsec.Expr
import Text.ParserCombinators.Parsec.Token
import Text.ParserCombinators.Parsec.Language
import Text.ParserCombinators.Parsec.Error
data Query = Contains T.Text
compareLists :: [String] -> [String] -> Int -> [(String,String)]
compareLists x y dist = nearest
where
-- Hashes
hashX = map (computeHash . FV) x
hashY = map (computeHash . FV) y
-- Lookups
nameX = zip hashX x
nameY = zip hashY y
nearest = [(fromJust $ lookup hx nameX
-- http://www.mail-archive.com/haskell-cafe@haskell.org/msg10223.html
instance Arbitrary Word64 where
arbitrary = do
let mx,mn :: Integer
mx = fromIntegral (maxBound :: Word64)
mn = fromIntegral (minBound :: Word64)
c <- choose (mx, mn)
return (fromIntegral c)
hammingDistance2 :: Word64 -> Word64 -> Int
hammingDistance2 a b = f 0 (a `xor` b)
where
f c v | v == 0 = c
| otherwise = f (c + 1) (v .&. (v - 1))
hammingDistance :: Word64 -> Word64 -> Int
hammingDistance a b = length $ filter (\x -> testBit a x /= testBit b x) [0..63]