Skip to content

Instantly share code, notes, and snippets.

Avatar
🕺
🌐

Vaibhav Sagar vaibhavsagar

🕺
🌐
View GitHub Profile
@vaibhavsagar
vaibhavsagar / expressionmatch.hs
Created Nov 7, 2016
Cute interview question solution
View expressionmatch.hs
import qualified Data.Map.Strict as Map
import Data.List (inits, tails, any)
match "" "" _ = True
match "" _ _ = False
match _ "" _ = False
match expr text matches = let
possibilities = zip (tail $ inits text) (tail $ tails text)
pattern = head expr
expr' = tail expr
@vaibhavsagar
vaibhavsagar / decompress-fail.hs
Last active Nov 13, 2016
Reproduction of a bug in pipes(?)
View decompress-fail.hs
#!/usr/bin/env stack
{- stack
--resolver lts-6.24
runghc
--package attoparsec
--package pipes-bytestring
--package containers
--package pipes-zlib -}
{-# LANGUAGE OverloadedStrings #-}
@vaibhavsagar
vaibhavsagar / empty-producer.hs
Last active Nov 27, 2016
Reproduction of a different bug in pipes-zlib(?)
View empty-producer.hs
#!/usr/bin/env stack
{- stack
--resolver lts-6.24
runghc
--package attoparsec
--package pipes-bytestring
--package containers
--package pipes-zlib -}
{-# LANGUAGE OverloadedStrings #-}
View code-dojo-2016-11-29.hs
import Data.List (inits, tails)
partitionSum [] n = n
partitionSum ls n = let
pairs = zip (tail $ inits ls) (tail $ tails ls)
pass = filter (\(l,r) -> (sum l) == (sum r)) pairs
maxes = map (\(l,r) -> max (partitionSum l (n+1)) (partitionSum r (n+1))) pass
in if (maxes == []) then n else (maximum maxes)
@vaibhavsagar
vaibhavsagar / Leftovers.ipynb
Created Dec 20, 2016
Zlib decompression with leftovers.
View Leftovers.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@vaibhavsagar
vaibhavsagar / KVStore.hs
Created Jan 24, 2017
A simple key-value store.
View KVStore.hs
#!/usr/bin/env stack
{- stack --resolver lts-7 --install-ghc runghc
--package aeson
--package servant-server
--package text
--package transformers
--package unordered-containers
--package warp
-}
View count8s.hs
import Data.List
count8s number = let
len = length $ filter (\string -> (read string :: Int) `mod` 8 == 0) $ tail $ subsequences number
in len `mod` (10^9+7)
@vaibhavsagar
vaibhavsagar / Owl.hs
Created Oct 5, 2017
Fun with (.).(.)
View Owl.hs
module Owl where
import System.FilePath ((</>))
fromMaybe :: a -> Maybe a -> a
fromMaybe a m = case m of
Nothing -> a
Just b -> b
prependPath :: FilePath -> FilePath -> FilePath
@vaibhavsagar
vaibhavsagar / default.nix
Created Jan 30, 2018
Nix expression for Exference
View default.nix
{ nixpkgs ? import <nixpkgs> {}, compiler ? "default" }:
let
inherit (nixpkgs) pkgs;
f = { mkDerivation, base, base-orphans, bifunctors, containers
, data-pprint, deepseq, deepseq-generics, directory, either
, hashable, haskell-src-exts, hood, lens, mmorph, mtl, multistate
, parsec, pqueue, pretty, process, safe, split, stdenv
@vaibhavsagar
vaibhavsagar / 2SAT.hs
Last active Feb 13, 2019
A series of 2SAT checker refactorings
View 2SAT.hs
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}
import qualified Data.Graph as G
import qualified Data.Array as A
import qualified Prelude as P
import Prelude hiding (lookup, read, replicate)