Range我正在尝试使用以下定义对泛型类型进行建模:
data Range a = Range
{ lower :: a
, upper :: a }
Run Code Online (Sandbox Code Playgroud)
我添加了一个智能构造函数(并且仅在没有数据构造函数的情况下导出它),以便消费者不会意外创建一个Range其lower字段大于其upper字段的 a :
range :: Ord a => a -> a -> Range a
range lower upper =
if lower > upper
then Range upper lower
else Range lower upper
Run Code Online (Sandbox Code Playgroud)
这一切都很好,我继续派生Functor它的一个实例:
instance Functor Range where
fmap f (Range lower upper) = Range (f lower) (f upper)
Run Code Online (Sandbox Code Playgroud)
这变得有问题,因为我可以这样做:
main :: IO ()
main = do
let r1 …Run Code Online (Sandbox Code Playgroud) 从之前提出的类似问题来看,众所周知,缩进嵌套using语句的首选方法是(这是Visual Studio 2015的默认设置):
using (var enumerator1 = list1.GetEnumerator())
using (var enumerator2 = list2.GetEnumerator())
{
// Use enuemrator1 and enumerator2 here
}
Run Code Online (Sandbox Code Playgroud)
但是,当我输入以下内容时,我发现Visual Studio 2015中的行为已更改:
using (var enumerator1 = list1.GetEnumerator())
Run Code Online (Sandbox Code Playgroud)
并点击↵,光标在第二行缩进:
using (var enumerator1 = list1.GetEnumerator())
? // Text caret appears here instead of at the same level of
// indent of the previous line
Run Code Online (Sandbox Code Playgroud)
如何恢复旧行为以及更改此行为的理由是什么?
我有一个从面向铁路的编程复制的验证模块,它在我的应用程序中执行错误处理:
type ErrorMessage = ErrorMessage of string
type ValidationResult<'T> =
| Success of 'T
| Error of ErrorMessage
module ValidationResult =
let doubleMap successHandler errorHandler = function
| Success x -> successHandler x
| Error e -> errorHandler e
let bind f = function
| Success x -> f x
| Error e -> Error e
let (>=>) f g = f >> bind g
Run Code Online (Sandbox Code Playgroud)
我正在使用以下测试函数测试Kleisli组合:
let validation1 (list: int list) =
if List.length list = …Run Code Online (Sandbox Code Playgroud) 我看到了很多具有以下结构的代码:
public void Blah()
{
int a = 0;
string b = "";
DateTime c = DateTime.MinValue;
bool d = false;
// ...More initializations with dummy values
// Overwrite the values in a, b, c, d, e.g. a = ReturnInt();
// Do calculations, reading the values from a, b, c, d, like DoCalculations(a);
}
Run Code Online (Sandbox Code Playgroud)
一般来说,我更喜欢这样的东西:
public void Blah()
{
int a = GetInt();
string b = GetString();
DateTime c = GetDateTime();
bool d = GetBool();
// Do calculations, reading the …Run Code Online (Sandbox Code Playgroud)