在C#中实现monad的最佳方法是什么?是否存在特定的实施策略,或者每个monad是否与另一个不同?
在 C# 中,如何使用 LINQ 过滤 SortedDictionary 生成一个也是 SortedDictionary 的子集?例如。我想写
SortedDictionary<int, Person> source = ..fetch..
SortedDictionary<int, Person> filtered = source.Where(x=>x.foo == bar)
Run Code Online (Sandbox Code Playgroud)
我发现的唯一方法是创建一个辅助方法并使用它
SortedDictionary<TKey, TValue> SubDictionary<TKey, TValue> IEnumerable<KeyValuePair<TKey, TValue>> l)
{
SortedDictionary<TKey, TValue> result = new SortedDictionary<TKey, TValue>();
foreach (var e in l)
result[e.Key] = e.Value;
return result;
}
...
SortedDictionary<int, Person> source = ..fetch..
SortedDictionary<int, Person> filtered = SubDictionary(source.Where(x=>x.foo == bar))
Run Code Online (Sandbox Code Playgroud) 如何使用github api为我没有检出的项目创建github pull请求.我想自动化对存储库的简单更改,我不需要先检查.我只想从raw.githubusercontent.com中提取文件,修改它并创建所述拉取请求.
编辑 github Web用户界面允许通过克隆和在后台创建拉取请求来编辑外部文件.
为什么await在 a 里面做一个线程不安全.ForEach()?
在 .Net Core 3.1 项目中,我从一些 WebApi 中选择符合条件的用户列表,然后删除它们,最后再次询问用户列表。当然,这次我希望列表为空。
该错误代码
var existing = (await client.GetByMatchAsync(new SearchParameters() {..})).ToList();
existing.ForEach(async x => await client.DeleteByIdAsync(x.Id));
var ensure = (await client.GetByMatchAsync(new SearchParameters() {..})).ToList();
ensure.Count.Should().Be(0); <-- ERROR WAS 1!
Run Code Online (Sandbox Code Playgroud)
我发现当我在代码工作Thread.Sleep(50)之前插入 a 时var ensure。这清楚地向我表明存在线程问题,我不明白。
的工作码(使用延迟)
var existing = (await client.GetByMatchAsync(new SearchParameters() {..})).ToList();
existing.ForEach(async x => await client.DeleteByIdAsync(x.Id));
Thread.Sleep(50);
var ensure = (await client.GetByMatchAsync(new SearchParameters() {..})).ToList();
ensure.Count.Should().Be(0);
Run Code Online (Sandbox Code Playgroud)
替代工作代码(使用 foreach)
var existing = …Run Code Online (Sandbox Code Playgroud) 在较旧版本的MOQ中,以下代码可以正常工作(例如3.1.416.3).它不再了.如何将模拟更改为工作?问题是接口继承了另一个接口并重新定义了一个字段.因此我的模拟现在需要配置两个字段的值,因为这在我Code1()和Code2()调用中使用
public interface I
{
string field { get; }
}
public interface IModi : I
{
new string field { get; set; }
}
public class P : IModi
{
private string val;
string I.field
{
get { return val; }
}
public string field
{
get { return val; }
set { val = value; }
}
public static void Code1(I p)
{
Console.WriteLine(p.field);
}
public static void Code2(IModi p)
{
Code1(p);
Console.WriteLine(p.field);
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试理解如何应用来自Haskel的Maybe-idiom ..我正在阅读http://en.wikibooks.org/wiki/Haskell/Understanding_monads/Maybe,它表明在字典中查找可能会返回a Maybe和该值通过>>=运营商提出.
来自URL的示例:
如果我们想在第三次查询中使用政府数据库查询的结果(比如我们想要查看他们的注册号以查看他们是否欠任何汽车税),那么我们可以扩展我们的getRegistrationNumber函数:
getTaxOwed :: String -- their name
-> Maybe Double -- the amount of tax they owe
getTaxOwed name =
lookup name phonebook >>=
(\number -> lookup number governmentalDatabase) >>=
(\registration -> lookup registration taxDatabase)
Run Code Online (Sandbox Code Playgroud)
或者,使用do-block样式:
getTaxOwed name = do
number <- lookup name phonebook
registration <- lookup number governmentalDatabase
lookup registration taxDatabase
Run Code Online (Sandbox Code Playgroud)
题:
我该如何处理错误处理?我认为大多数代码都会从告诉出错的地方中受益.它不应仅报告"无法在电话簿或政府数据库中找到John Doe",而应报告哪个资源存在问题.
在我的表达式树(使用Expression类创建)中,我需要将我的代码包装在一个未经检查的块中(http://msdn.microsoft.com/en-us/library/a569z7k8.aspx)但是如何?这种Expression类型没什么有趣的.
我有5个具有以下ID的div;
- slide0
- slide1
- slide2
- slide3
- slide4
Run Code Online (Sandbox Code Playgroud)
我想使用变量和id选择它们.
var clk = 0;
Run Code Online (Sandbox Code Playgroud)
每次运行该函数时,clk都会递增.
$('slide' + clk).fadeOut()< - 我如何使这项工作?
我是jQuery的新手,在javascript中它会如此简单;
document.getElementById('slide' + clk).style
c# ×4
linq ×2
monads ×2
async-await ×1
git ×1
github ×1
github-api ×1
haskell ×1
html ×1
javascript ×1
jquery ×1
maybe ×1
moq ×1
unit-testing ×1