Skip to content

Instantly share code, notes, and snippets.

{-# LANGUAGE ScopedTypeVariables #-}
import Data.Maybe
swords :: String -> Bool
swords "hello" = True
swords "my" = True
swords "sam" = True
swords _ = False
@seantalts
seantalts / BinarySearch.hs
Created June 19, 2016 15:58
Binary Search to find first occurrence with duplicates
module BinarySearch where
import Data.Array
binarySearch :: (Ord a1) => Array Int a1 -> a1 -> Int -> Int -> Maybe Int
binarySearch haystack needle lo hi
| hi < lo = Nothing
| pivot > needle = binarySearch haystack needle lo (mid-1)
| pivot < needle = binarySearch haystack needle (mid+1) hi
| lo /= mid = binarySearch haystack needle lo mid
@seantalts
seantalts / BinarySearch.hs
Created June 19, 2016 15:45
trying to find first element with duplicates (doesn't work)
{-# LANGUAGE ScopedTypeVariables #-}
module BinarySearch where
import Data.Array
binarySearch :: (Ord a1) => Array Int a1 -> a1 -> Int -> Int -> Maybe Int
binarySearch haystack needle lo hi
| hi < lo = Nothing
| pivot > needle = binarySearch haystack needle lo (mid-1)
dim mat = (length mat, length (head mat))
subMats mat (height, width) =
[trimmed | r <- [0..rows]
, c <- [0..columns]
, let vertTrimmed = take height (drop r mat)
trimmed = map (take width . drop c) vertTrimmed
, length trimmed == height
, length (head trimmed) == width]
where (rows, columns) = dim mat
module Bear (main, findMinSubStr) where
import qualified Data.Map.Strict as Map
import qualified Data.List as List
import qualified Data.ByteString as BS
import Data.Maybe (fromMaybe)
import Data.ByteString.Char8 (readInt)
import GHC.Word (Word8)
type CharFreq = Map.Map Word8 Int
@seantalts
seantalts / lisp.py
Created May 4, 2016 18:40
small terrible lisp ast parser
import re
def lex(s):
return re.findall("(\(|\)|[\w\*\+\-]+)", s)
def get_ast(tokens):
ast = []
while tokens:
t = tokens.pop(0)
if t == '(':
2014-05-31 19:24:29,346 WARN : org.graylog2.rest.resources.search.SearchResource - Unable to execute search: Failed to execute phase [query], all shards failed; shardFailures {[QuBZzG4dRFK2NpQkn3BFlg][graylog2_0][0]: RemoteTransportException[[Leap-Frog][inet[/10.102.144.101:9300]][search/phase/query]]; nested: SearchParseException[[graylog2_0][0]: from[0],size[100]: Parse Failure [Failed to parse source [{"from":0,"size":100,"query":{"query_string":{"query":"column \\\"product_id\\\" of relation \\\"cart_records\\\" does not exist\" P:\"61\"","allow_leading_wildcard":false}},"post_filter":{"bool":{"must":{"range":{"timestamp":{"from":"2014-05-31 18:24:29.330","to":"2014-05-31 19:24:29.330","include_lower":true,"include_upper":true}}}}},"sort":[{"timestamp":{"order":"desc"}}]}]]]; nested: QueryParsingException[[graylog2_0] Failed to parse query [column \"product_id\" of relation \"cart_records\" does not exist" P:"61"]]; nested: ParseException[Cannot parse 'column \"product_id\" of relation \"cart_records\" do
package httptimeout
import (
"net/http"
"time"
"fmt"
)
type TimeoutTransport struct {
http.Transport
(**
* OCaml for the Curious
An Introductory tutorial in OCaml
Compiled by Prasad Rao (raoprasadv AT gmail DOT com)
Compiled together from various sources.
As this material is introductory
_None_ of this material is likely to be original. I thank
>>> def greets(function):
... def greets_wrapper(*args, **kwargs):
... print "hi there, ", function
... return function(*args, **kwargs)
... return greets_wrapper
...
>>> @greets
... def add(num1, num2):
... return num1 + num2
...