Skip to content

Instantly share code, notes, and snippets.

View metric-space's full-sized avatar

Luke Meyers metric-space

View GitHub Profile
@metric-space
metric-space / hamming-distance.clj
Created October 21, 2016 04:01
hamming distance in clojure
(defn hamming-distance [s1 s2]
(if (not= (count s1) (count s2))
(throw (Exception. "Lengths are not equal"))
(->> (map = s1 s2)
(remove true?)
(count))))
@metric-space
metric-space / sicp1-31.scm
Created November 10, 2016 06:33
sicp 1.31 solution
;;Exercise 1.31, generate Pi via wallis formula
;; recognize pi = * 4 (2 * (1/3)* 4 * (1/5) ...) * ((1/3) * 4 * (1/5)...)
;; arithmetic progression formula was used to find out the ending term
;; a_k = a_1 + (n-1)d
(define (pi-prod n)
(define (inv k)
(if (even? k)
k
(/ 1 k)))
@metric-space
metric-space / randomtree.hs
Created January 10, 2017 07:36
random integer node value tree maker (of fixed height)
import Control.Monad.Trans.State
import System.Random
data Tree a = Empty | Node a (Tree a) (Tree a) deriving Show
makeRandomTree :: Int -> State StdGen (Tree Int)
makeRandomTree 0 = return Empty
makeRandomTree x = get >>= \gen -> (
let (randVal, newGen) = randomR (1,6) gen
in put newGen >> makeRandomTree (x-1) >>=
@metric-space
metric-space / fooling_around.hs
Last active January 22, 2017 14:42
fooling around with the state monad
import System.Random
import Control.Monad.State
-- given a [Int] add random numbers to it
a :: [Int]
a = [3, 1, 6, 7, 9]
myFunc :: Int -> State StdGen Int
myFunc x = do
gen <- get
@metric-space
metric-space / diceRolls.hs
Created March 17, 2017 02:24
dice rolls using state monad and mapM
import System.Random
import Control.Monad.State
diceRange :: (Int, Int)
diceRange = (1,6)
diceThrow :: State StdGen Int
diceThrow = do
gen <- get
let (a,newGen) = randomR diceRange gen
@metric-space
metric-space / begscotty.hs
Created March 22, 2017 20:27
simple beginner scotty that makes a http client request in response to a GET request
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
module Main where
import Web.Scotty
import qualified Network.Wreq as W
-- import Data.Aeson
import GHC.Generics
import qualified Data.Text.Lazy as L
class Node:
def __init__(self,val,parent=None):
self.val = val
self.children = []
self.parent = parent
def __eq__(self,other):
if self.val == other.val:
return True
@metric-space
metric-space / simpleParallelScheduler.hs
Last active April 6, 2017 18:56
simple parallel scheduler in haskell
import Data.Time
import Data.Time.Clock
import Control.Monad
import Control.Concurrent
import Data.Maybe
data Job = Job { date :: UTCTime, message :: String }
timeFormat :: String
timeFormat = "%Y-%m-%dT%H:%M:%S"
@metric-space
metric-space / something.hs
Created April 16, 2017 04:44
haskell file serving the server and continously printing out a line at the same time
{-# LANGUAGE OverloadedStrings #-}
import Control.Concurrent
import Control.Concurrent.Async
import Control.Monad
import Web.Scotty
asyncPrint :: IO ()
asyncPrint = forever $ putStrLn "Hello" >> threadDelay (1000000)
@metric-space
metric-space / something.scala
Created April 19, 2017 20:06
scala + http4s to download and stash webpage
package com.example
import java.io._
import org.http4s._
import org.http4s.client.blaze._
object Hello {
def writeToFile (content: String): Unit = {