Remove unnecessary F# match expressions and option wrapping

This commit is contained in:
Adam Liddell 2022-08-24 16:11:34 +01:00
parent df5acb850e
commit 406b13653c
3 changed files with 71 additions and 90 deletions

View File

@ -14,17 +14,15 @@ type Client(client: RouteGuide.RouteGuide.RouteGuideClient) =
printfn "*** GetFeature: lat=%i lon=%i" lat lon
let request : RouteGuide.Point =
{ Latitude = ValueSome(lat)
Longitude = ValueSome(lon)
{ Latitude = lat
Longitude = lon
_UnknownFields = null }
let feature : RouteGuide.Feature = client.GetFeature(request)
match (feature.Name, feature.Location) with
| (ValueSome (name), ValueSome (location)) ->
match (location.Latitude, location.Longitude) with
| (ValueSome (lat), ValueSome (lon)) -> printfn "Found feature called \"%s\" at %i, %i" name lat lon
| _ -> printfn "Point did not have coordinates"
| (name, ValueSome (location)) ->
printfn "Found feature called \"%s\" at %i, %i" name location.Latitude location.Longitude
| _ -> printfn "Found no feature at %i, %i" lat lon
with :? RpcException as ex -> printfn "Rpc failed: %s" ex.Message
@ -36,14 +34,14 @@ type Client(client: RouteGuide.RouteGuide.RouteGuideClient) =
let request : RouteGuide.Rectangle =
{ Lo =
ValueSome(
{ Latitude = ValueSome(lowLat)
Longitude = ValueSome(lowLon)
{ Latitude = lowLat
Longitude = lowLon
_UnknownFields = null }
)
Hi =
ValueSome(
{ Latitude = ValueSome(hiLat)
Longitude = ValueSome(hiLon)
{ Latitude = hiLat
Longitude = hiLon
_UnknownFields = null }
)
_UnknownFields = null }
@ -85,22 +83,17 @@ type Client(client: RouteGuide.RouteGuide.RouteGuideClient) =
match point with
| ValueSome (p) ->
match (p.Latitude, p.Longitude) with
| (ValueSome (lat), ValueSome (lon)) ->
Some(
async {
printfn "Visiting point %i, %i" lat lon
Some(
async {
printfn "Visiting point %i, %i" p.Latitude p.Longitude
call.RequestStream.WriteAsync(p)
|> Async.AwaitTask
|> ignore
call.RequestStream.WriteAsync(p)
|> Async.AwaitTask
|> ignore
do! Async.Sleep 1000
}
)
| _ ->
printfn "%s" "Point did not have latitude and longitude"
None
do! Async.Sleep 1000
}
)
| _ ->
printfn "%s" "Feature did not have a point"
None)
@ -114,15 +107,12 @@ type Client(client: RouteGuide.RouteGuide.RouteGuideClient) =
let! summary = call.ResponseAsync |> Async.AwaitTask
match (summary.PointCount, summary.FeatureCount, summary.Distance, summary.ElapsedTime) with
| (ValueSome (p), ValueSome (f), ValueSome (d), ValueSome (e)) ->
printfn
"Finished trip with %i points. Passed %i features. Travelled %i meters. It took %i seconds."
p
f
d
e
| _ -> printfn "%s" "Summary did not include full details"
printfn
"Finished trip with %i points. Passed %i features. Travelled %i meters. It took %i seconds."
summary.PointCount
summary.FeatureCount
summary.Distance
summary.ElapsedTime
printfn "%s" "Finished RecordRoute"
@ -136,35 +126,35 @@ type Client(client: RouteGuide.RouteGuide.RouteGuideClient) =
printfn "%s" "*** RouteChat"
let requests : RouteGuide.RouteNote list =
[ { Message = ValueSome("First message")
[ { Message = "First message"
Location =
ValueSome(
{ Longitude = ValueSome(0)
Latitude = ValueSome(0)
{ Longitude = 0
Latitude = 0
_UnknownFields = null }
)
_UnknownFields = null }
{ Message = ValueSome("Second message")
{ Message = "Second message"
Location =
ValueSome(
{ Longitude = ValueSome(0)
Latitude = ValueSome(1)
{ Longitude = 0
Latitude = 1
_UnknownFields = null }
)
_UnknownFields = null }
{ Message = ValueSome("Third message")
{ Message = "Third message"
Location =
ValueSome(
{ Longitude = ValueSome(1)
Latitude = ValueSome(0)
{ Longitude = 1
Latitude = 0
_UnknownFields = null }
)
_UnknownFields = null }
{ Message = ValueSome("Fourth message")
{ Message = "Fourth message"
Location =
ValueSome(
{ Longitude = ValueSome(0)
Latitude = ValueSome(0)
{ Longitude = 0
Latitude = 0
_UnknownFields = null }
)
_UnknownFields = null } ]

View File

@ -16,64 +16,49 @@ let private coordFactor : double = 1e7
let toRadians (value: double) : double = (Math.PI / (180 |> double)) * value
let getLatitude (point: Point) =
match point.Latitude with
| ValueSome (l) -> (l |> double) / coordFactor
| ValueNone -> 0.
let getLatitude (point: Point) = (point.Latitude |> double) / coordFactor
let getDistance (start: Point) (``end``: Point) : double =
match (start.Latitude, start.Longitude, ``end``.Latitude, ``end``.Longitude) with
| (ValueSome (slat), ValueSome (slon), ValueSome (elat), ValueSome (elon)) ->
let r = 6371000.
let lat1 = toRadians (slat |> double)
let lat2 = toRadians (elat |> double)
let lon1 = toRadians (slon |> double)
let lon2 = toRadians (elon |> double)
let deltalat = lat2 - lat1
let deltalon = lon2 - lon1
let r = 6371000.
let lat1 = toRadians (start.Latitude |> double)
let lat2 = toRadians (``end``.Latitude |> double)
let lon1 = toRadians (start.Longitude |> double)
let lon2 = toRadians (``end``.Longitude |> double)
let deltalat = lat2 - lat1
let deltalon = lon2 - lon1
let a =
Math.Sin(deltalat / 2.) * Math.Sin(deltalat / 2.)
+ Math.Cos(lat1)
* Math.Cos(lat2)
* Math.Sin(deltalon / 2.)
* Math.Sin(deltalon / 2.)
let a =
Math.Sin(deltalat / 2.) * Math.Sin(deltalat / 2.)
+ Math.Cos(lat1)
* Math.Cos(lat2)
* Math.Sin(deltalon / 2.)
* Math.Sin(deltalon / 2.)
let c =
2. * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1. - a))
r * c
| _ -> 0.
let c =
2. * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1. - a))
r * c
let contains (rectangle: Rectangle) (point: Point) =
match (rectangle.Lo, rectangle.Hi) with
| (ValueSome (reclo), ValueSome (rechi)) ->
match (reclo.Latitude, reclo.Longitude, rechi.Latitude, rechi.Latitude, point.Longitude, point.Latitude) with
| (ValueSome (lolat),
ValueSome (lolon),
ValueSome (hilat),
ValueSome (hilon),
ValueSome (plon),
ValueSome (plat)) ->
let left = Math.Min(lolon, hilon)
let right = Math.Max(lolon, hilon)
let top = Math.Max(lolat, hilat)
let bottom = Math.Min(lolat, hilat)
| (ValueSome (recLo), ValueSome (recHi)) ->
let left = Math.Min(recLo.Longitude, recHi.Longitude)
let right = Math.Max(recLo.Longitude, recHi.Longitude)
let top = Math.Max(recLo.Latitude, recHi.Latitude)
let bottom = Math.Min(recLo.Latitude, recHi.Latitude)
(plon >= left
&& plat <= right
&& plat >= bottom
&& plat <= top)
| _ -> false
(point.Longitude >= left
&& point.Longitude <= right
&& point.Latitude >= bottom
&& point.Latitude <= top)
| _ -> false
let jsonFeatureToProtoFeature (jsonFeature: JsonFeature) : Feature =
{ Name = ValueSome(jsonFeature.Name)
{ Name = jsonFeature.Name
Location =
ValueSome(
{ Latitude = ValueSome(jsonFeature.Location.Latitude)
Longitude = ValueSome(jsonFeature.Location.Longitude)
{ Latitude = jsonFeature.Location.Latitude
Longitude = jsonFeature.Location.Longitude
_UnknownFields = null }
)
_UnknownFields = null }

View File

@ -82,7 +82,13 @@ module Server =
stopwatch.Stop()
let summary: RouteGuide.RouteSummary = { PointCount = ValueSome(pointCount); FeatureCount = ValueSome(featureCount); Distance = ValueSome(distance); ElapsedTime = ValueSome(((stopwatch.ElapsedMilliseconds / int64(1000)) |> int)); _UnknownFields = null}
let summary: RouteGuide.RouteSummary = {
PointCount = pointCount;
FeatureCount = featureCount;
Distance = distance;
ElapsedTime = ((stopwatch.ElapsedMilliseconds / int64(1000)) |> int);
_UnknownFields = null
}
return summary
} |> Async.StartAsTask