如果我有一个接受Type参数的类,例如Seq[T],我有很多这个类的对象.我想根据类型Argument拆分它们T
例如 :
val x = List(Seq[Int](1,2,3,4,5,6,7,8,9,0),Seq[String]("a","b","c"))
x.foreach { a =>
a match{
case _ : Seq[String] => print("String")
case _ : Seq[Int] => print("Int")
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码的结果是 StringString.它只匹配类Seq而不是类型,我应该怎么做才能强制它匹配类型?
我是AWK的新手,我觉得很棒,但我是个菜鸟.
我这样做:
awk '{print $1, $2 = /Pattern\w+/}' file
Run Code Online (Sandbox Code Playgroud)
这种数据:
3145 wfgd4 2345 sdf 55667 PatternAHF34
3148 fh345 34f PatternRT4555 NNN NNN
3158 57ggt 3458f 58886 PatternYT334 56YY
3165 rf3d4 2t45 sdf 55667 Potato
3188 fhrf5 34b PatternRT8855 N3 NN55
3218 g7ggt 34ggf 5116 PatternYYY34 56YT
Run Code Online (Sandbox Code Playgroud)
如您所见,列未对齐.
我想要的是:
3145 PatternAHF34
3148 PatternRT4555
3158 PatternYT334
3165
3188 PatternRT8855
3218 PatternYYY34
Run Code Online (Sandbox Code Playgroud)
我得到了什么:
3145 1
3148 1
3158 1
3165 0
3188 1
3218 1
Run Code Online (Sandbox Code Playgroud)
提前谢谢大家
我试图使用ChoiceFormat显示除格式元素之外的引号的消息.使用该模式,引用不会打印,但使用编程方式,如在用于ChoiceFormat的Java文档中,它工作正常.有线索吗?
Object[] testArgs = { "Navin", 0 };
// Using Pattern
System.out.println(MessageFormat.format("You commented on {1,choice,0#{0}''s diary|1#many diaries}", testArgs));
// Creating programmatically
MessageFormat form = new MessageFormat("You commented on {1}");
double[] limits = { 0, 1 };
String[] part = { "{0}''s diary", "many diaries" };
ChoiceFormat cform = new ChoiceFormat(limits, part);
form.setFormatByArgumentIndex(1, cform);
System.out.println(form.format(testArgs));
Run Code Online (Sandbox Code Playgroud)
给出输出:
你评论了Navins日记
你评论了纳文的日记
我有一种情况,我交给一个对象,需要产生一个字符串.在大多数情况下,我可以调用它.ToString()并完成它(适用于字符串,数字,日期等)
这不起作用的一种情况是表示GUID的字节数组.在这种情况下,ToString()只需报告"System.Byte []".我正在尝试使用模式匹配来捕获字节数组,以便我可以自定义字符串化.但我无法弄清楚如何匹配字节数组(或实际上,任何数组),除了通过数组模式.GUID是16个字节长,我真的不希望有一个16个命名数组元素的模式.此外,使用数组模式会导致F#想要一个数组作为输入,而不是一个对象:
match value (* <-- obj *) with
| [|
b1; b2; b3; b4;
b5; b6; b7; b8;
b9; b10; b11; b12;
b13; b14; b15; b16
|] -> (* do some magic here later... *) b1.ToString()
| x -> x.ToString()
Run Code Online (Sandbox Code Playgroud)
> This expression was expected to have type obj but here has type 'a []
Run Code Online (Sandbox Code Playgroud)
我试图使用Type测试模式,但它会导致编译器错误(似乎有一个打开方括号的问题):
match value (* <-- obj *) with
| :? byte[] as guid -> (* do some magic here …Run Code Online (Sandbox Code Playgroud) F#通过模式匹配来分配函数参数.这就是为什么
// ok: pattern matching of tuples upon function call
let g (a,b) = a + b
g (7,4)
Run Code Online (Sandbox Code Playgroud)
有效:元组与(a,b)匹配,a和b直接在f内可用.
对受歧视的工会采取同样的做法同样有益,但我无法做到:
// error: same with discriminated unions
type A =
| X of int * int
| Y of string
let f A.X(a, b) = a + b // Error: Successive patterns
// should be separated by spaces or tupled
// EDIT, integrating the answer:
let f (A.X(a, b)) = a + b // correct
f (A.X(7, 4))
Run Code Online (Sandbox Code Playgroud)
模式匹配是函数调用的一部分,仅限于元组吗?有没有办法与受歧视的工会一起做?
我们的代码库中包含这样的代码:
def receive: Receive = {
case x: TypeX => doXishThingsWith(x)
case y: TypeY => doYishThingsWith(y)
case z: TypeZ => doZishThingsWith(z)
}
Run Code Online (Sandbox Code Playgroud)
我觉得有必要给名字x,y和z愚蠢和混乱的一种.
我想知道这样的事情是否有效?
def receive: Receive = {
case TypeX => doXishThingsWith(_)
case TypeY => doYishThingsWith(_)
case TypeZ => doZishThingsWith(_)
}
Run Code Online (Sandbox Code Playgroud)
我不知道是否_真的以这种方式工作.但也许有类似的东西?
我刚开始学习haskell和模式匹配.我只是不明白它是如何实现的,是因为多态性而对这种模式的不同类型和函数实现进行评估[]和(x:_)评估,或者我只是错了并且使用了另一种技术.
head' :: [a] -> a
head' [] = error "Can't call head on an empty list, dummy!"
head' (x:_) = x
Run Code Online (Sandbox Code Playgroud)
或者让我们考虑这种模式匹配功能:
tell :: (Show a) => [a] -> String
tell [] = "The list is empty"
tell (x:[]) = "The list has one element: " ++ show x
tell (x:y:[]) = "The list has two elements: " ++ show x ++ " and " ++ show y
tell (x:y:_) = "This list is long. …Run Code Online (Sandbox Code Playgroud) 我的问题:我发现这项练习非常具有挑战性.我被困在出生日期.
挑战:尝试使用正则表达式来验证以下文本字符串(不要担心不区分大小写):
我能够拿到前三个名字.但是我被困在出生日期.
"^[a-z]{1,10}$", // First name
"^[a-z]$", // Middle initial
"^[a-z']{2,10}$", // Last name
Run Code Online (Sandbox Code Playgroud)
请帮我.
这两个模式匹配示例中的最佳等价物是什么- 分别来自On Lisp和PAIP?
>(match ’(p a b c a) ’(p ?x ?y c ?x))
((?Y . B) (?X . A))
T
(difference between ?x* and ?y*)
Run Code Online (Sandbox Code Playgroud)