Skip to content

Instantly share code, notes, and snippets.

Jeff Foster fffej

Block or report user

Report or block fffej

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@fffej
fffej / StableMarriageProblem.hs
Created Jul 14, 2014
Stable Marriage Problem
View StableMarriageProblem.hs
import Data.List
import Data.Maybe
stableMatch :: (Eq m, Eq w) => [(m,[w])] -> [(w,[m])] -> [(m,w)]
stableMatch ms ws = stableMatch' []
where
stableMatch' ps = case unmarried ms ps of
Just unmarriedMan -> stableMatch' (findMatch unmarriedMan ws ps)
Nothing -> ps
View DynamicTimeWarping.hs
dtw :: V.Vector a -> V.Vector a -> (a -> a -> Int) -> Array (Int,Int) Int
dtw x y cost = runSTArray $ do
let n = V.length x
m = V.length y
maxcost = maxBound
d <- newArray ((0,0),(m,n)) 0
forM_ [1..n] (\i -> writeArray d (0,i) maxcost)
forM_ [1..m] (\i -> writeArray d (i,0) maxcost)
forM_ [1..n] $ \i ->
forM_ [1..m] $ \j -> do
@fffej
fffej / DynamicTimeWarpingWin.hs
Created Jul 21, 2014
DynamicTimeWarpingWin.hs
View DynamicTimeWarpingWin.hs
dtwWin :: V.Vector a -> V.Vector a -> (a -> a -> Int) -> Int -> Array (Int,Int) Int
dtwWin x y cost window = runSTArray $ do
let n = V.length x
m = V.length y
maxCost = maxBound
w = max window (abs (n - m)) -- constrain window size
d <- newArray ((0,0),(m,n)) maxCost
writeArray d (0,0) 0
forM_ [1..n] $ \i ->
forM_ [max 1 (i-w) .. min m (i+w)] $ \j -> do
View WarpingPath.hs
warpingPath :: Array (Int,Int) Int -> [(Int,Int)]
warpingPath arr = go (w,h) []
where
(_,(w,h)) = bounds arr
go p@(x,y) xs
| x == 0 && y == 0 = p : xs
| otherwise = go minVal (minVal : xs)
where
minVal = minimumBy (comparing (arr !)) [down,downLeft,left]
down = (max 0 (x-1),max 0 y)
View gist:791cf604e8feada28439
var foos = new List<int>{1,2,3,4};
var bars = new List<int>();
foreach(var foo in foos) {
Console.WriteLine("hello");
bars.Add(x*2);
}
// versus.
View gist:132843
(defn contains-value?
[coll val]
(not (nil? (some (partial = val) coll))))
(defn executing?
[x]
"Is x of the form: (executing ...)?"
(and (seq? x) (= 'executing (first x))))
(defn convert-op
View gist:134503
(deftest test-pattern-matching
(is (= fail (pat-match '(i need a ?X) '(i really need a vacation))))
(is (= no-bindings (pat-match '(this is easy) '(this is easy))))
(is (= fail (pat-match '(?X is ?X) '((2 + 2 is 4)))))
(is (= '{?X (2 + 2)} (pat-match '(?X is ?X) '((2 + 2) is (2 + 2)))))
(is (= '{?P (Mr Hulot and I) ?X (a vacation)} (pat-match '((?* ?P) need (?* ?X))
'(Mr Hulot and I need a vacation))))
(is (= '{?X (1 2 a b)} (pat-match '((?* ?X) a b (?* ?X)) '(1 2 a b a b 1 2 a b)))))
View gist:134781
(defn read-symbol
"Read a single symbol from a string returning nil on failure"
[s]
(read s false nil))
(defn get-response
"Get a response from the given string"
[s]
(with-open [r (PushbackReader. (StringReader. s))]
View gist:138049
(defstruct virtualmachine :mem :counter :inport :outport :status :firstrun :user)
(defn increment-counter
[vm]
(assoc vm :counter (inc (:counter vm))))
(defn memory-put
"Update the memory specified by the key with the address to the given value"
[vm key addr val]
(assoc vm key (assoc (key vm) addr val)))
View gist:138052
(defn run-machine
"Run the virtual machine with the decoded instructions."
[vm ops update-input]
(reduce
(fn [v [op args]]
(increment-counter (op v args)))
(update-input vm)
ops))
You can’t perform that action at this time.