我正在使用Visual Studio Professional 2015,并且安装了NUnit测试适配器的2.0.0.0版本.
它没有发现构建以下代码的任何测试:
namespace SmallestDivisibleIntegers
module Core =
let f n = [2..4] |> List.map (fun x -> x + n - n % x)
module Tests =
open FsUnit
open NUnit.Framework
open Core
[<Test>]
let ``Correct answers`` () =
f 1 |> should equal [2; 3; 4]
f 4 |> should equal [6; 6; 8]
f 43 |> should equal [44; 45; 44]
f 123 |> should equal [124; 126; 124]
f 420 |> should equal [422; …Run Code Online (Sandbox Code Playgroud) 我无法弄清楚为什么下面的代码挂在调用GetTotal.我似乎无法在MailboxProcessor内部进行调试,因此很难看到发生了什么.
module Aggregator
open System
type Message<'T, 'TState> =
| Aggregate of 'T
| GetTotal of AsyncReplyChannel<'TState>
type Aggregator<'T, 'TState>(initialState, f) =
let myAgent = new MailboxProcessor<Message<'T, 'TState>>(fun inbox ->
let rec loop agg =
async {
let! message = inbox.Receive()
match message with
| Aggregate x -> return! loop (f agg x)
| GetTotal replyChannel ->
replyChannel.Reply(agg)
return! loop agg
}
loop initialState
)
member m.Aggregate x = myAgent.Post(Aggregate(x))
member m.GetTotal = myAgent.PostAndReply(fun replyChannel -> GetTotal(replyChannel))
let …Run Code Online (Sandbox Code Playgroud) 如果我有一个无限的序列,我就不能使用法线Seq.tryFind.
但是,如果序列是有序的,那意味着当我检测到序列中没有其他元素可以满足我的条件时,可以取消搜索.
是否有一种优雅的方式来表达这样的搜索?
我正在尝试在Fable项目中使用NPM包规范化轮.
我已确认该软件包已下载到该node-modules文件夹中.
type INormalizedWheel =
abstract member pixelX: float
abstract member pixelY: float
abstract member spinX: float
abstract member spinY: float
[<Import("normalizeWheel", "normalize-wheel")>]
let normalizeWheel (we: React.WheelEvent) : INormalizedWheel = jsNative
...
let x = normalizeWheel wheelEvent
Run Code Online (Sandbox Code Playgroud)
但是,每当到达最后一行时,都会"Object(...) is not a function"抛出JS错误消息.在调试器中,类型normalizeWheel显示为undefined.
我猜导入不起作用,但我做错了什么?
我创建了一个应用,可以下载一些数据并使用Google图表将其绘制到div具有特定的Id。
在静态页面上可以正常工作。但是,当使用菜单在多个视图之间切换时,当切换回适当的视图时,我很难重新绘制图表。
如果在更新模型时使用命令重绘图表,则该请求将失败,因为div尚未呈现该特定视图。
完成View阶段后,Model-View-Update有没有办法触发命令?还是重绘图表的更好方法?
后面是示例代码(不可运行,但应易于理解)。单击“获取数据”按钮可以正常工作,但是如果您随后又切换到另一页,我将找不到重新绘制图表的方法。
module Client
open Elmish
open Elmish.React
open Fable.Helpers.React
open Fable.Helpers.React.Props
open Fulma
type View = ShowChart | NoChart
type Model = { View: View; Values: float[] option }
type Msg =
| RequestValues
| ReceiveValues of float[]
| PageWithChart
| PageWithoutChart
| DrawChart
let init () : Model * Cmd<Msg> =
{ View = ShowChart; Values = None }, []
let update (msg : Msg) …Run Code Online (Sandbox Code Playgroud) 我有一个处理大量各种类型数据的通用方法。
当数据是特定类型时(在本例中),我想应用一些额外的处理double。不同类型的所有其他功能都是相同的。
有没有比下面示例中所示的(非常慢)装箱/拆箱更好的方法?
这似乎意味着我们需要让编译器相信T和double是相同的类型,在if..else断言这一点的部分中。
public static T[] HalfIfDouble<T>(T[] data)
{
T[] result;
if (typeof(T) == typeof(double))
{
// result = data.Select(x => x * 0.5).ToArray(); // does not compile
result = data.Select(x =>
{
double d = (double)(object)x;
return (T)(object)(d * 0.5);
}).ToArray();
}
else
{
result = data;
}
// do some more processing...
return result;
}
Run Code Online (Sandbox Code Playgroud)
实际功能显然比这个例子更复杂。
需要在 .NET Framework 和 .NET 6 中工作。
我是F#的新手,但是当我使用详细语法将for循环放入另一个时,它将无法编译:
#light "off"
let Multiple9x9 () =
for i in 1 .. 9 do
printf "\n";
for j in 1 .. 9 do
let k = i * j;
printf "%d x %d = %2d " i j k ;
done;;
done;;
Multiple9x9 ();;
printf "\n" ;;
Run Code Online (Sandbox Code Playgroud)
我知道它可以与#light"on"一起使用:
let Multiple9x9 () =
for i in 1 .. 9 do
printf "\n"
for j in 1 .. 9 do
let k = i * j
printf "%d x %d = %2d …Run Code Online (Sandbox Code Playgroud) f# for-loop functional-programming nested visual-studio-2012
我没什么问题.我需要在我的应用程序中连接互联网上的图像,但我遇到错误.
拒绝加载图片'http // testdomain/test_img.jpg',因为它违反了以下内容安全策略指令:"img-src'self'data:chrome-extension-resource:".
{
"name": "TEST",
"description": "TEST for TEST",
"version": "0.1",
"app": {
"background": {
"scripts": ["background.js"]
}
},
"permissions": [
"storage",
"fullscreen",
],
"content_security_policy": "img-src 'self' data: chrome-extension-resource:;",
"icons": {"128": "icon.png" }
}
Run Code Online (Sandbox Code Playgroud)
<html><img src="http://testdomain/test_img.jpg"></html>
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
let funcsAppliedToData data = funcs |> Seq.map (fun f -> f data)
是否有一个运算符来表达括号中定义的功能(或者说整齐地写整行)?
以下函数在我尝试匹配空列表时给出了编译错误:
let rec tuplesToList (acc: int list) (remaining: int*int list) =
match remaining with
| [] -> acc
| (a, b) :: tail -> tuplesToList (a :: b :: acc)
Run Code Online (Sandbox Code Playgroud)
错误是:
This expression was expected to have type int * int list but here has type 'a list
Run Code Online (Sandbox Code Playgroud)
当remaining一个简单的ints 列表而不是元组时,这很好用.如何匹配空元组列表?
我正在使用Umbraco CMS版本7的网站上工作.我正在使用NWebSec在网站上实现CSP标头.NWebSec具有内置功能,可在发生CSP违规时引发.Net事件.通常你会用这样的事情来抓住那个事件:
protected void NWebSecHttpHeaderSecurityModule_CspViolationReported(object sender, CspViolationReportEventArgs e)
{
var report = e.ViolationReport;
var serializedReport = JsonConvert.SerializeObject(report.Details);
// Do a thing with the report
}
Run Code Online (Sandbox Code Playgroud)
在Global.asax.cs文件中.但据我所知,Umbraco抢占了Global.asax.cs文件,它可以播放任何被抛出的事件.我有一个文件,其中包含一些自定义事件处理程序,如:
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
Run Code Online (Sandbox Code Playgroud)
处理通常在Global.asax.cs文件中的标准应用程序启动代码,但是将NWebSec事件处理程序放在同一个文件中并不起作用.大概是因为它使用.Net事件处理程序语法而不是Umbraco替换它的任何东西.
如何访问NWebSec抛出的事件?
给定2个元素集合S1和S2,计算3个集合(A,B,C)的最有效方法是什么,其中A是S1和B的唯一元素,S2和C的唯一元素是S1的常见项目和S2.
我正在编写一个函数,它接受一个PropertyInfo对象,获取MyAttribute并返回一个MyAttribute对象:
let getparamattribute(p : PropertyInfo) =
let attr = p.GetCustomAttribute (typeof<MyAttribute>, true)
attr :? MyAttribute
Run Code Online (Sandbox Code Playgroud)
但是FSI将返回类型显示为bool:
val getparamattribute : (PropertyInfo -> bool)
Run Code Online (Sandbox Code Playgroud)
为什么?