相关疑难解决方法(0)

使用LINQ将集合拆分为`n`部分?

有没有一种很好的方法可以n使用LINQ 将集合拆分为多个部分?当然不一定均匀.

也就是说,我想将集合划分为子集合,每个子集合包含元素的子集,其中最后一个集合可以是不规则的.

.net c# linq data-structures

121
推荐指数
6
解决办法
7万
查看次数

C#中的管道转发

继续我在C#中表达F#想法的调查,我想要一个管道前进算子.对于包含在IEnumerable中的任何东西,我们已经拥有它,因为你可以.NextFunc()到你心中的内容.但是,例如,如果最后有任何类似折叠的缩减,则无法将其结果输入函数.

这里有两种扩展方法,我想知道是否有其他人试过这个,如果这是个好主意(编辑:现在有了Earwicker的可能包括在内):

public static void Pipe<T>(this T val, Action<T> action) where T : class
{ if (val!=null) action(val); }

public static R Pipe<T, R>(this T val, Func<T, R> func) where T : class where R : class
{ return val!=null?func(val):null; }
Run Code Online (Sandbox Code Playgroud)

然后你可以写下这样的东西:

Func<string, string[]> readlines = (f) => File.ReadAllLines(f);
Action<string, string> writefile = (f, s) => File.WriteAllText(f, s);

Action<string, string> RemoveLinesContaining = (file, text) =>
    {
        file.Pipe(readlines)
            .Filter(s => !s.Contains(text))
            .Fold((val, sb) => sb.AppendLine(val), …
Run Code Online (Sandbox Code Playgroud)

c# f# functional-programming

24
推荐指数
2
解决办法
7926
查看次数

如何从F#调用Enumerable.Join?

我有两个序列(元组),我需要在其中进行连接:

  • Seq 1:[(City1*Pin1),(City2*Pin2),(City1*Pin3),(City1*Pin4)]
  • Seq 2:[(Pin1*ProductA),(Pin2*ProductB),(Pin1*ProductC),(Pin2*ProductA)]

进入序列(元组):

  • [(City1*ProductA),(City2*ProductB),(City*ProductC),(City2*Product A)...]

在C#中,我可以使用Linq Join扩展方法执行此操作,如:

seq1.Join(seq2, t => t.Item2, t=> t.Item1,
    (t,u) => Tuple.Create(t.Item1, u.Item2))
Run Code Online (Sandbox Code Playgroud)

我如何在F#中实现这一目标?我在那里找不到Seq的加入.

c# linq f#

5
推荐指数
1
解决办法
1582
查看次数

标签 统计

c# ×3

f# ×2

linq ×2

.net ×1

data-structures ×1

functional-programming ×1