我试图用以下定义定义二叉树:
trait Node {
val label: Int
}
case class BranchNode(override val label: Int, left: Option[Node], right: Option[Node]) extends Node
case class LeafNode(override val label: Int) extends Node
Run Code Online (Sandbox Code Playgroud)
然后printTree使用Scala的模式匹配定义一个简单的方法,如下所示:
def printTree(aTree: Option[Node]): Unit = aTree match {
case None => print(".")
case Some(LeafNode(label)) => print(label)
case Some(BranchNode(label, left, right)) => "{" + printTree(left) + label + printTree(right) + "}"
}
Run Code Online (Sandbox Code Playgroud)
Intellij IDE警告我,这场比赛可能并非详尽无遗.一个Option可以拥有None或Some作为它的价值观.如果是Option[Node],它可以是Some(LeafNode)或Some(BranchNode).还有什么其他情况我可以忽略?
如果我有一个表测试,其值如下:
id | value
----------------
1 | ABC 1-2-3
2 | AB 1-2-3-4-5
3 | ABC 1
4 | ABC 1-2
5 | ABC
Run Code Online (Sandbox Code Playgroud)
而我尝试输入的字符串是ABC 1-2-3-4-5,那么最接近的子字符串匹配项(如果可以这样称呼)应该是ABC 1-2-3。第2行不应匹配,因为它没有“ ABC”。如果输入的字符串比实际记录短,我只能搜索该字符串,但如果它长一些,则无法搜索。例如
select * from test where value ilike 'ABC 1-2%';
Run Code Online (Sandbox Code Playgroud)
但这也没有给我确切的记录,只有给以ABC 1-2开头的记录。如何构造适当的sql语句来解决此问题?
是否应该预期这两者之间的性能差异empty,还是仅仅是风格偏好的问题?
foo list = case list of
[] -> True
(_ : _) -> False
bar list = case list of
(_ : _) -> False
_ -> True
Run Code Online (Sandbox Code Playgroud) 我需要使用正则表达式匹配一个单词,它位于反引号/反引号之间,最多有1和2个反引号.
匹配案例
不应该匹配
例
I `need` to match a ``word`` from a ```sentence``
Which `lies`` between `backquotes```` and this ``should```` also match
and ```more``` than ```three```````` quotes ```````not``` matched
Run Code Online (Sandbox Code Playgroud)
火柴:
尝试
Pattern PATTERN = Pattern.compile("`{1,2}\\w+|\\w+`{1,2}", Pattern.DOTALL);
Run Code Online (Sandbox Code Playgroud) 是否可以在IntelliJ中创建运行/调试配置,以仅选择那些匹配正则表达式的文件?
我想运行所有的单元测试。我不想运行同事编写的集成测试。集成测试包含文本“ IT”,因此我认为以下正则表达式可以:
^((?!IT).)*$
Run Code Online (Sandbox Code Playgroud)
但事实并非如此。
编辑
作为一个简单的示例,我尝试了以下正则表达式:
^.*IT.*$
(这与我想要实现的目标完全相反),但是我仍然失败了。它仍然拾取每个测试并运行它。
获得的示例测试:
com.my_company.session.SessionTest.java
com.my_company.test.server.api.ITAuthentication.java
Run Code Online (Sandbox Code Playgroud)
这就是“测试结果”面板的外观
我在这里缺少什么
Visual Studio告诉我使用内联模式匹配并为我重写代码但是当它发生时我得到错误:
严重级代码描述项目文件行抑制状态错误CS8121类型为TReturnState的表达式不能由LightState类型的模式处理.DataModels C:\ Users\Michael\Documents\windows\GCMS UWP\Models\Models\Elements\Lights\Light.cs 77 Active
这是原始代码:
public override void UpdateState<TReturnState>(TReturnState returnState)
{
var newState = returnState as LightState;
if (newState != null)
State = newState;
base.UpdateState(returnState);
}
Run Code Online (Sandbox Code Playgroud)
这是VS为我重做它时的样子.
public override void UpdateState<TReturnState>(TReturnState returnState)
{
if (returnState is LightState newState)
State = newState;
base.UpdateState(returnState);
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢调整后的方式,但我收到错误.我错过了什么或这是一个错误吗?
我正在使用的包:
"dependencies": {
"Microsoft.NETCore.UniversalWindowsPlatform": "5.3.1",
"Newtonsoft.Json": "9.0.1",
"System.ValueTuple": "4.3.0"
},
Run Code Online (Sandbox Code Playgroud) 我知道我可以使用模式匹配功能参数,如下所示:
fn :: (Integral a) => (a,a) -> (a, a)
fn (x,y) = (y,x)
Run Code Online (Sandbox Code Playgroud)
但是如何匹配返回值?我希望这样的事情:
g :: (Integral a) => a -> a
g z = do
(x, y) = fn (z, z + 5)
x `mod` y
Run Code Online (Sandbox Code Playgroud)
这会导致语法错误.有没有办法匹配返回值?基本上,我想将返回的元组拆分为两个变量.
我正在尝试将%{{x, y} => [items]}一堵带有墙壁的房间涂成地图,并且正在使用一个案例声明来确定要绘制哪种类型的墙壁。
但是,尝试进行模式匹配并将其分配pos给左侧的值很困难。(引发illegal pattern编译错误)
我知道我可以阻止分配^,但是我该怎么办(from_y+sizey)呢?
def place_room({from_x, from_y}, {size_x, size_y}, original_map) do
Enum.reduce from_x..(from_x + size_x-1), original_map, fn x, map ->
Enum.reduce from_y..(from_y + size_y-1), map, fn y, map ->
pos = {x, y}
case pos do
{from_x, from_y} ->
place(map, pos, :wall, %{type: :room_wall_tl})
{from_x, (from_y+size_y)} ->
place(map, pos, :wall, %{type: :room_wall_tr})
{from_x, (from_y+size_y)} ->
place(map, pos, :wall, %{type: :room_wall_bl})
{(from_x+size_x), (from_y+size_y)} ->
place(map, pos, :wall, %{type: …Run Code Online (Sandbox Code Playgroud) 鉴于以下内容:
type IFruit = interface end
type Avocado = { color : string; age : int } interface IFruit
let (|AvocadoTexture|) (a : Avocado) = if a.age < 7 then "firm" else "mushy"
Run Code Online (Sandbox Code Playgroud)
...为什么这样做:
let texture (f : IFruit) =
match f with
| :? Avocado as a -> if a.age < 7 then "firm" else "mushy"
| _ -> String.Empty
Run Code Online (Sandbox Code Playgroud)
......但不是吗?
let texture (fruit : IFruit) =
match fruit with
| AvocadoTexture t -> t // "The type IFruit …Run Code Online (Sandbox Code Playgroud) 我构建了一个简单的函数,给定一个列表,返回一个列表列表.每个单独的清单都必须订购.例如:
subOrd [4;4;10;20;5;30;6;10] --> [[4;4;10;20];[5;30];[6;10]]
subOrd [5;6;4;3;2;1] --> [[5;6];[4];[3];[2];[1]]
Run Code Online (Sandbox Code Playgroud)
我这是我目前的解决方案,除了详细信息外,它的效果非常好:
let rec subOrd (l1: int list) :int list list =
let rec aux2 (l2: int list) (l3: int list) :int list=
match l2 with
| [] -> []
| [x] -> [x]
| x0::(x1::_ as xs) when x0 > x1 -> x0::l3
| x0::(x1::_ as xs) when x0 <= x1 -> (x0::l3)@(aux xs l3)
match l1 with
| [] -> []
| x::xs -> (aux2 l1 [])::subOrd xs
Run Code Online (Sandbox Code Playgroud)
它重复xs …
pattern-matching ×10
f# ×2
haskell ×2
regex ×2
c# ×1
c#-7.0 ×1
elixir ×1
java ×1
list ×1
performance ×1
postgresql ×1
scala ×1
string ×1
tuples ×1