我只是对模式匹配及其相应的字节代码进行了一些调查.
val a = Array(1,2,3,4)
a.map {
case i => i + 1
}
Run Code Online (Sandbox Code Playgroud)
对于上面的代码,我使用javap并获取map中的匿名函数的字节代码:
public int apply$mcII$sp(int);
Code:
0: iload_1
1: istore_2
2: iload_2
3: iconst_1
4: iadd
5: ireturn
Run Code Online (Sandbox Code Playgroud)
所以,在我看来,在0线,我们推一个int(参数),然后在第1行,我们载入int和第2行,我们将其推回......这里有什么目的?
谢谢!
如果我们有一个数组
array = [1, 1, 0, 0, 2, 3, 0, 0, 0, 3, 3, 3 ]
Run Code Online (Sandbox Code Playgroud)
我们如何识别给定数字的运行(具有相同值的连续数量)?举例:
run_pattern_for(array, 0) -> 2
run_pattern_for(array, 3) -> 1
run_pattern_for(array, 1) -> 1
run_pattern_for(array, 2) -> 0
Run Code Online (Sandbox Code Playgroud)
没有2的运行,因为没有两个连续的显示.有一次运行3,因为只有一个将树作为连续数字显示.
在Haskell(和各种其他函数式编程语言)中,模式匹配可用于分配列表的特定元素,同时丢弃其他元素:
Prelude> let [x, _, z] = "abc"
Prelude> x
'a'
Prelude> [z, x]
"ca"
Run Code Online (Sandbox Code Playgroud)
请注意,' _'不是变量,并且未分配任何内容:
Prelude> _
<interactive>:5:1: Pattern syntax in expression context: _
Run Code Online (Sandbox Code Playgroud)
对于用Perl编写的Irssi脚本,我想做类似的事情并丢弃' '的第二个元素@_(即不将它分配给任何东西):
my ($message, _, $windowItem) = @_;
Run Code Online (Sandbox Code Playgroud)
这失败并显示错误消息:" Can't declare constant item in "my" at [...]overlength_filter.pl line 17, near ") =""
那么这个下划线通配符的Perl等价物是什么?
在F#中,我可以在语法中的各个位置使用模式匹配.
例如:
// Given this type...
type SingleCaseUnion = | SingleCaseUnion of int
/// ...I can do this:
let destructureInFunc (SingleCaseUnion n) =
printfn "%d" n
// ...and this:
type DestructureInMethod() =
member t.M(SingleCaseUnion n) =
printfn "%d" n
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何做到这一点:
type DestructureInCtor(SingleCaseUnion n) =
do printfn "%d" n
// type DestructureInCtor(SingleCaseUnion n) =
// ---------------------------------------^
//
// stdin(3,40): error FS0010: Unexpected identifier in type definition. Expected ')' or other token.
Run Code Online (Sandbox Code Playgroud)
我是否有错误的语法,或者F#不支持构造函数参数中的模式匹配?
现在支持吗?我能找到的唯一信息是维基(https://github.com/clojure/core.match/wiki/Deftype-and-defrecord-matching)中的示例,它会产生错误:
CompilerException java.lang.AssertionError:无效的列表语法(Red.(Red.axb)yc)in(Black.(Red.(Red.axb)yc)zd).有效语法:[[:default:guard] [:或:default] [:default:only] [:default:seq] [:default:when] [:default:as] [:default:<<] [:default :clojure.core.match /载体]]
我正在寻找一种算法来计算以下内容:我有一个带有预定义区域的图像(附加图像上的绿色图像).用户绘制红色矩形,算法应计算红色矩形是否与绿色矩形匹配.例如,附加图片上的红色矩形的位置就可以了.
计算这个的好方法是什么?有没有最佳实践算法?
我的想法是计算红色矩形的中间,然后确定中间是否在绿色矩形内.另外,我会计算长度和高度是否与绿色长度和高度相匹配(25%或更多).
这是一个好主意吗?还有其他建议吗?
algorithm image-processing pattern-matching template-matching
Swift是否具有与模式匹配中使用的Haskell的as-patterns类似的东西?我试图switch通过使用嵌套模式摆脱下面的代码中的第二条语句:
indirect enum Type: CustomStringConvertible {
case Int
case Fun(Type, Type)
var description: String {
switch self {
case .Int: return "int"
case .Fun(let p, let r):
switch p {
case .Fun(_): return "(\(p)) -> \(r)"
case _: return "\(p) -> \(r)"
}
}
}
}
Type.Int // "int"
Type.Fun(.Int, .Int) // "int -> int"
Type.Fun(Type.Fun(.Int, .Int), .Int) // "(int -> int) -> int"
Run Code Online (Sandbox Code Playgroud)
使用as-pattern的Haskell等效项为:
data Type =
Int
| Fun Type Type
desc :: Type …Run Code Online (Sandbox Code Playgroud) 我在模式匹配中对此进行了调查
| {call_name = #bundle_source; _ }
在代码的早期,bundle_source定义为type(type bundle_source = ...).
那么哈希标志是什么意思呢?{call_name = #bundle_source }在模式匹配中是否意味着call_name预期值具有类型bundle_source?
我搜索了手册中的"哈希标志"和"英镑标志",但一无所获.
-module(count).
-export([count/1]).
count(L) when is_list(L) ->
do_count(L, #{});
count(_) ->
error(badarg).
do_count([], Acc) -> Acc;
do_count([H|T], #{}) -> do_count(T, #{ H => 1 });
do_count([H|T], Acc = #{ H := C }) -> do_count(T, Acc#{ H := C + 1});
do_count([H|T], Acc) -> do_count(T, Acc#{ H => 1 }).
Run Code Online (Sandbox Code Playgroud)
在此示例中,映射键"H"存在且具有与之关联的计数的第三个子句将无法编译.编译器抱怨:
count.erl:11: variable 'H' is unbound
Run Code Online (Sandbox Code Playgroud)
为何H不受约束?
这顺便说一下:
do_count([], Acc) -> Acc;
do_count([H|T], Acc) -> do_count(T, maps:update_with(H, fun(C) -> C + 1 end, 1, Acc)).
Run Code Online (Sandbox Code Playgroud)
但似乎模式匹配应该起作用,而事实并非如此.
我有一个包含简短履历的行政文件数据集。我试图通过使用python和某些模式匹配来提取人们的年龄。句子的一些示例是:
这些是我在数据集中确定的一些模式。我想补充一点,还有其他模式,但是我还没有遇到它们,并且不确定如何实现。我编写了以下代码,效果很好,但是效率很低,因此在整个数据集上运行将花费太多时间。
#Create a search list of expressions that might come right before an age instance
age_search_list = [" " + last_name.lower().strip() + ", age ",
" " + clean_sec_last_name.lower().strip() + " age ",
last_name.lower().strip() + " age ",
full_name.lower().strip() + ", age ",
full_name.lower().strip() + ", ",
" " + last_name.lower() + ", ",
" " + last_name.lower().strip() + " \(",
" " + last_name.lower().strip() + " is "]
#for each element in …Run Code Online (Sandbox Code Playgroud)