-
Notifications
You must be signed in to change notification settings - Fork 9
/
Helpers.fs
64 lines (52 loc) · 1.81 KB
/
Helpers.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// This is Richard Minerich's F# Ant Colony Silverlight Ediiton
// Visit my Blog at http://RichardMinerich.com
// This code is free to be used for anything you like as long as I am properly acknowledged.
//
// The basic Silverlight used here is based on Phillip Trelford's Missile Command Example
// http://www.trelford.com/blog/post/MissileCommand.aspx
//
module AntsEverywhereLib.Helpers
open System
open System.Reflection
module Array2D =
let foldi f arr i =
let s = ref i in Array2D.iteri (fun x y cur -> s := (f x y !s cur)) arr
!s
module Array =
let randomPermute a =
let n = Array.length a
if n > 0 then
let rand = new Random()
let rec aux = function
| 0 -> a
| k ->
let i = rand.Next(k+1)
let tmp = a.[i]
a.[i] <- a.[k]
a.[k] <- tmp
aux (k-1)
aux (n-1)
else a
module Seq =
let randomPermute a =
a |> Seq.toArray |> Array.randomPermute |> Array.toSeq
module List =
let rec combinations n l =
match (n,l) with
| (0,_) -> [[]]
| (_,[]) -> []
| (n,x::xs) ->
let useX = List.map (fun l -> x::l) (combinations (n-1) xs)
let noX = combinations n xs
useX @ noX
let private r = Random(int DateTime.Now.Ticks)
let random l =
let index = r.Next(0, List.length l) in
l.[index]
let input (x : 'a) app = app x
let inline holds (v : 'a) (conds : ('a -> bool) list) = List.forall (fun cond -> cond v) conds
let inline (<?<) l r = (fun x -> x > l && x < r)
let inline (<=?<) l r = (fun x -> x >= l && x < r)
let inline (<?=<) l r = (fun x -> x > l && x <= r)
let inline (<=?=<) l r = (fun x -> x >= l && x <= r)