我是使用scala的新手.我试图使用模式匹配与反射:
abstract class Person{
val name:String,
val age:Int
}
case class Man (name:String,age:Int,workPlace:String) extends Person
val aclass = Class.forName("man")
aclass match{
case m:Man => println("this is a man class")
case p:Person => println("Person")
case _ => println("Nothing")
}
Run Code Online (Sandbox Code Playgroud)
我总是得到默认部分("Nothing")和正确的匹配(Man).我究竟做错了什么 ?
谢谢
我有这个正则表达式:
<a href(.*foo.bar.*)a>
Run Code Online (Sandbox Code Playgroud)
对于这个字符串,它只给我一个匹配,但我需要它给3个匹配.
<a href="https://foo.bar/1">First</a> RANDOM TEXT COULD BE HERE <a href="https://foo.bar/2">Second</a> RANDOM TEXT COULD BE HERE <a href="https://foo.bar/3">Third</a>
Run Code Online (Sandbox Code Playgroud)
所以每个都a href应该是个体的.
我怎么能做到这一点?
编辑:
此代码搜索匹配项:
Pattern pattern = Pattern.compile("<a href(.*foo.bar.*)a>");
Matcher matcher = pattern.matcher(body);
List<String> matches = new ArrayList<String>();
while (matcher.find()) {
matches.add(matcher.group());
}
Run Code Online (Sandbox Code Playgroud) 如何在盒装元组上进行模式匹配?或者有更好的方法来做这样的事情(简化示例):
open System.Drawing
let coerceColor a =
match box a with
| :? Color as c -> c
| (:? int as r),(:? int as g),(:? int as b) -> Color.FromArgb(r,g,b)
| _ -> failwith "Cannot coerce color"
Run Code Online (Sandbox Code Playgroud) 所以我已经检查了 PHP - 在Laravel 和 Laravel 4强制参数错误中使用参数进行路由
然而,使用所说的 - 我似乎无法使简单的路由成为可能,除非我不了解filter/get/parameters如何工作.
所以我想要做的是路由URL为/ display/2,其中display是一个动作,2是id,但我想将它限制为仅限数字.
我想
Route::get('displayproduct/(:num)','SiteController@display');
Route::get('/', 'SiteController@index');
class SiteController extends BaseController {
public function index()
{
return "i'm with index";
}
public function display($id)
{
return $id;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,如果我使用它会抛出404
Route::get('displayproduct/{id}','SiteController@display');
Run Code Online (Sandbox Code Playgroud)
它将传递参数,但URL可以显示/ ABC,它将传递参数.我想将其限制为仅限数字.
我也不希望它变得安静,因为索引I理想地希望将该控制器与不同的动作混合.
我如何将模式匹配应用于此代码?所以它看起来像功能风格的代码:
def getIndex(i: Int): Option[Int] = {
if (check0(i) == Some) {
check0(i)
}
else if (check1(i) == Some) {
check1(i)
}
else if (check2(i) == Some) {
check2(i)
}
else if (check3(i) == Some) {
check3(i)
}
else {
None
}
}
Run Code Online (Sandbox Code Playgroud)
方法check0, check1, check2, check3全部返回Option[Int]
我有一个类似于的自定义数据类型:
data Token = Number Int
| Otherthings
Run Code Online (Sandbox Code Playgroud)
我希望能够以一种方式使用"数字"而在另一种方式中使用其他东西.所以我可以成功创建一个case语句,如:
parse x = case x of
Number y -> y
Run Code Online (Sandbox Code Playgroud)
然后成功地采取:
let x = Number 7 in parse x
Run Code Online (Sandbox Code Playgroud)
并评估为7.但是,当我尝试将"解析"功能更改为:
parse [] = []
parse (x:xs) = case x of
Number y -> y
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Couldn't match expected type `[a0]' with actual type `Int'
In the expression: y
In a case alternative: Number y -> y
In the expression: case x of { Number y -> y }
Run Code Online (Sandbox Code Playgroud)
为什么这不能以这种方式工作以及解决这个问题的正确方法是什么?非常感谢!
我正在寻找一个函数,例如,它给了我所有匹配的字母数字 giveNumber "D" [("D", 5), ("A", 4) ("D", 25)] = [5,25]
giveNumber :: String -> [(String, a)] -> [a]
giveNumber letter (x:xs) = [snd x | x <- xs | fst x == letter]
Run Code Online (Sandbox Code Playgroud)
我得到一个解析器错误...谢谢
我想实现以下内容:
private def msgPrefix(implicit myClass: MyClass, anotherClass: AnotherClass) = {
Option(myClass, anotherClass) match {
case Some(validMyClass, validAnotherClass) => validMyClass.process + validAnotherClass.process
case _ => ""
}
}
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么?
我正在尝试编写一个程序,它在命令行中使用两个整数并对它们执行一些有趣的操作.我想尽可能容易和必要地编写整数的读/解,因为它应该是相对简单的代码.
我面临的问题是,在Haskell中处理错误并不是那么简单.似乎在Haskell中经常使用模式匹配.这似乎使得代码比命令式版本更难以遵循.
该程序将像这样运行(在这个例子中,它只是将两个数字加在一起):
$ ./my_prog
ERROR: Must run like `./my_prog NUM_A NUM_B`.
$ ./my_prog cat 1
ERROR: Could not parse NUM_A "cat" as integer
$ ./my_prog 10 dog
ERROR: Could not parse NUM_B "dog" as integer
$ ./my_prog 10 1
11
Run Code Online (Sandbox Code Playgroud)
这是我想要在命令式伪Python中做的事情:
function die (errorMessage):
print("ERROR: %s" % errorMessage)
sys.exit(1)
function main ():
if len(sys.argv) != 2:
die("Must run program like `%s NUM_A NUM_B`" % sys.progname)
num_a = 0
num_b = 0
try:
num_a = int(sys.argv[0])
except:
die("Could not parse …Run Code Online (Sandbox Code Playgroud) python haskell coding-style control-structure pattern-matching
我将通过Learn You a Haskell教程并尝试elem'从Recursion部分修改该函数.
原来的elem'功能是:
elem' :: (Eq a) => a -> [a] -> Bool
elem' a [] = False
elem' a (x:xs)
| a == x = True
| otherwise = a `elem'` xs
Run Code Online (Sandbox Code Playgroud)
我的indexOf功能是:
indexOf :: (Eq a, Integral s) => a -> [a] -> s -> s
indexOf _ [] _ = -1
indexOf a (x:xs) s
| a == x = s
| otherwise = indexOf a xs …Run Code Online (Sandbox Code Playgroud)