在同一解决方案中混合 F# 和 C# 的模式 [英] Patterns to mix F# and C# in the same solution

查看:17
本文介绍了在同一解决方案中混合 F# 和 C# 的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习了很少的函数式语言,主要是为了学术目的.尽管如此,当我必须设计客户端-服务器应用程序时,我总是开始采用领域驱动设计,严格来说是面向对象编程.

I studied few functional languages, mostly for academical purposes. Nevertheless, when I have to project a client-server application I always start adopting a Domain Driven Design, strictly OOP.

使用 .Net 框架编写的复杂解决方案不仅可以使用一种语言,有时还可以使用多种范式来获得优势.将 C 或 C++ 与 LUA 或 Python 混合使用是一种常见做法,有时嵌入 prolog 会非常有趣.我从未尝试将 OOP 和函数范式混合在一起.

A complex solution written in a .Net framework could get advantages using more than a language and sometimes more than a paradigm. Mixing C or C++ with LUA or Python is a common practice, sometimes embedding prolog can be very interesting. I never tried to mix OOP and functional paradigm.

F# 是一种较新的函数式和面向对象语言,我认为在同一解决方案中将 C# 与 F# 库混合在技术上非常容易.但我想知道这是否有意义:我使用 LINQ 来满足我的许多功能需求.

F# is a newer functional and object oriented language, I see that's it's technically very easy to mix C# with F# libraries in the same solution. But I wonder if it makes any sense: I use LINQ to satisfy many of my functional requirements.

您认为何时以及如何将这两种语言混合在一起是个好主意?我想知道是否存在一组尝试这样做的模式.

When and how, do you think it's a good idea to mix these two languages together? I wonder if exists a set of patterns that tries that.

你真的在 C# 解决方案中使用 F# 吗?

Do you actually use F# in a C# solution?

推荐答案

在某些地方,传统的函数式技术很有意义,并导致代码更小、更简洁.一个经典的例子是文本解析和树处理,当您实现 DSL 时,这两者通常会同时出现.F# 特性,如匿名迭代器、可扩展模式匹配以及定义自定义中缀运算符以用作组合器的能力,在这里确实有很大帮助.同时,在 C# 方面,LINQ 是一个好的开始,但它并没有带你一路走下去.

There are certain places where traditional functional techniques make a lot of sense, and lead to code that is both smaller and more concise. A classic example is text parsing and tree processing, both often appearing together when you're implementing a DSL. F# features like anonymous iterators, extensible pattern matching, and ability to define custom infix operators to serve as combinators really helps a lot here. Meanwhile, on the C# side, LINQ is a good start, but it doesn't take you all the way there.

我建议您查看 FParsec,然后亲眼看看它有多适合比您可能用 C# 编写的任何库都要先进的文本处理/解析.

I suggest you have a look at FParsec, and see for yourself how much better suited it is to advanced text processing / parsing than any library you could possibly write in C#.

这篇关于在同一解决方案中混合 F# 和 C# 的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆