-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from FayCarsons/utils
Created utility module
- Loading branch information
Showing
7 changed files
with
69 additions
and
51 deletions.
There are no files selected for viewing
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
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
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
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
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
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 file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
(* point -> point arithmetic *) | ||
open Shape | ||
let ( /~ ) p1 p2 = { x = p1.x /. p2.x; y = p1.x /. p2.x } | ||
|
||
(* point -> scalar arithmetic *) | ||
let ( -! ) { x; y } scalar = { x = x -. scalar; y = y -. scalar } | ||
let ( /! ) { x; y } scalar = { x = x /. scalar; y = y /. scalar } | ||
let ( *! ) { x; y } scalar = { x = x *. scalar; y = y *. scalar } | ||
let pmap f { x; y } = { x = f x; y = f y } | ||
|
||
(* Tuple/Vector ops *) | ||
let tmap f (x, y) = (f x, f y) | ||
let tmap3 f (a, b, c) = (f a, f b, f c) | ||
let tmap4 f (a, b, c, d) = (f a, f b, f c, f d) | ||
|
||
(** Function composition *) | ||
let ( >> ) f g x = g (f x) | ||
|
||
(* Partitions point in a polygon into faces *) | ||
let rec take n lst = | ||
match (n, lst) with | ||
| 0, _ -> ([], lst) | ||
| _, [] -> ([], []) | ||
| n, x :: xs -> | ||
let taken, rest = take (n - 1) xs in | ||
(x :: taken, rest) | ||
|
||
let rec partition n ?(step = 0) lst = | ||
match lst with | ||
| [] -> [] | ||
| lst' -> | ||
let taken, _ = take n lst in | ||
if List.length taken = n then | ||
taken | ||
:: | ||
partition n ~step (List.tl lst') | ||
else [] | ||
|
||
(* Misc *) | ||
let range n = List.init n Fun.id |
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 file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
val ( /~ ) : float Shape.point -> float Shape.point -> float Shape.point | ||
val ( -! ) : float Shape.point -> float -> float Shape.point | ||
val ( /! ) : float Shape.point -> float -> float Shape.point | ||
val ( *! ) : float Shape.point -> float -> float Shape.point | ||
val pmap : ('a -> 'b) -> 'a Shape.point -> 'b Shape.point | ||
val tmap : ('a -> 'b) -> 'a * 'a -> 'b * 'b | ||
val tmap3 : ('a -> 'b) -> 'a * 'a * 'a -> 'b * 'b * 'b | ||
val tmap4 : ('a -> 'b) -> 'a * 'a * 'a * 'a -> 'b * 'b * 'b * 'b | ||
val ( >> ) : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c | ||
val take : int -> 'a list -> 'a list * 'a list | ||
val partition : int -> ?step:int -> 'a list -> 'a list list | ||
val range : int -> int list |