我一直在逐渐学习Haskell,甚至觉得我有一堆monad.然而,仍然有很多我不太理解的异国情调,比如Arrows,Applicative等.虽然我从Haskell代码中找到了点点滴滴,但我找到了一个真正解释的教程会很好他们完全.(似乎有几十个关于monad的教程......但是之后一切似乎都完成了!)
我要检查一个IEnumerable包含正好一个元素.这个片段确实有效:
bool hasOneElement = seq.Count() == 1
Run Code Online (Sandbox Code Playgroud)
然而,它并不是非常有效,因为Count()它将枚举整个列表.显然,知道列表是空的或包含多于1个元素意味着它不是空的.是否存在具有此短路行为的扩展方法?
我一直在玩Haskell,包括练习以无点形式编写函数.这是一个示例函数:
dotProduct :: (Num a) => [a] -> [a] -> a
dotProduct xs ys = sum (zipWith (*) xs ys)
Run Code Online (Sandbox Code Playgroud)
我想以无点形式编写这个函数.这是我在其他地方找到的一个例子:
dotProduct = (sum .) . zipWith (*)
Run Code Online (Sandbox Code Playgroud)
但是,我不明白为什么无点形式看起来像(sum .) . zipWith (*)而不是sum . zipWith (*).为什么括号中的和有2个组合运算符?
给定两个列表,我可以生成这两个列表的笛卡尔积的所有排列的列表:
permute :: [a] -> [a] -> [[a]]
permute xs ys = [ [x, y] | x <- xs, y <- ys ]
Example> permute [1,2] [3,4] == [ [1,3], [1,4], [2,3], [2,4] ]
Run Code Online (Sandbox Code Playgroud)
如何扩展置换,以便不使用两个列表,而是获取列表的列表(长度为n)并返回列表列表(长度为n)
permute :: [[a]] -> [[a]]
Example> permute [ [1,2], [3,4], [5,6] ]
== [ [1,3,5], [1,3,6], [1,4,5], [1,4,6] ] --etc
Run Code Online (Sandbox Code Playgroud)
我在Hoogle上找不到任何相关的东西..唯一与签名相匹配的功能是transpose,它不会产生所需的输出.
我的Haskell项目包括一个表达式求值程序,为了这个问题的目的可以简化为:
data Expression a where
I :: Int -> Expression Int
B :: Bool -> Expression Bool
Add :: Expression Int -> Expression Int -> Expression Int
Mul :: Expression Int -> Expression Int -> Expression Int
Eq :: Expression Int -> Expression Int -> Expression Bool
And :: Expression Bool -> Expression Bool -> Expression Bool
Or :: Expression Bool -> Expression Bool -> Expression Bool
If :: Expression Bool -> Expression a -> Expression a -> Expression a …Run Code Online (Sandbox Code Playgroud) 我正在使用MVVM模式构建一个小型计时应用程序,使用实体框架进行持久化.在这个阶段,我的逻辑很薄,因为我只需要对相关数据执行一些计算和聚合.目前,我已经通过在实体类的部分类中编写它们来实现这些.
例如:
// entity framework generated
partial class Lap {
int Id { /* boilerplate */ }
DateTime StartTime { /* etc */ }
DateTime EndTime { /* etc */ }
}
// in my partial class (written by me)
partial class Lap {
TimeSpan Duration {
get { return EndTime - StartTime; }
}
}
Run Code Online (Sandbox Code Playgroud)
将额外的逻辑直接放到实体生成的类上是不好的做法吗?我应该为这个逻辑创建另一个域层吗?
我最近一直在教自己LINQ并将它应用于各种小谜题.但是,我遇到的一个问题是LINQ-to-objects只适用于泛型集合.是否有将非泛型集合转换为泛型集合的秘密技巧/最佳实践?
我当前的实现将非泛型集合复制到一个数组然后操作,但我想知道是否有更好的方法?
public static int maxSequence(string str)
{
MatchCollection matches = Regex.Matches(str, "H+|T+");
Match[] matchArr = new Match[matches.Count];
matches.CopyTo(matchArr, 0);
return matchArr
.Select(match => match.Value.Length)
.OrderByDescending(len => len)
.First();
}
Run Code Online (Sandbox Code Playgroud) 这更像是一个C#语法问题,而不是一个需要解决的实际问题.假设我有一个将委托作为参数的方法.假设我定义了以下方法:
void TakeSomeDelegates(Action<int> action, Func<float, Foo, Bar, string> func)
{
// Do something exciting
}
void FirstAction(int arg) { /* something */ }
string SecondFunc(float one, Foo two, Bar three){ /* etc */ }
Run Code Online (Sandbox Code Playgroud)
现在,如果我想TakeSomeDelegates用FirstAction和SecondFunc作为参数调用,据我所知,我需要做这样的事情:
TakeSomeDelegates(x => FirstAction(x), (x,y,z) => SecondFunc(x,y,z));
Run Code Online (Sandbox Code Playgroud)
但是有没有更方便的方法来使用适合所需委托签名的方法而无需编写lambda?理想情况下TakeSomeDelegates(FirstAction, SecondFunc),虽然显然不能编译.
我有这个功能(产生斐波纳契序列):
unfoldr (\(p1, p2) -> Just (p1+p2, (p1+p2, p1)) ) (0, 1)
Run Code Online (Sandbox Code Playgroud)
在这里,我注意到一个重复的表达式,p1+p2我想要考虑它,以便它只计算一次.添加本身并不是一个昂贵的计算,但对于更通用的版本:
unfoldr (\(p1, p2) -> Just (f p1 p2, (f p1 p2, p1)) ) (0, 1)
where f = arbitrary, possibly time-consuming function
Run Code Online (Sandbox Code Playgroud)
在上面的情况下,f p1 p2计算两次(除非有一些神奇的编译器优化我不知道),如果f需要大量计算,这可能会产生性能瓶颈.我无法考虑f p1 p2到where因为p1而且p2不在范围内.将此表达式f计算在内的最佳方法是什么,只计算一次?
我想编写一个读取文件的函数,并计算每个单词出现的次数.假设处理文件读取并生成表示文件中每一行的字符串列表,我需要一个函数来计算每个单词的出现次数.首先,是使用Dictionary<string,int>最好的方法?关键是单词,值是该单词的出现次数.
我编写了这个函数,它遍历每一行和一行中的每个单词并构建一个字典:
static IDictionary<string, int> CountWords(IEnumerable<string> lines)
var dict = new Dictionary<string, int>();
foreach (string line in lines)
{
string[] words = line.Split(' ');
foreach (string word in words)
{
if (dict.ContainsKey(word))
dict[word]++;
else
dict.Add(word, 1);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我想以某种方式编写这个函数..功能上,使用LINQ(因为LINQ很有趣,我正在努力提高我的函数编程技能:D)我设法得出这个表达式,但我不确定是否是在功能上做到这一点的最佳方式:
static IDictionary<string, int> CountWords2(IEnumerable<string> lines)
{
return lines
.SelectMany(line => line.Split(' '))
.Aggregate(new Dictionary<string, int>(),
(dict, word) =>
{
if (dict.ContainsKey(word))
dict[word]++;
else
dict.Add(word, 1);
return dict;
});
}
Run Code Online (Sandbox Code Playgroud)
因此,虽然我有两个有效的解决方案,但我也有兴趣了解这个问题的最佳方法.有兴趣了解LINQ和FP的人吗?
{-# LANGUAGE RankNTypes #-}
Run Code Online (Sandbox Code Playgroud)
继续前一系列问题,我有一个带有通用量化函数作为参数的函数,如下所示:
emap :: (forall a. Expression a -> Expression a) -> Expression b -> Expression b
Run Code Online (Sandbox Code Playgroud)
对于不需要额外约束的函数,可以使用哪个,例如:
postmap :: (forall a. Expression a -> Expression a) -> Expression b -> Expression b
postmap f = f . emap (postmap f)
reduce = postmap step
Run Code Online (Sandbox Code Playgroud)
但是,我现在想要将此函数与带有附加约束的函数一起使用,但以下内容不进行类型检查.
substitute :: Pack a => Identifier -> a -> Expression a -> Expression a
substitute i v (Var x) | x == i = pack v
substitute _ _ …Run Code Online (Sandbox Code Playgroud) 这有什么区别:
public class Foo {
private Bar bar;
public Foo() { bar = new Bar(); }
}
Run Code Online (Sandbox Code Playgroud)
还有这个:
public class Foo {
private Bar bar = new Bar();
public Foo() { }
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试Connection创建一个对象的子类AuthenticatedConnection,它应该以与a相同的方式运行Connection,并添加一个令牌.一个简单的解决方案是通过子类构造函数传递'wrap'连接.
这个解决方案似乎坏了,因为我无法访问参数的受保护成员.这意味着无法调用基础构造函数.有没有办法实现构造函数,以便可以像这样包装构造函数?例如:new AuthenticatedConnection("token", existingConnection)
这是当前(破碎)的实现.编译错误是:Cannot access protected member 'Connection._client' via a qualifier of type 'Connection'; the qualifier must be of type 'AuthenticatedConnection' (or derived from it)
class Connection
{
// internal details of the connection; should not be public.
protected object _client;
// base class constructor
public Connection(object client) { _client = client; }
}
class AuthenticatedConnection : Connection
{
// broken constructor?
public AuthenticatedConnection(string token, Connection connection)
: base(connection._client)
{
// …Run Code Online (Sandbox Code Playgroud) c# ×6
haskell ×6
linq ×2
constructor ×1
delegates ×1
evaluation ×1
generics ×1
inheritance ×1
lambda ×1
mvvm ×1
performance ×1
pointfree ×1
syntax ×1