我是刚刚开始学习F#的C#dev,我对单元测试有一些疑问.假设我想要以下代码:
let input () = Console.In.ReadLine()
type MyType= {Name:string; Coordinate:Coordinate}
let readMyType =
input().Split(';')
|> fun x -> {Name=x.[1]; Coordinate = {
Longitude = float(x.[4].Replace(",","."))
Latitude =float(x.[5].Replace(",","."))
}}
Run Code Online (Sandbox Code Playgroud)
您可以注意到,有几点需要考虑:
我认为这样做的方法是:
说实话,我只是在努力找到一个向我展示这个例子的例子,以便学习F#中的语法和其他最佳实践.所以,如果你能告诉我一条非常棒的道路.
提前致谢.
我正在使用 Grid.ColumnDefinition 但百分比似乎无法在 Android 上正常工作,而在 Windows 上却可以正常工作。
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Views.TestMapPage"
Title="TestMap">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="2*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<BoxView Grid.Row="0" Grid.ColumnSpan="3" Color="Blue" />
<BoxView Grid.Row="1" Grid.Column="0" Color="Purple" />
<BoxView Grid.Row="1" Grid.Column="1" Color="Yellow" />
<BoxView Grid.Row="1" Grid.Column="2" Color="Purple" />
<BoxView Grid.Row="2" Grid.ColumnSpan="3" Color="Red" />
</Grid>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)
结果并不如预期(20%,60%,20%)
编辑并回答问题:
我正在处理 ASP Razor 项目 (.Net5)。这是一个带有用户列表和过滤器的页面。目标是向符合过滤条件的用户展示。下面的代码能够进行过滤,但是每次过滤完成时过滤值都会丢失(变成空白)。
我有 [BindProperties],但我不明白为什么它适用于 UserList(表数据)而不适用于过滤条件。
我尝试重新分配这些值,但它不起作用。有谁知道如何解决这个问题?任何指向根本原因和文档页面的指针将不胜感激。我看过https://www.learnrazorpages.com/razor-pages/tempdata但我不确定这是正确的方法。
另外,请毫不犹豫地提供有关代码本身的反馈,因为这实际上是我的第一个 ASP.Net 项目。
用户列表.cshtml
@model Web.Areas.Admin.Pages.UserListModel
@{
ViewData["Title"] = "User list";
}
<h1>User list</h1>
<div>
<form method="post">
<label asp-for="UserNameFilter"></label>
<input type="text" name="UserNameFilter" /> @*This input criteria reset when I click on filter*@
<input type="submit" value="Filter" />
</form>
</div>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>User Name</th>
<th>Email</th>
<th>Password</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var user in Model.UserInfos)
{
<tr>
<td>@user.Id</td>
<td>@user.UserName</td>
<td>@user.Email</td>
<td>
<a asp-page="/UserUpdate" asp-route-id="@user.Id">Edit</a> |
<a asp-page="/Details" asp-route-id="@user.Id">Details</a> …Run Code Online (Sandbox Code Playgroud) 我是F#的新手,我正在尝试使用复杂类型进行简单的模式匹配,但我找不到这样做的方法.请参阅下面的伪代码来解释我想要做的模式匹配.
type Vector= {X:int; Y:int}
let calculateDirection vector =
match vector with
| vector.X=0 && vector.Y>0 -> "N" // pseudo code
| vector.X>0 && vector.Y>0 -> "NE" // pseudo code
| vector.X>0 && vector.Y=0 -> "E" // pseudo code
| vector.X>0 && vector.Y<0 -> "SE" // pseudo code
| vector.X=0 && vector.Y<0 -> "S" // pseudo code
| vector.X<0 && vector.Y<0 -> "SW" // pseudo code
| vector.X<0 && vector.Y=0 -> "W" // pseudo code
| vector.X<0 && vector.Y>0 -> …Run Code Online (Sandbox Code Playgroud) 我是F#的新手,我正在尝试一些事情来解决这个问题.
我必须两种类型几乎相同(坐标和矢量).由于这种类型的推断无法正常工作,我很难在每个函数上指定正确的类型.
它在某种程度上不符合这里的矢量:
type Coordinate = {X:int; Y:int}
type Vector = {X:int; Y:int}
let calculateVector (origin:Coordinate) (destination:Coordinate) = { X=destination.X-origin.X; Y= destination.Y-origin.Y;}
Run Code Online (Sandbox Code Playgroud)
在这里,当我想要一个返回类型的Coordinate时,我找不到如何为此函数指定返回它:
let calculateNextCoordinate (coordinate:Coordinate) direction =
match direction with
| "N" -> { X=coordinate.X; Y=coordinate.Y-1 }
| "NE" -> { X=coordinate.X+1; Y=coordinate.Y-1 }
| "E" -> { X=coordinate.X+1; Y=coordinate.Y }
| "SE" -> { X=coordinate.X+1; Y=coordinate.Y+1 }
| "S" -> { X=coordinate.X; Y=coordinate.Y+1 }
| "SW" -> { X=coordinate.X-1; Y=coordinate.Y+1 }
| "W" -> { X=coordinate.X-1; Y=coordinate.Y } …Run Code Online (Sandbox Code Playgroud) 给定从-273到5526的列表int,我想将最接近的整数打印到零.如果你有平等(n和-n)我们应该拿n.
let temps = // this contains => 1 -2 -8 4 5
let (|Greater|_|) a b = if a > b then Some() else None
let (|Smaller|_|) a b = if a < b then Some() else None
let compareTemperatures a b =
let distanceA = abs a
let distanceB = abs b
match distanceA with
| Greater distanceB -> b
| Smaller distanceB -> a
| _ -> abs a
printfn "%i" (temps |> Seq.reduce compareTemperatures)
Run Code Online (Sandbox Code Playgroud)
并且返回-8而不是1.这对我来说似乎是正确的,我找不到错误,但我是F#的新手,所以我可能在任何地方都犯了一个错误,看不到它:(
提前致谢
我是F#的新手,我正在编写很少的挑战来学习语言的细节.我认为因为不变性而有问题.
场景:我必须在控制台中读取高度线,每行包含一个整数.该整数表示山的大小.阅读输入后我需要写出最高山脉的行号.如果给出的指数是最高的山峰,那么大小设置为零,否则我会松动.重复该场景,直到所有山脉的大小都设置为零.
这里是我写的代码:
open System
type Mountain = {Id:int; Height:int}
let readlineInt() = int(Console.In.ReadLine())
let readMountainData id = {Id = id; Height = readlineInt()}
let readAllMountainsData = [ for a in 0 .. 7 do yield readMountainData a ]
let rec mainLoop () =
let mountains = readAllMountainsData
let highestMountain = mountains |> List.maxBy (fun x -> x.Height)
printfn "%i" highestMountain.Id
mainLoop()
mainLoop()
Run Code Online (Sandbox Code Playgroud)
这段代码将无限循环,我相信这是因为
let readlineInt() = int(Console.In.ReadLine())
Run Code Online (Sandbox Code Playgroud)
是不可变的,所以值设置一次,之后永远不会再次停止读取该行.我尝试将'mutable'关键字用于
let mutable readAllMountainsData = [ for a in 0 .. 7 …Run Code Online (Sandbox Code Playgroud) 从无序的int列表中,我希望两个元素之间的差异最小.我有一个正在运行但速度慢的代码.任何人都可以采取一些改变以改善性能吗?请解释您为何进行更改以及性能提升的原因.
let allInt = [ 5; 8; 9 ]
let sortedList = allInt |> List.sort;
let differenceList = [ for a in 0 .. N-2 do yield sortedList.Item a - sortedList.Item a + 1 ]
printfn "%i" (List.min differenceList) // print 1 (because 9-8 smallest difference)
Run Code Online (Sandbox Code Playgroud)
我想我正在做很多列表创建或迭代,但我不知道如何在F#中以不同的方式编写它...
编辑:我正在列表中测试此代码,包含10万个或更多项目.
编辑2:我相信如果我可以计算出差异并且一分为二,那么它应该会提高很多,但是我不知道如何做到这一点,一个想法?
提前致谢
我刚刚用 .net core 2.0 创建了一个空的 web api 项目。我有一个默认控制器,现在我想创建一个集成测试。
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
Run Code Online (Sandbox Code Playgroud)
目标是集成中的自托管,然后输入 url api/values 并检查返回。
注意:我只使用 wep api 2 和 owin,这很容易做到。但是以下链接:https : //docs.microsoft.com/en-us/aspnet/core/fundamentals/owin说.net core 应用程序不应该使用owin,那么我们应该使用什么以及如何使用呢?
f# ×6
c# ×2
.net ×1
asp.net ×1
immutability ×1
maui ×1
performance ×1
razor-pages ×1
side-effects ×1
unit-testing ×1