标签: pattern-matching

有没有一种方法可以检查 F# 中一个模式中的嵌套选项值?

假设我们有以下类型:

type Message {
    text : Option<string>
}

type Update {
    msg : Option<Message> 
}
Run Code Online (Sandbox Code Playgroud)

如何在一行中匹配它,就像在 C# 中使用 null 条件运算符 ie 一样update?.msg?.text?

有没有办法做到这一点?:

match msg, msg.text with 
| Some msg, Some txt -> ...
| None -> ...
Run Code Online (Sandbox Code Playgroud)

因为我不想编写 2 个嵌套的匹配表达式。

monads f# functional-programming pattern-matching

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

F# 与可选元组列表的模式匹配

我正在尝试对可选的元组列表使用模式匹配,但尽管尝试了我能想到的一切,但我无法编写详尽的匹配表达式。

我很难理解为什么 F# 编译器坚持认为我在以下示例中的模式并不详尽。

module Mapper.PatternMatchingOddity

type A = A of string
type B = B of string

type ProblemType = ProblemType of (A * B) list option

//Incomplete pattern matches on this expression. Some ([_;_]) may indicate a case...
let matchProblem = function
    |Some [(x:A,y:B)] -> []
    |Some ([_,_]) -> [] //rider says this rule will never be matched
    |None -> []

//same as before    
let matchProblem1 = function
    |Some [_,_] -> []
    |Some [] -> []
    //|Some _ …
Run Code Online (Sandbox Code Playgroud)

f# pattern-matching

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

如何模式匹配选项&lt;&amp;Path&gt;?

我的代码看起来像这样:

// my_path is of type "PathBuf"
match my_path.parent() {
    Some(Path::new(".")) => {
        // Do something.
    },
    _ => {
        // Do something else.
    }
}
Run Code Online (Sandbox Code Playgroud)

但我收到以下编译器错误:

expected tuple struct or tuple variant, found associated function `Path::new`
for more information, visit https://doc.rust-lang.org/book/ch18-00-patterns.html
Run Code Online (Sandbox Code Playgroud)

我读了Rust 书中的第 18 章,但我不知道如何使用Path和PathBuf类型来修复我的特定场景。

如何通过检查特定值(如 )来进行模式匹配Option<&Path>(根据文档,这是方法返回的内容) ?parentPath::new("1")

pattern-matching rust

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

如何在匹配的保护表达式中使用 TryParse?

我构建了一个玩具电子表格来帮助学习 F#。当我处理新单元格的文本时,我将其存储为可识别类型。为了解析它,我觉得我应该能够做类似的事情:

        let cv =
            match t with
            | _ when t.Length=0 -> Empty
            | x when t.[0]='=' -> Expr(x)
            | x when t.[0]='\"' -> Str(x)
            | (true,i) when Int32.TryParse t -> IntValue(i) // nope!
            | _ -> Str(t)
Run Code Online (Sandbox Code Playgroud)

我已经尝试了相当多的组合,但我无法将 TryParse 放在守卫中。我写了一个助手:

let isInt (s:string) = 
    let mutable m:Int64 = 0L
    let (b,m) = Int64.TryParse s
    b
Run Code Online (Sandbox Code Playgroud)

我现在可以写:

|    _ when Utils.isInt t -> IntValue((int)t)  
Run Code Online (Sandbox Code Playgroud)

这似乎是一个糟糕的解决方案,因为它丢弃了转换后的结果。将 TryParse 纳入防护的正确语法是什么?

f# pattern-matching

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

模式匹配中“as”的范围

给定一个从列表中删除连续重复项的函数

let rec compress l =
  match l with
  | [] -> []
  | [x] -> [x]
  | x :: (y :: _ as t) -> if x = y then compress t else x :: compress t;;
Run Code Online (Sandbox Code Playgroud)

产生正确的结果

compress ["a"; "a";"b";"c";"c"] ;;
- : string list = ["a"; "b"; "c"]
Run Code Online (Sandbox Code Playgroud)

但如果x :: (y :: _ as t)我改为x :: (y :: t),那么我会得到不正确的结果

compress ["a"; "a";"b";"c";"c"] ;;
- : string list = ["b"; "c"]
Run Code Online (Sandbox Code Playgroud)

这里发生了什么 ?我无法理解声明如何_ …

ocaml pattern-matching

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

仅当函数的参数是临时参数时,`:as` 绑定才有用吗?

Clojure 编程:as中显示的第一个用法如下

(let [[x _ z :as original-vector] v]
  (conj original-vector (+ x z)))
;= [42 foo 99.2 [5 12] 141.2]
Run Code Online (Sandbox Code Playgroud)

但是,通过在https://rextester.com/l/clojure_online_compiler上进行一些实验,我发现它v在 的正文中可见let,因此我可以跳过:as解构并使用v而不是original-vector,结果是相同的

(let [[x _ z] v]
  (conj v (+ x z)))
;= [42 foo 99.2 [5 12] 141.2]
Run Code Online (Sandbox Code Playgroud)

如果我有一个更复杂的表达式而不是v(在 C++ 中我会说是临时/ rvalue,而v将是lvalue),这显然是不可行的。

所以我的问题是::as语法是否仅在我们解构临时对象时有用,而当我们解构命名实体时它完全多余?我的意思是在上面的例子中,使用的唯一区别似乎:as是我们通过另一个名称来引用v,但我不明白这有什么好处......


该示例假设

(def v [42 "foo" 99.2 [5 …
Run Code Online (Sandbox Code Playgroud)

data-binding functional-programming clojure pattern-matching destructuring

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

F# 列表模式匹配限制还是只是写得不好?

鉴于一些CreditScoreInput:

type CreditScoreInput = { id: string; score: string; years: int }

let input = [
    { id = "CUSTOMER001"; score = "Medium"; years = 1 }
    { id = "CUSTOMER001"; score = "Medium"; years = 1 }
    { id = "CUSTOMER002"; score = "Medium"; years = 10 }
    { id = "CUSTOMER003"; score = "Bad"; years = 0 }
    { id = "CUSTOMER003"; score = "Bad"; years = 0 }
    { id = "CUSTOMER003"; score = "Bad"; …
Run Code Online (Sandbox Code Playgroud)

f# pattern-matching

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

在 Rust 模式匹配中将变量绑定到文字

我有一个用 Rust 编写的状态机,它需要对两个状态执行相同的操作。

这两种状态都是枚举的变体,但包含不同数量的元素。

match self.state {
    RunState::ACCUMULATE(byte, count) | RunState::ESCAPE(count) => todo!(), 
    _ => todo!()
}
Run Code Online (Sandbox Code Playgroud)

该示例是无效的源代码,因为byte未绑定在所有模式中。

这可以通过byte在第二个模式中绑定到文字零来解决,但我不知道如何存档。

我目前正在分别匹配这两种模式,这会导致我想避免的代码重复。

感谢您的关注。

pattern-matching rust

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

Haskell 中“[_]”模式的含义是什么?

在 Haskell 中创建模式匹配时,是否执行以下模式匹配:

function [_] = []
Run Code Online (Sandbox Code Playgroud)

意思相同:

function (x:xs) = []
Run Code Online (Sandbox Code Playgroud)

如果不是,这个[_]图案意味着什么?

haskell pattern-matching

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

如果字符串包含如何匹配?

我有一个字符串变量“distro”

let distro = get_distro().unwrap().name;
Run Code Online (Sandbox Code Playgroud)

该变量的值例如是“Arch Linux”。
现在我想检查一个匹配,如果变量包含“arch”。

let distro = get_distro().unwrap().name;
Run Code Online (Sandbox Code Playgroud)

pattern-matching rust

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