我经常在F#中使用这个递归的"访客"
let rec visitor dir filter=
seq { yield! Directory.GetFiles(dir, filter)
for subdir in Directory.GetDirectories(dir) do yield! visitor subdir filter}
Run Code Online (Sandbox Code Playgroud)
最近我开始在C#中实现一些F#功能,我试图将其重现为IEnumerable,但是我很难得到更多:
static IEnumerable<string> Visitor(string root, string filter)
{
foreach (var file in Directory.GetFiles(root, filter))
yield return file;
foreach (var subdir in Directory.GetDirectories(root))
foreach (var file in Visitor(subdir, filter))
yield return file;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我必须在C#版本中为递归做一个双重foreach,而不是在F#中... seq {}是否隐含地执行'concat'?
我刚刚安装了最新版本的F#,并打开了一个旧的解决方案,看看它会告诉我什么.
这是一个多文件解决方案,其中第一个文件包含List模块上的一些"扩展函数":
module List =
///Given list of 'rows', returns list of 'columns'
let rec transpose lst =
match lst with
| (_::_)::_ -> List.map List.hd lst :: transpose (List.map List.tl lst)
| _ -> []
Run Code Online (Sandbox Code Playgroud)
编译器不再喜欢这个,并说:
库或多文件应用程序中的文件必须以命名空间或模块声明开头,例如'namespace SomeNamespace.SubNamespace'或'module SomeNamespace.SomeModule'
但如果我这样做:
module Foo.List =
Run Code Online (Sandbox Code Playgroud)
它说:
模块缩写必须是简单名称,而不是路径
我在这里错过了什么?那个'特殊'案例的解决方案是什么,我正在扩展来自其他地方的模块?
我想要的只是一本字典,告诉我它找不到哪个键,而不仅仅是说The given key was not present in the dictionary.
我简单地考虑过做一个子类override new this[TKey key],但觉得它有点hacky,所以我已经实现了IDictionary接口,并将所有内容直接传递给内部Dictionary,并且索引器中只有其他逻辑:
public TValue this[TKey key]
{
get
{
ThrowIfKeyNotFound(key);
return _dic[key];
}
set
{
ThrowIfKeyNotFound(key);
_dic[key] = value;
}
}
private void ThrowIfKeyNotFound(TKey key)
{
if(!_dic.ContainsKey(key))
throw new ArgumentOutOfRangeException("Can't find key [" + key + "] in dictionary");
}
Run Code Online (Sandbox Code Playgroud)
这是正确的/唯一的方法吗?对这个[]的新作真的会那么糟糕吗?
sql server中的作业和维护计划有什么区别?
我很困惑我应该在哪里使用工作和维护计划.
我们使用状态机"框架"(基于状态模式),它不暴露其当前状态,这有时意味着我必须以迂回的方式做事.
当我质疑这个设计决定时,其中一个理由是"如果你需要知道当前的状态,你就错了".
它是否正确?我不是国家机器方面的专家.
(我在这里问,因为我知道我对状态模式有一种固有的偏见,我发现它太冗长了.)
例
想象一个系统,其中一个状态读取两个传感器.一个传感器给出一个数值,另一个给出一个布尔值,它告诉你第一个是否"可靠".系统根据最后n个良好值输出一个值,该值是当前的"好"值或插值(或其他一些奇特的计算).
我的想法是有两个子状态 - 一个'好',另一个'不'.当一个新值到来时,我想询问状态机状态,以便我知道如何处理插值.
(我想我已经回答了我自己的问题:解决方案是NewDataValue(val)在状态机中有一个事件,它只能从'良好'状态转发价值?)
我花了很长时间才弄清楚为什么这段代码对某些网址"悬空":
let getImage (imageUrl:string) =
async {
try
let req = WebRequest.Create(imageUrl) :?> HttpWebRequest
req.UserAgent <- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
req.Method <- "GET";
req.AllowAutoRedirect <- true;
req.MaximumAutomaticRedirections <- 4;
req.Timeout <- 3000; //HAHAHA, nice try!
let! response1 = req.AsyncGetResponse()
let response = response1 :?> HttpWebResponse
use stream = response.GetResponseStream()
let ms = new MemoryStream()
let bytesRead = ref 1
let buffer = Array.create 0x1000 0uy
while !bytesRead > 0 do
bytesRead := stream.Read(buffer, …Run Code Online (Sandbox Code Playgroud) 我有一个Win 8.1托管操作系统,运行VS2013,可以在模拟器中测试我的WinPhone应用程序.主机操作系统是Win7 Pro.
我重新安装了Win10和VS2015的托管操作系统,现在Hypervisor not enabled当我尝试使用模拟器测试我的应用程序时出现错误:
我已经尝试按照以下方式禁用和重新启用HyperV(在托管操作系统中):
但无济于事.
我也尝试打开托管计算机的BIOS设置(PhoenixBIOS),但我没有看到任何可以帮助的选项.
还有什么我可以尝试让它工作?
(为什么我为什么不首先备份VM?!)
我有一个非一次性类,我希望能够使用Open/Close语法use,所以我试图继承它,并将Open打开到newClose to Dispose.
第二部分没问题,但我无法解决如何进行Open:
type DisposableOpenCloseClass(openargs) =
inherit OpenCloseClass()
//do this.Open(openargs) <-- compiler no like
interface IDisposable
with member this.Dispose() = this.Close()
Run Code Online (Sandbox Code Playgroud)
(参考我很久以前问过的这个问题,但我不能加入这个问题)
这个问题结合了我不完全理解的两个主题
通过阅读有关F#中异步的文章,我遇到了Agent/MailboxProcessors的主题,它可用于实现反应状态机.是否可以使用C#5中的新异步/等待功能来实现C#中类似的功能,或者是否已经存在更适合的类似模拟?
我想尝试这个自托管Web服务的例子(最初用WCF WebApi编写),但是使用新的ASP.NET WebAPI(它是WCF WebApi的后代).
using System;
using System.Net.Http;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Microsoft.ApplicationServer.Http;
namespace SampleApi {
class Program {
static void Main(string[] args) {
var host = new HttpServiceHost(typeof (ApiService), "http://localhost:9000");
host.Open();
Console.WriteLine("Browse to http://localhost:9000");
Console.Read();
}
}
[ServiceContract]
public class ApiService {
[WebGet(UriTemplate = "")]
public HttpResponseMessage GetHome() {
return new HttpResponseMessage() {
Content = new StringContent("Welcome Home", Encoding.UTF8, "text/plain")
};
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,要么我没有NuGotten正确的包,要么HttpServiceHost是AWOL.(我选择了'自托管'变体).
我错过了什么?
f# ×5
c# ×4
agent ×1
async-await ×1
asynchronous ×1
c#-to-f# ×1
coding-style ×1
constructor ×1
dictionary ×1
hypervisor ×1
namespaces ×1
sql ×1
sql-server ×1
syntax ×1
vmware ×1
wcf-web-api ×1