remove instances of 'local ... in ... end', replacing it with opauque structures with signatures, because I like the lower indentation level and additional whitespace in the latter

This commit is contained in:
2024-08-08 01:37:46 +01:00
parent d9607d27d8
commit 1264e20dc8
5 changed files with 505 additions and 466 deletions

View File

@@ -1,9 +1,14 @@
structure AppInit = signature APP_INIT =
sig
val fromWidthAndHeight : int * int -> AppType.app_type
end
structure AppInit :> APP_INIT =
struct struct
open AppType open AppType
local fun helpFromWidthAndHeight
fun init (windowWidth, windowHeight, wStart, wFinish, hStart, hFinish) = (windowWidth, windowHeight, wStart, wFinish, hStart, hFinish) =
let let
val xClickPoints = ClickPoints.generate (wStart, wFinish) val xClickPoints = ClickPoints.generate (wStart, wFinish)
val yClickPoints = ClickPoints.generate (hStart, hFinish) val yClickPoints = ClickPoints.generate (hStart, hFinish)
@@ -20,17 +25,19 @@ struct
, graphLines = graphLines , graphLines = graphLines
} }
end end
in
fun fromWidthAndHeight (windowWidth, windowHeight) = fun fromWidthAndHeight (windowWidth, windowHeight) =
if windowWidth = windowHeight then if windowWidth = windowHeight then
init (windowWidth, windowHeight, 0, windowWidth, 0, windowHeight) helpFromWidthAndHeight
(windowWidth, windowHeight, 0, windowWidth, 0, windowHeight)
else if windowWidth > windowHeight then else if windowWidth > windowHeight then
let let
val difference = windowWidth - windowHeight val difference = windowWidth - windowHeight
val wStart = difference div 2 val wStart = difference div 2
val wFinish = wStart + windowHeight val wFinish = wStart + windowHeight
in in
init (windowWidth, windowHeight, wStart, wFinish, 0, windowHeight) helpFromWidthAndHeight
(windowWidth, windowHeight, wStart, wFinish, 0, windowHeight)
end end
else else
let let
@@ -38,7 +45,7 @@ struct
val hStart = difference div 2 val hStart = difference div 2
val hFinish = hStart + windowWidth val hFinish = hStart + windowWidth
in in
init (windowWidth, windowHeight, 0, windowWidth, hStart, hFinish) helpFromWidthAndHeight
end (windowWidth, windowHeight, 0, windowWidth, hStart, hFinish)
end end
end end

View File

@@ -1,4 +1,20 @@
structure AppWith = signature APP_WITH =
sig
val windowResize: AppType.app_type * int * int -> AppType.app_type
val triangleStage: AppType.app_type * AppType.triangle_stage
-> AppType.app_type
val newTriangle:
AppType.app_type
* Real32.real
* Real32.real
* Real32.real
* Real32.real
* Real32.real
* Real32.real
-> AppType.app_type
end
structure AppWith :> APP_WITH =
struct struct
open AppType open AppType
@@ -49,16 +65,9 @@ struct
} }
end end
local fun helpWindowResize
fun make (app: app_type, windowWidth, windowHeight, wStart, wFinish, hStart, hFinish) :
( app: app_type app_type =
, windowWidth
, windowHeight
, wStart
, wFinish
, hStart
, hFinish
) : app_type =
let let
val val
{ xClickPoints = _ { xClickPoints = _
@@ -84,17 +93,18 @@ struct
, windowHeight = windowHeight , windowHeight = windowHeight
} }
end end
in
fun windowResize (app: app_type, windowWidth, windowHeight) = fun windowResize (app: app_type, windowWidth, windowHeight) =
if windowWidth = windowHeight then if windowWidth = windowHeight then
make (app, windowWidth, windowHeight, 0, windowWidth, 0, windowHeight) helpWindowResize
(app, windowWidth, windowHeight, 0, windowWidth, 0, windowHeight)
else if windowWidth > windowHeight then else if windowWidth > windowHeight then
let let
val difference = windowWidth - windowHeight val difference = windowWidth - windowHeight
val wStart = difference div 2 val wStart = difference div 2
val wFinish = wStart + windowHeight val wFinish = wStart + windowHeight
in in
make helpWindowResize
(app, windowWidth, windowHeight, wStart, wFinish, 0, windowHeight) (app, windowWidth, windowHeight, wStart, wFinish, 0, windowHeight)
end end
else else
@@ -103,7 +113,7 @@ struct
val hStart = difference div 2 val hStart = difference div 2
val hFinish = hStart + windowWidth val hFinish = hStart + windowWidth
in in
make (app, windowWidth, windowHeight, 0, windowWidth, hStart, hFinish) helpWindowResize
end (app, windowWidth, windowHeight, 0, windowWidth, hStart, hFinish)
end end
end end

View File

@@ -1,4 +1,20 @@
structure ClickPoints = signature CLICK_POINTS =
sig
val generate: int * int -> Real32.real vector
val getClickPosition:
Real32.real
* Real32.real
* Real32.real
* Real32.real
* Real32.real
* Real32.real vector
* Real32.real vector
* int
* int
-> Real32.real vector * Real32.real * Real32.real
end
structure ClickPoints :> CLICK_POINTS =
struct struct
fun generate (start, finish) = fun generate (start, finish) =
let let
@@ -9,7 +25,6 @@ struct
Vector.tabulate (41, fn idx => (Real32.fromInt idx * increment) + start) Vector.tabulate (41, fn idx => (Real32.fromInt idx * increment) + start)
end end
local
(* (*
* Range to detect from clickable position. * Range to detect from clickable position.
* For example, if we have a clickable position at (x, y) = (500, 500), * For example, if we have a clickable position at (x, y) = (500, 500),
@@ -37,8 +52,7 @@ struct
val curVerticalPos = Vector.sub (yClickPoints, idx) val curVerticalPos = Vector.sub (yClickPoints, idx)
in in
if if
mouseY < curVerticalPos - range mouseY < curVerticalPos - range orelse mouseY > curVerticalPos + range
orelse mouseY > curVerticalPos + range
then then
getVerticalClickPos getVerticalClickPos
( yClickPoints ( yClickPoints
@@ -171,7 +185,7 @@ struct
, windowHeight , windowHeight
) )
end end
in
(* (*
* This function returns a vector containing the position data of the * This function returns a vector containing the position data of the
* clicked square. * clicked square.
@@ -202,4 +216,3 @@ struct
, windowHeight , windowHeight
) )
end end
end

View File

@@ -1,11 +1,17 @@
structure GraphLines = signature GRAPH_LINES =
sig
val generate: int * int * Real32.real vector * Real32.real vector
-> Real32.real vector
end
structure GraphLines :> GRAPH_LINES =
struct struct
local (*
(* This function only produces the desired result * This function only produces the desired result
* when the window is a square and has the aspect ratio 1:1. * when the window is a square and has the aspect ratio 1:1.
* This is because the function assumes it can use * This is because the function assumes it can use
* the same position coordinates both horizontally and vertically. * the same position coordinates both horizontally and vertically.
* *) *)
fun helpGenGraphLinesSquare (pos: Real32.real, limit, acc) = fun helpGenGraphLinesSquare (pos: Real32.real, limit, acc) =
if pos >= limit then if pos >= limit then
Vector.concat acc Vector.concat acc
@@ -21,6 +27,7 @@ struct
, pos + 0.002, 1.0 , pos + 0.002, 1.0
, pos - 0.002, 1.0 , pos - 0.002, 1.0
, pos - 0.002, ~1.0 , pos - 0.002, ~1.0
(* y = _.1 *) (* y = _.1 *)
, ~1.0, pos - 0.002 , ~1.0, pos - 0.002
, ~1.0, pos + 0.002 , ~1.0, pos + 0.002
@@ -54,7 +61,8 @@ struct
helpGenGraphLinesSquare (nextPos, limit, acc) helpGenGraphLinesSquare (nextPos, limit, acc)
end end
fun helpGenGraphLinesHorizontal (pos, xClickPoints, acc, halfWidth, yMin, yMax) = fun helpGenGraphLinesHorizontal
(pos, xClickPoints, acc, halfWidth, yMin, yMax) =
if pos = Vector.length xClickPoints then if pos = Vector.length xClickPoints then
acc acc
else else
@@ -125,7 +133,7 @@ struct
helpGenGraphLinesVertical helpGenGraphLinesVertical
(pos + 1, yClickPoints, acc, halfHeight, xMin, xMax) (pos + 1, yClickPoints, acc, halfHeight, xMin, xMax)
end end
in
fun generate (windowWidth, windowHeight, xClickPoints, yClickPoints) = fun generate (windowWidth, windowHeight, xClickPoints, yClickPoints) =
if windowWidth = windowHeight then if windowWidth = windowHeight then
helpGenGraphLinesSquare (~1.0, 1.0, []) helpGenGraphLinesSquare (~1.0, 1.0, [])
@@ -173,4 +181,3 @@ struct
Vector.concat lines Vector.concat lines
end end
end end
end

View File

@@ -1,10 +1,13 @@
structure Triangles = signature TRIANGLES =
sig
val toVector: AppType.app_type -> Real32.real vector
end
structure Triangles :> TRIANGLES =
struct struct
open AppType open AppType
local fun helpToVector (lst, acc, windowWidth, windowHeight, halfWidth, halfHeight) =
fun helpToVector
(lst, acc, windowWidth, windowHeight, halfWidth, halfHeight) =
case lst of case lst of
{x1, y1, x2, y2, x3, y3} :: tl => {x1, y1, x2, y2, x3, y3} :: tl =>
let let
@@ -30,7 +33,7 @@ struct
(tl, acc, windowWidth, windowHeight, halfWidth, halfHeight) (tl, acc, windowWidth, windowHeight, halfWidth, halfHeight)
end end
| [] => acc | [] => acc
in
fun toVector (app: app_type) = fun toVector (app: app_type) =
let let
val windowWidth = #windowWidth app val windowWidth = #windowWidth app
@@ -43,4 +46,3 @@ struct
Vector.concat lst Vector.concat lst
end end
end end
end