Skip to content

Instantly share code, notes, and snippets.

View davenportw15's full-sized avatar

William Davenport davenportw15

View GitHub Profile
@davenportw15
davenportw15 / FirstSpock.hs
Created May 30, 2015 16:44
A journey into Haskell web development
{-# LANGUAGE OverloadedStrings #-}
-- Ignore all the redundant string conversions
-- There's probably a better way to do it
import Control.Applicative
import qualified Data.Text as Text
import Data.ByteString.Lazy.Char8 (unpack)
import Web.Spock.Safe
@davenportw15
davenportw15 / BrainF.rs
Last active August 29, 2015 14:22
Partial BrainF Interpreter
fn main() {
let program_data = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
let mut program = Program::from_chars(program_data);
program.step();
println!("OUTPUT:");
println!(" pointer: {}", program.pointer);
println!(" data: {:?}", program.data);
}
#[derive(Debug)]
@davenportw15
davenportw15 / WordCounter.hs
Created July 14, 2015 03:25
Map counter in Haskell
import Data.Map (Map)
import qualified Data.Map as Map
import Control.Monad
-- | Returns a Map with counts of items in a list
count :: (Ord a, Integral b) => [a] -> Map a b
count =
foldr updateMap Map.empty
where updateMap v counts
| Map.member v counts = Map.adjust succ v counts
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"database/sql"
_ "github.com/lib/pq"
)
module EvaluateSandwich
( SandwichComponent
, Sandwich
, validateSandwich
, indicesSeparated
) where
import Data.List (elemIndices)
-- | Components of a sandwich
@davenportw15
davenportw15 / FizzBuzz.hs
Last active August 29, 2015 14:26
A FizzBuzz implementation in one line of Haskell
mapM_ putStrLn $ map (\x -> case (x `rem` 3, x `rem` 5) of (0, 0) -> "FizzBuzz"; (0, _) -> "Fizz"; (_, 0) -> "Buzz"; (_, _) -> show x) [1..100]
-- Alternatively
import Data.Function ((&))
[1..100] & map (\x -> case (x `rem` 3, x `rem` 5) of (0, 0) -> "Fizzbuzz"; (0, _) -> "Fizz"; (_, 0) -> "Buzz"; (_, _) -> show x) & mapM_ putStrLn
@davenportw15
davenportw15 / ReferenceSurroundings.hs
Created August 11, 2015 04:00
A general method to traverse neighboring elements in lists.
-- This module demonstrates two key principles:
-- 1. A general method of traversing neighboring elements in lists
-- 2. Appropriate and understandable abstractions are always your friends
module ReferenceSurroundings where
-- | Returns sublists from xs of length `size`
-- i.e. chunkIntoSize 3 [1..5] ==
-- [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
chunkInto :: Int -> [b] -> [[b]]
@davenportw15
davenportw15 / RailroadMonad.hs
Created August 20, 2015 00:57
An example of "railroad" error handling
module Result where
import Control.Monad
import Data.Monoid
data Result a b
= Failure a
| Success b
deriving (Show)
@davenportw15
davenportw15 / HashMap.hs
Created September 7, 2015 05:51
A hashmap implementation in Haskell backed by a binary tree.
module HashMap where
import Prelude hiding (lookup)
-- | HashMap backed by a binary tree. Most operations are O(log(n))
data HashMap k v = EmptyMap | Map (k, v) (HashMap k v) (HashMap k v) deriving (Read, Show)
-- | Apply fmap over all values
instance (Ord k) => Functor (HashMap k) where
//
// LazySquares.swift
// SwiftBook
//
// Created by William Davenport on 12/10/15.
// Copyright © 2015 William Davenport. All rights reserved.
//
import Foundation