标签: pattern-matching

我的匹配似乎很笨重,有没有更好的方法来编码?

这似乎有效,但看起来很笨重.有没有更好的方法来编码?

// Hunting for the best index to use for a data compare
let getIndex connDB strTable =
    match getIndexByPrimaryKey connDB strTable with
    | Some(name) -> Some(name)  
    | None ->
    match getIndexByCluster connDB strTable with
    | Some(name) -> Some(name)
    | None -> 
    match getIndexByFirstColumns connDB strTable with
    | Some(name) -> Some(name)
    | None -> 
    match getIndexByOnlyUnique connDB strTable with
    | Some(name) -> Some(name)
    | None -> 
    match getAnyUniqueIndex connDB strTable with
    | Some(name) -> Some(name)
    | None -> None
Run Code Online (Sandbox Code Playgroud)

f# pattern-matching option

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

模式匹配有多聪明?

我的程序大部分时间花在数组模式匹配上,我想知道我是否应该重写该函数并丢弃自动模式匹配.

例如一个非常简单的案例

let categorize array =
    match array with
    | [|(1|2);(1|2);(1|2)|] -> 3
    | [|(1|2);(1|2);_|] -> 2
    | [|(1|2);_;_|] -> 1
    | _ -> 0

categorize [|2;1;3|]
Run Code Online (Sandbox Code Playgroud)

在这种情况下,编译器是否应用最少量的比较,通过识别例如第一种情况与第二种情况相同,除了第三种情况.

实际上模式更复杂,预优化模式匹配可能比完全优化模式匹配花费更多时间.

f# pattern-matching

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

如何模式匹配继承树中的抽象父类

我是scala的新手,拥有java背景.

有没有办法在类继承树中模式匹配超类(或特征),叶子作为案例类和节点抽象类或特征?据我所知,不允许使用case类继承.

我认为在大型继承树中匹配抽象类的模式会非常有用

在下面的代码中,编译期间匹配语句错误的最后一种情况

sealed trait Person {
   def name: String
}

case class Customer(name: String, email: String) extends Person

sealed trait Employee extends Person {
   def id: Int
}

case class Worker(name: String, id: Int, skills: Array[String]) extends Employee

case class Manager(name: String, id: Int, title: String) extends Employee

def process(p: Person) = p match {
   case Customer(_, email) => email
   case Employee(name, _) => name + "@acme.com"
}
Run Code Online (Sandbox Code Playgroud)

scala pattern-matching case-class

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

如何在"案例"Mathematica函数中使用">"和"<"?

如何在"案例"Mathematica函数中使用">"和"<"?

例如,我希望以下嵌套列表中以大于2的结尾的情况:

    lst = { {1, 0, 0}, {1, 1, 1}, {1,1,4} }
Run Code Online (Sandbox Code Playgroud)

我想做点什么

    Cases[lst, {_, _,>2} ] 
Run Code Online (Sandbox Code Playgroud)

表达"> 2"的正确方法是什么?

wolfram-mathematica pattern-matching

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

PHP,从包含特定单词的大字符串中删除所有行

$file = file_get_contents("http://www.bigsite.com");
Run Code Online (Sandbox Code Playgroud)

我怎样才能从$file包含单词"hello"的字符串中删除所有行?

php pattern-matching

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

Perl:匹配模式后如何打印下一行?

我想在匹配图案或线后打印特定数据.我有这样一个文件:

#******************************    
List : car  
Design: S  
Date: Sun 10:10  
#******************************

b-black  
g-green
r-red  

Car Type              No.           color  
#-------------------------------------------    
N17              bg099            g  
#-------------------------------------------    
Total 1 car  

#****************************** 
List : car  
Design: L  
Date: Sun 10:20  
#******************************

b-black  
g-green
r-red  

Car Type            No.            color   
#-------------------------------------------    
A57            ft2233            b  
#-------------------------------------------    
Total 1 car  

#******************************    
List : car  
Design: M  
Date: Sun 12:10  
#******************************    

b-black  
g-green
r-red  

Car Type           No.             color  
#-------------------------------------------    
L45            nh669             g   
#-------------------------------------------    
Total 1 car  

#. .    
#. . …
Run Code Online (Sandbox Code Playgroud)

perl pattern-matching

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

找到非详尽的模式

我能以某种方式看到haskell尝试使用的模式吗?它适用于较小的例子,但在较大的例子上崩溃,我很难找到它可能是什么情况.

euler18 :: [[Integer]]
euler18 = pathPyr minipyr

pathPyr xss = path [(head xss)] (tail xss)

minipyr = [[3],[7,4],[2,4,6]]
pyramid = [[75], [95,64], [17,47,82], [18,35,87,10], [20,4,82,47,65], [19,1,23,75,3,34], [88,2,77,73,7,63,67], [99,65,4,28,6,16,70,92], [41,41,26,56,83,40,80,70,33], [41,48,72,33,47,32,37,16,94,29], [53,71,44,65,25,43,91,52,97,51,14], [70,11,33,28,77,73,17,78,39,68,17,57], [91,71,52,38,17,14,91,43,58,50,27,29,48], [63,66,4,68,89,53,67,30,73,16,69,87,40,31], [4,62,98,27,23,9,70,98,73,93,38,53,60,4,23]]

path :: [[Integer]] -> [[Integer]] -> [[Integer]]
path xss (ys:[])  = extendpath xss ys
path xss (ys:yss) = path (extendpath xss ys) yss

extendpath :: [[Integer]] -> [Integer] -> [[Integer]]
extendpath (xs:[]) (y1:y2:[]) = [y1:xs,y2:xs]
extendpath (xs:xss) (y1:y2:ys) = [y1:xs,y2:xs] ++ extendpath xss (y2:ys) …
Run Code Online (Sandbox Code Playgroud)

haskell pattern-matching

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

非穷举模式异常,用于绑定但不用于执行

为了进入上下文,我正在将列表理解(从那里)转换为"模式过滤"到其monadic对应物(do和bind),然后我遇到异常.

我从这些定义开始(bf为breadthFirst持有),

上下文,数据和帮助器

data Tree a = Leaf | Node a (Tree a) (Tree a) deriving Show
let test = Node 1 (Node 2 (Node 4 Leaf Leaf) Leaf) (Node 3 Leaf (Node 5 Leaf Leaf))
let nextChild = concatMap (\x -> case x of; Leaf -> []; Node n l r -> [l,r])
Run Code Online (Sandbox Code Playgroud)

功能测试

let bfLc xs   | null xs = [] | otherwise = [ n | Node n _ _ <- xs] ++ (bfLc $ …
Run Code Online (Sandbox Code Playgroud)

monads haskell list-comprehension pattern-matching

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

从文本文件中获取数据

我正在尝试使用以下结构从文本文件中提取数据:

Employee: John C.
  2013-01-01  10  $123
  2013-01-02  12  $120
  2013-01-03  8  $150
Employee: Michael G.
  2013-01-01  5  $13
  2013-01-05  11  $20
  2013-01-10  2  $155
Run Code Online (Sandbox Code Playgroud)

如您所见,模式是一个包含Employee名称的表头,然后是包含其所有事务的表内容,然后重复模式.

要提取交易,我这样做:

awk '/^  [A-Z]/{print $1"\t"$2"\t"$3}'
Run Code Online (Sandbox Code Playgroud)

这给出了这个结果:

  2013-01-01  10  $123
  2013-01-02  12  $120
  2013-01-03  8   $150
  2013-01-01  5   $13
  2013-01-05  11  $20
  2013-01-10  2   $155
Run Code Online (Sandbox Code Playgroud)

如何创建一个返回此的两遍提取:

  2013-01-01  10  $123  John C.
  2013-01-02  12  $120  John C.
  2013-01-03  8   $150  John C.
  2013-01-01  5   $13   Michael G.
  2013-01-05  11  $20   Michael G.
  2013-01-10  2   $155  Michael …
Run Code Online (Sandbox Code Playgroud)

regex perl awk sed pattern-matching

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

F#奇数模式匹配问题

在昨天编写一些代码的时候,我遇到了两个奇怪的问题,我和我的函数编程导向的朋友都不知道.我们已经看了很长时间,并在网上研究过它,但我们无法在任何地方找到任何答案,所以这里有:

问题是在这段代码中:

第一个奇怪的问题:

let outer1 (bs : byte array) =
    let rec inner (bs : byte array) (bacc : byte array) (i : int) =
        match i with
        | bs.Length -> bacc // <--- Error: bs is not recognized. Why?
        | _ -> bacc.[i] <- bs.[i]
               inner bs bacc (i + 1)
    inner bs (Array.zeroCreate bs.Length) 0
Run Code Online (Sandbox Code Playgroud)

这里的问题是:FS0039: The namespace or module 'bs' is not defined. 这怎么可能?毕竟bs在函数签名中.而且,在let bsLength = bs.Length工作之前定义一个新的值match.但通过这样做,我看到了一个新的怪异:

let outer2 (bs : byte …
Run Code Online (Sandbox Code Playgroud)

f# pattern-matching

3
推荐指数
2
解决办法
395
查看次数