小编Kai*_*Kai的帖子

C#Linq vs. Currying

我正在尝试函数式编程及其各种概念.所有这些都非常有趣.我曾几次读过Currying以及它有什么优势.

但我不明白这一点.以下来源演示了咖喱概念的使用和linq的解决方案.实际上,我没有看到任何使用currying概念的建议.

那么,使用currying有什么好处?

static bool IsPrime(int value)
{
    int max = (value / 2) + 1;
    for (int i = 2; i < max; i++)
    {
        if ((value % i) == 0)
        {
            return false;
        }
    }
    return true;
}

static readonly Func<IEnumerable<int>, IEnumerable<int>> GetPrimes = 
        HigherOrder.GetFilter<int>().Curry()(IsPrime);

static void Main(string[] args)
{
    int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

    Console.Write("Primes:");
    //Curry
    foreach (int n in GetPrimes(numbers))
    { …
Run Code Online (Sandbox Code Playgroud)

c# linq currying

15
推荐指数
2
解决办法
3451
查看次数

依赖注入和其他构造函数参数 - 不好的做法?

目前我正在尝试使用依赖注入容器,这次使用Unity.

给出以下界面:

public interface IPodcastCommService
{
    void Download();

    void Upload();
}
Run Code Online (Sandbox Code Playgroud)

以及以下实施:

public class PodcastService
{
     private IPodcastCommService commservice;
     private String url;

     public PodcastService(String url, IPodcastCommService commservice)
     {
         this.commservice = commservice;
         this.url = url;
     }
}
Run Code Online (Sandbox Code Playgroud)

由于构造函数,我一直在寻找一个解决方案来将参数传递给它并找到它:

var p = container.Resolve<IPodcastCommService>(new ParameterOverride("url", myUrl));
Run Code Online (Sandbox Code Playgroud)

到目前为止一切都那么好,但与此同时我读到了这是多么糟糕,这个类的设计有多糟糕,是的,它看起来有点难看.但是如何以优雅的方式将参数传递给类?

我的第一个想法是将它作为一个属性来做,但是每次我需要它已经给出的Url时我必须检查.

更新: 一个例子,我读到这是一个糟糕的设计,是这样的:

但是在某些情况下,您可以为解析操作传递自定义构造函数参数.有些人可能会争辩说,这种糟糕的体系结构的尖叫声,但有一些情况,比如将DI容器带到遗留系统,可能需要这些操作.

资料来源:http://mikaelkoskinen.net/unity-passing-constructor-parameters-to-resolve/

c# dependency-injection

14
推荐指数
1
解决办法
9866
查看次数

对ListView和DataContext的一些困扰

我编写了以下C#和XAML代码:

namespace ListViewExample1
{
public partial class MainWindow : Window
{
    public ObservableCollection<MyColleague> myCollegues = new ObservableCollection<MyColleague>();

    public MainWindow()
    {
        myCollegues.Add(new MyColleague() { Name = "Tim", Surname = "Meier" });
        myCollegues.Add(new MyColleague() { Name = "Martin", Surname = "Hansen" });
        myCollegues.Add(new MyColleague() { Name = "Oliver", Surname = "Drumm" });


        InitializeComponent();
    }

    public ObservableCollection<MyColleague> MyColleagues 
    {
        get { return this.myCollegues; }
    }
}

public class MyColleague
{
    public String Name { get; set; }

    public String Surname { get; set; …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf datacontext xaml

2
推荐指数
1
解决办法
1万
查看次数

使用F#计算三角形

我正在尝试编写一个程序来计算三角形.对于这个计算,有人能给我一个F#sharp的简短代码吗?

在此输入图像描述

这是我到目前为止所做的,但我不相信这是最好的方法:

let area a b c = 
  let s = sqrt((a + b + c) / 2) 
  sqrt(s * (s - a) * (s - b) * (s - c))
Run Code Online (Sandbox Code Playgroud)

geometry f#

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