Created
May 16, 2014 07:30
-
-
Save ninegrid/0673800557868f0bbb12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| |
(* original sample data.. search/replaced symbols from python | |
dictionary of dictionaries *) | |
let criticsList = [ | |
"Lisa Rose", [ | |
"Lady in the Water", 2.5; "Snakes on a Plane", 3.5; | |
"Just My Luck", 3.0; "Superman Returns", 3.5; | |
"You; Me and Dupree", 2.5; "The Night Listener", 3.0 | |
]; | |
"Gene Seymour", [ | |
"Lady in the Water", 3.0; "Snakes on a Plane", 3.5; | |
"Just My Luck", 1.5; "Superman Returns", 5.0; | |
"The Night Listener", 3.0; "You; Me and Dupree", 3.5 | |
]; | |
"Michael Phillips", [ | |
"Lady in the Water", 2.5; "Snakes on a Plane", 3.0; | |
"Superman Returns", 3.5; "The Night Listener", 4.0 | |
]; | |
"Claudia Puig", [ | |
"Snakes on a Plane", 3.5; "Just My Luck", 3.0; | |
"The Night Listener", 4.5; "Superman Returns", 4.0; | |
"You; Me and Dupree", 2.5 | |
]; | |
"Mick LaSalle", [ | |
"Lady in the Water", 3.0; "Snakes on a Plane", 4.0; | |
"Just My Luck", 2.0; "Superman Returns", 3.0; | |
"The Night Listener", 3.0; "You; Me and Dupree", 2.0 | |
]; | |
"Jack Matthews", [ | |
"Lady in the Water", 3.0; "Snakes on a Plane", 4.0; | |
"The Night Listener", 3.0; "Superman Returns", 5.0; | |
"You; Me and Dupree", 3.5 | |
]; | |
"Toby", [ | |
"Snakes on a Plane", 4.5; "You; Me and Dupree", 1.0; | |
"Superman Returns", 4.0 | |
] | |
] | |
(* coerced it to a Map *) | |
let xs : Map<string,Map<string,float>> = | |
Map.ofList | |
(List.map (fun x -> | |
(fst x, (Map.ofList <| snd x))) criticsList) | |
(* some tuple helpers *) | |
module Tuple = | |
let apply2 (t1: 'a1 -> 'b1) | |
(t2: 'a2 -> 'b2) | |
((a1, a2) : 'a1 * 'a2) : 'b1 * 'b2 = t1 a1,t2 a2 | |
let tee2 (f1 : 'a -> 'b1) (f2 : 'a -> 'b2) (a:'a) = f1 a,f2 a | |
let flatten<'a,'b,'c when 'a : comparison and 'b : comparison> : Map<'a,Map<'b,'c>> -> seq<('a * 'b) * 'c> = | |
Map.toSeq | |
>> Seq.map (Tuple.apply2 id Map.toSeq) | |
>> Seq.collect (fun (a, s) -> | |
Seq.map (fun (b,c) -> | |
(a,b),c) s) | |
let unflatten<'a,'b,'c when 'a : comparison and 'b : comparison> : seq<('a * 'b) * 'c> -> Map<'a,Map<'b,'c>> = | |
Seq.map (fun ((a,b),c) -> a,(b,c)) | |
>> Seq.groupBy fst | |
>> Seq.map (Tuple.apply2 id (Seq.map snd >> Map.ofSeq)) | |
>> Map.ofSeq | |
let pivot<'a,'b,'c when 'a : comparison and 'b : comparison> : Map<'a,Map<'b,'c>> -> Map<'b,Map<'a,'c>> = | |
flatten | |
>> Seq.map (fun ((a,b),c) -> b,(a,c)) | |
>> Seq.groupBy fst | |
>> Seq.map (Tuple.apply2 id (Seq.map snd >> Map.ofSeq)) | |
>> Map.ofSeq | |
let ys = flatten xs | |
let ys' = unflatten ys | |
ys' = xs | |
let xs' = pivot xs | |
pivot (pivot xs) = xs | |
type G<'a, 'b, 'c when 'a : comparison and 'b : comparison> = { | |
vertices : Map<'a,'b option> | |
edges : Map<('a * 'b), 'c option> | |
} | |
let g = { | |
vertices = Map.ofSeq | |
<| Seq.append | |
(seq { for x in xs do yield x.Key,None }) | |
(seq { for x in pivot xs do yield x.Key,None }) | |
edges = Map.ofSeq | |
<| Seq.map (fun (x,y) -> ( x,(Some y) ) ) (flatten xs) | |
} | |
let listAllFoldersUnder dir = | |
let rec aux dir = | |
seq { yield System.IO.DirectoryInfo(dir) | |
for d in System.IO.Directory.EnumerateDirectories(dir) do | |
yield! aux d } | |
aux dir |> Seq.collect (fun x -> seq [ x.FullName ]) | |
let listAllFoldersUnder' dir = | |
let rec aux dir = | |
seq { yield System.IO.DirectoryInfo(dir) | |
for d in System.IO.Directory.EnumerateDirectories(dir) do | |
yield! aux d } | |
Seq.collect (fun (x : System.IO.DirectoryInfo) -> seq [ x.FullName ]) <| aux dir | |
// returns true if a is member of set l | |
let rec memb a l = | |
match (a, l) with | |
| (_, []) -> false | |
| (a, (x::xs)) -> if a = x then true else memb a xs | |
// returns the intersection of sets l1 and l2 | |
let rec intersection l1 l2 = | |
match (l1, l2) with | |
| ([], _) -> [] | |
| ((x::xs), l) -> if (memb x l) then x::(intersection xs l) else (intersection xs l) | |
// returns the union of sets l1 and l2 | |
let rec union (l1 : 'a list when 'a : equality) (l2 : 'a list when 'a : equality) : 'a list = | |
if (l1,l2) = (List.empty,List.empty) then List.empty | |
else | |
match (l1, l2) with | |
| ([], l) -> l | |
| ((x::xs), l) -> if not (memb x l) then x::(union xs l) else (union xs l) | |
let union' (xs : 'a list) (ys : 'a list) = | |
let aux xs ys = xs @ ys | |
if (xs,ys) = (List.empty,List.empty) then [] | |
else | |
aux (List.map (fun x -> Some x) xs) (List.map (fun y -> Some y) ys) | |
|> List.filter (fun x -> x.IsSome) |> List.map (fun x -> x.Value) | |
let maxLst lst = | |
lst |> List.fold (fun acc x -> | |
match acc with | |
| None -> Some(x) | |
| Some(y) -> Some(max x y)) None;; | |
maxLst [1;2;3;4] | |
let maxLst' = List.fold (fun acc x -> match acc with None -> Some(x) | Some(y) -> Some(max x y)) None | |
List.max [1;2;3;4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment