我正在尝试创建一个可以创建一个Action的函数来增加传入的整数.但是我的第一次尝试是给出了一个错误"不能在匿名方法体内使用ref或out参数".
public static class IntEx {
public static Action CreateIncrementer(ref int reference) {
return () => {
reference += 1;
};
}
}
Run Code Online (Sandbox Code Playgroud)
我理解为什么编译器不喜欢这个,但是我希望有一个优雅的方法来提供一个可以指向任何整数的漂亮的增量器工厂.我看到这样做的唯一方法如下:
public static class IntEx {
public static Action CreateIncrementer(Func<int> getter, Action<int> setter) {
return () => setter(getter() + 1);
}
}
Run Code Online (Sandbox Code Playgroud)
但当然这对来电者来说更是一种痛苦; 要求调用者创建两个lambda而不是仅仅传入一个引用.有没有更优雅的方式来提供这个功能,或者我只需要忍受双lambda选项?
Clojure中的 - >运算符(以及在Clojure中调用的运算符是什么?)相当于F#中的管道运算符|>?如果是这样,当(|>)被定义为时,为什么它需要这样一个复杂的宏定义
let inline (|>) x f = f x
Run Code Online (Sandbox Code Playgroud)
或者如果没有,F#的管道运算符是否存在于Clojure中,或者您将如何在Clojure中定义这样的运算符?
因此,在C#4.0的悲伤时期,我创建了以下"WorkflowExecutor"类,它允许在GUI线程中通过入侵IEnumerable的"yield return"延续来等待可观察的异步工作流.因此,以下代码将在button1Click处启动一个简单的工作流程来更新文本,等待您单击button2,并在1秒后循环.
public sealed partial class Form1 : Form {
readonly Subject<Unit> _button2Subject = new Subject<Unit>();
readonly WorkflowExecutor _workflowExecutor = new WorkflowExecutor();
public Form1() {
InitializeComponent();
}
IEnumerable<IObservable<Unit>> CreateAsyncHandler() {
Text = "Initializing";
var scheduler = new ControlScheduler(this);
while (true) {
yield return scheduler.WaitTimer(1000);
Text = "Waiting for Click";
yield return _button2Subject;
Text = "Click Detected!";
yield return scheduler.WaitTimer(1000);
Text = "Restarting";
}
}
void button1_Click(object sender, EventArgs e) {
_workflowExecutor.Run(CreateAsyncHandler());
}
void button2_Click(object sender, EventArgs e) {
_button2Subject.OnNext(Unit.Default);
} …Run Code Online (Sandbox Code Playgroud) 我看到了几个变种; ClojureCLR,LSharp,IronScheme,IronLisp等.这些中的任何一个是否已经积极维护和/或接近"成熟",或者它们主要是实验还是灰尘采集者?哪个被认为是最成熟的框架,用于编译.Net dll并引用其他.Net dll,如果有的话?有没有与Visual Studio很好地集成至少一个"创建Lisp项目"功能?
如果我有一个返回的函数IO Bool(特别是一个atomically),有没有办法直接在if语句中使用返回值,而不绑定?
所以目前我已经有了
ok <- atomically $ do
...
if (ok) then do
...
else do
...
Run Code Online (Sandbox Code Playgroud)
是不是可以把它写成类似的东西
if (*some_operator_here* atomically $ do
...) then do
...
else do
...
Run Code Online (Sandbox Code Playgroud)
我希望有一种方法可以使用<-匿名的东西,即,if (<- atomically ...)但到目前为止还没有这样的运气.
类似地,在getLine上,是否可以编写类似的内容
if ((*operator* getLine) == "1234") then do ...
Run Code Online (Sandbox Code Playgroud)
相关附录 - 是什么类型的(<-)?我无法让它出现在ghci中.我假设它是m a -> a,但那意味着它可以在monad之外使用来逃避那个不安全的monad,对吧?是(<-)不是一个功能呢?
我试图检查参数是否是类声明中指定的泛型类型的实例。然而Python似乎不允许这样做。
T = TypeVar('T')
class MyTypeChecker(Generic[T]):
def is_right_type(self, x: Any):
return isinstance(x, T)
Run Code Online (Sandbox Code Playgroud)
这给出了错误'T' is a type variable and only valid in type context。
我刚刚开始使用F#,看看如何使用currying将第一个参数预加载到函数中.但是如何用第二,第三或其他任何参数来做呢?命名参数会使这更容易吗?是否有任何其他功能语言具有命名参数或其他方式使currying对参数顺序无动于衷?
我想用不可变的F#写一大块C#代码.它是一个设备监视器,当前的实现通过不断从串行端口获取数据并根据新数据更新成员变量来工作.我想把它转移到F#并获得不可变记录的好处,但我在概念验证实现中的第一次拍摄真的很慢.
open System
open System.Diagnostics
type DeviceStatus = { RPM : int;
Pressure : int;
Temperature : int }
// I'm assuming my actual implementation, using serial data, would be something like
// "let rec UpdateStatusWithSerialReadings (status:DeviceStatus) (serialInput:string[])".
// where serialInput is whatever the device streamed out since the previous check: something like
// ["RPM=90","Pres=50","Temp=85","RPM=40","Pres=23", etc.]
// The device streams out different parameters at different intervals, so I can't just wait for them all to arrive and aggregate them all …Run Code Online (Sandbox Code Playgroud) 我想main从单个代码库创建一些具有不同入口点的uberjars .我看到你可以指定main命名空间作为参数,lein uberjar但我没有看到指定结果文件名或路径的方法,因此它们只会相互覆盖.有没有办法从命令行覆盖输出文件名或路径?
或者有更好的方法吗?有单独的项目文件,都引用一个中央"库"项目?如果是这样,它的外观结构会是什么以及如何构建它?
后随以不.NET JIT优化空循环走?:
以下程序只运行十亿次空循环并打印出运行时间.我的机器需要700毫秒,我很好奇是否有办法让抖动优化掉空循环.
using System;
namespace ConsoleApplication1 {
class Program {
static void Main() {
var start = DateTime.Now;
for (var i = 0; i < 1000000000; i++) {}
Console.WriteLine((DateTime.Now - start).TotalMilliseconds);
}
}
}
Run Code Online (Sandbox Code Playgroud)
据我所知,答案是否定的,但我不知道是否有隐藏的编译器选项,我可能没试过.我确保在发布模式下编译并在没有附加调试器的情况下运行,但仍然需要700毫秒才能运行此空循环.我也试过NGEN同样的结果(虽然我的理解是它应该生成与JIT相同的编译代码,对吧?).但是我之前从未使用过NGEN,可能使用错了.
它看起来像这将是一些容易的JIT发现和优化掉,但是了解甚少抖动一般是如何工作的,我很好奇,如果有这种优化将遭到冷落具体原因.VC++编译器肯定似乎做了这个优化,所以我想知道为什么会出现这种差异.有任何想法吗?