Skip to content

Instantly share code, notes, and snippets.

View m-renaud's full-sized avatar

Matt Renaud m-renaud

  • Google Inc.
  • San Francisco, CA
View GitHub Profile
@m-renaud
m-renaud / MatrixDemo.hs
Last active August 29, 2015 14:09
Simple Matrix addition in Haskell.
{-# LANGUAGE TemplateHaskell #-}
module MatrixDemo where
import Control.Lens
-- | Represents a matrix with the contents stored in row major order.
-- Note that we get equality, ability to read a Matrix from a stream
-- as well as write it to a stream for free by deriving (Eq,Read,Show).
data Matrix = Matrix { _numRows :: Int
, _numColumns :: Int
@m-renaud
m-renaud / matrix_demo.cxx
Last active August 29, 2015 14:09
Simple Matrix addition in C++.
#include <iostream>
#include <vector>
using std::vector;
// Represents a matrix with the contents stored in row major order.
struct Matrix {
int numRows;
int numColumns;
vector<vector<int>> contents;
@m-renaud
m-renaud / X.hs
Created November 15, 2014 06:38
A simple problem in C++ and Haskell.
f :: [[a]] -> Bool
f matrix = flattenedMatrix == (reverse flattenedMatrix)
where flattenedMatrix = concat matrix
-- Note that concat (defined in the default environment) simply flattens a list of lists into a list.
concat :: [[a]] -> [a]
@m-renaud
m-renaud / ScrabbleSolver.hs
Last active August 29, 2015 14:09
Find words in the English language that can be made with the characters given on the command line.
-- This version runs in ~0.7 seconds due to String actually being a linked list
-- of Char. Not super efficient.
main = do
chars <- head <$> getArgs
englishWords <- words <$> readFile "en.txt"
mapM_ putStrLn $ sortBy (comparing length) $ findValidWords chars englishWords
findValidWords :: String -> [String] -> [String]
findValidWords chars englishWords = filter (`canBeMadeWith` chars) englishWords
@m-renaud
m-renaud / Attoparsec.hs
Last active August 29, 2015 14:09
Reading lots of input.
-- | Timing breakdown:
-- Reading: 2.5s
-- Sorting: 3.5s
module Main where
import Control.Applicative
import Control.Monad
import Data.ByteString.Char8 (pack)
import Data.Attoparsec.ByteString.Char8
@m-renaud
m-renaud / FastParse.hs
Last active August 29, 2015 14:10
Fast Int Parsing
{-# LANGUAGE BangPatterns #-}
module FastParse (parseInts) where
import Prelude hiding (length)
import Control.Applicative hiding (empty)
import Control.Monad
import Data.Char
import Data.Maybe
import Data.Sequence
@m-renaud
m-renaud / IntParse.hs
Last active August 29, 2015 14:10
Int parsing and simple operation
{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_GHC -Odph #-}
import qualified Data.ByteString.Char8 as S
import qualified Data.Vector as U
-- Read ints from stdin into a vector then print the length.
main = S.getContents >>= print . U.length . parse
-- Fill a new vector from a file containing a list of numbers.
@m-renaud
m-renaud / SumOfSquaresStreamFusion.hs
Created November 22, 2014 09:29
Sum of squares. This contains two versions, one using Vector and another that takes advantage of stream fusion. The stream fusion version is sightly faster and uses constant space. You'll ALSO notice, the only difference between the two files is the 3rd import statement :)
{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_GHC -Odph #-}
import Prelude hiding (map, sum)
import qualified Data.ByteString.Char8 as S
import Data.List.Stream
main = S.getContents >>= print . sumOfSquares . parse
where sumOfSquares = sum . map (\x -> x * x)
@m-renaud
m-renaud / ParseUriQuery.hs
Last active August 29, 2015 14:10
Parse the query component of a URI.
module ParseUriQuery where
import Control.Applicative
import Data.Attoparsec.ByteString.Char8
import Data.ByteString.Char8 (ByteString, pack, unpack)
import Data.Either
data QueryTerm = QueryTerm { termName :: ByteString
, termValue :: Maybe ByteString
} deriving (Eq, Read, Show)
@m-renaud
m-renaud / .inputrc
Created December 4, 2014 19:26
Ergonomic keyboard mappings.
set bind-tty-special-chars off
Control-h: backward-char
"\eh": backward-word
Control-t: previous-history
Control-n: next-history
Control-s: forward-char
"\es": forward-word