小编Rub*_*ink的帖子

如何从Option'a的序列中的Some(x)值中提取x值?

对我来说,这似乎是一个脑子.我需要从序列中提取包含在Some(x)中的x值.我可以做到

xs |> Seq.fold (fun state x -> match x with -> | Some(y) -> y::state | None -> state) [] 
    |> Seq.toList 
    |> List.rev 
    |> List.toSeq
Run Code Online (Sandbox Code Playgroud)

有更好的方法吗?

f#

0
推荐指数
2
解决办法
972
查看次数

在变量绑定中使用Discriminated Union类型

我已经创建了一个自定义类型,并希望创建2个变量,以证明我的类型按预期工作.

type number = A of int | B of float;;

let a = 0;;
let b = 0.0;; 
Run Code Online (Sandbox Code Playgroud)

我该如何更改变量声明以强制它们输入number?目前a为int,b为float.

f# types discriminated-union

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

String.length和连接字符串

下面的两个表达式产生相同的输出:

> ("hello" + " " + "world!");;
val it : string = "hello world!"

> "hello" + " " + "world!";;
val it : string = "hello world!"
Run Code Online (Sandbox Code Playgroud)

为什么然后String.length与第一个一起工作但不与第二个工作?

> String.length ("hello" + " " + "world!");;
val it : int = 12

> String.length "hello" + " " + "world!";;

  String.length "hello" + " " + "world!";;
  ------------------------^^^

stdin(57,25): error FS0001: The type 'string' does not match the type 'int'
Run Code Online (Sandbox Code Playgroud)

这是在FSI 14.0.22214.0上生成的

f#

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

F#GetDigitValue值或构造无效

我是F#的新手,想得到一个建议.

我想使用GetDigitValue函数.

open System
open System.Drawing
open System.Globalization

    let getSubscript ichar = 
        match ichar with 
        |1 -> GetDigitValue(843)
        | _ -> GetDigitVale(852)
Run Code Online (Sandbox Code Playgroud)

我有以下错误:未定义值或构造函数'getDigitValue".

f#

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

在F#中精确使用Async.Sleep()

我有个问题:

我在F#中使用Async.Sleep()方法时遇到问题.这是我程序中的一段代码:

if (newAngle <> currentAngle) then
    if (newAngle = 0) then
        Motor(MotorPort.OutA).SetSpeed(100y)
        angle1 <- Motor(MotorPort.OutA).GetTachoCount()
        **Async.Sleep(20)**
        angle2 <- Motor(MotorPort.OutA).GetTachoCount()
        speed <- abs(angle2 - angle1)
        distance <- abs(newAngle - angle2)
        if (speed > 11) then
            pwr <- 20L + int64 distance
            if (pwr < 100L) then
                Motor(MotorPort.OutA).SetSpeed(sbyte pwr)
        while (distance > 30 || angle2 <> angle1) do
            angle1 <- Motor(MotorPort.OutA).GetTachoCount()
            **Async.Sleep(20)**
            angle2 <- Motor(MotorPort.OutA).GetTachoCount()
            speed <- abs(angle2 - angle1)
            distance <- abs(newAngle - angle2)
            if (speed > 11) then
                pwr …
Run Code Online (Sandbox Code Playgroud)

f# asynchronous

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

如果我有一个完整的单元测试套件用于应用程序,我是否仍然应用开放/封闭原则(OCP)?

关于OCP维基百科文章说(强调我的):

...开放/封闭原则规定"软件实体(类,模块,功能等)应该是可以扩展的,但是关闭以进行修改"......这在生产环境中特别有价值,在这种环境中,源代码发生了变化可能需要代码审查,单元测试和其他此类程序才能使其符合产品使用要求:遵守原则的代码在扩展时不会发生变化,因此不需要这样的努力.

所以,我在正确的阅读它,如果有OCP将是有价值的自动化单元测试,但不一定,如果有?或维基百科的文章错了吗?

open-closed-principle

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