Skip to content

Instantly share code, notes, and snippets.

module Data.Vector.FindMax where
import Data.Vector (Vector, (!), (!?))
import qualified Data.Vector as V
import Data.Maybe (fromMaybe)
findMax :: Vector Int -> Maybe Int
findMax v
| V.null v = Nothing
| pivotValue > leftValue && pivotValue > rightValue = Just pivotValue
module Data.Integer.EveryOther where
genericEveryOther :: Monoid a => [a] -> [a]
genericEveryOther [] = []
genericEveryOther xs = zipWith mappend (scanl mappend mempty (init xs)) (scanr mappend mempty (tail xs))
everyOther :: [Int] -> [Int]
everyOther [] = []
everyOther xs = zipWith (*) (scanl (*) 1 (init xs)) (scanr (*) 1 (tail xs))
#!/bin/bash
# usage: copy me to .git/hooks/pre-commit
# chmod +x .git/hooks/pre-commit
# Stash unstaged changes and untracked files before running tests
git stash -q --keep-index -u
# Run non-feature tests
rspec --format progress --tag ~type:feature
#!/bin/bash
# usage: copy me to .git/hooks/pre-commit
# chmod +x .git/hooks/pre-commit
# Stash unstaged changes and untracked files before running tests
git stash -q --keep-index -u
# Run non-feature tests
rspec --format progress --tag ~type:feature
#!/bin/bash
# from https://github.com/depy/my-git-hooks/blob/master/run-rspec-test-pre-commit
# usage: copy me to .git/hooks/pre-commit
# chmod +x .git/hooks/pre-commit
# Stash unstaged changes before running tests
git stash -q --keep-index
# Run tests
{
"latitude": 42.3212,
"longitude": -71.1103,
"timezone": "America\/New_York",
"offset": -4,
"currently": {
"time": 1460238221,
"summary": "Mostly Cloudy",
"icon": "partly-cloudy-day",
"nearestStormDistance": 8,
module Data.List.Permute where
permute :: [a] -> [[a]]
permute [] = [[]]
permute xs = let picks = pick xs
f (x, xs') = map (x:) (permute xs')
in concatMap f picks
pick :: [a] -> [(a, [a])]
pick [] = []
module Data.Tree where
import Data.List (sortBy)
import Data.Ord (comparing)
data Tree a = Tip | Node a (Tree a) (Tree a)
deriving (Show, Eq)
-- x, y
type Coords = (Int, Int)
module ClosestToZero where
closestToZero :: [Int] -> Maybe Int
closestToZero [] = Nothing
closestToZero xs = Just $ foldl1 f xs
where f x y = case compare (abs x) (abs y) of
LT -> x
EQ -> abs x
GT -> y
import java.util.*;
class MapDiff {
public static void main(String[] args) {
Map<String, Integer> m1 = new HashMap<String, Integer>();
m1.put("a", 1);
m1.put("b", 2);
m1.put("c", 3);
Map<String, Integer> m2 = new HashMap<String, Integer>();