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 / process.php
Last active December 14, 2015 00:29
Validate a form using php.
<?php
session_start();
header("Content-Type: text/html");
$errors = array();
// Reset form.
if(array_key_exists('clearForm', $_POST))
{
unset($_SESSION);
@m-renaud
m-renaud / eintr-handling.c
Last active April 5, 2018 04:49
EINTR handling.
// Examples of EINTR handling for reentrant functions.
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Open can be interupted when blocked, waiting for a slow device such as a
@m-renaud
m-renaud / signal-vs-sigaction.c
Last active December 20, 2015 08:39
signal() and sigaction()
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
void signal_handler_function(int signo);
typedef void (*sighandler_t)(int);
int main()
@m-renaud
m-renaud / basic-config.el
Created July 29, 2013 02:17
Basic Emacs configuration.
(setq make-backup-files nil)
(electric-indent-mode t)
(setq auto-save-interval 0)
(setq transient-mark-mode nil)
(setq visible-bell 1)
(iswitchb-mode 1)
(auto-compression-mode 1)
;; Give buffers unique names when there are 2 with the same name...
(require 'uniquify)
@m-renaud
m-renaud / basic-vimrc
Last active December 20, 2015 08:39
Basic Vim config.
" Turn on syntax highlighting...
syn on
" Turn on file type detection...
filetype on
filetype plugin indent on
" Set up default settings...
set autoindent
set expandtab
@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