当您在Google"100F to C"中搜索时,如何将华氏温度转换为摄氏温度?同样,从不同货币转换和简单计算.
使用的数据结构是什么,还是匹配字符串的简单模式?
scala> class A
defined class A
scala> class B
defined class B
scala> val a: A = new A
a: A = A@551510e8
scala> a match {
| case _: B => println("unlikely")
| case _ => println("no match")
| }
no match
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,编译器不应该告诉我其中一个案例永远不能匹配吗?一个稍微复杂的例子最近引起了我的注意,导致了一个应该被编译器捕获的不必要的bug.
编辑:
只是为了更清楚这个问题.由于某种原因,我无法看到Scala中这是不可能的吗?(我可以理解这些类型是否使用泛型和类型擦除导致问题,但这看起来非常直接.)如果这不是不可能的,是否有正当理由这不是Scala?如果不是什么时候加入?;)
可以检查字符串是否适合这种格式的最小C##-#####数量是多少(1个数字,破折号再多5个数字).
在我看来,正则表达式可以做到这一点(再次,我希望我知道正则表达式).
所以,这是一个例子:
public bool VerifyBoxNumber (string boxNumber)
{
// psudo code
if (boxNumber.FormatMatch("#-#####")
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
如果您知道将使上述比较有效的真实代码,请添加答案.
我最近一直在玩提取器,并且想知道List提取器如何工作尤其如此:
List(1, 2, 3) match {
case x :: y :: z :: Nil => x + y + z // case ::(x, ::(y, ::(z , Nil)))
}
Run Code Online (Sandbox Code Playgroud)
Ok ::用在模式中,所以我猜编译器现在在:: - Object中查找unapply方法.试过这个:
scala> (::).unapply(::(1, ::(2, Nil)))
res3: Option[(Int, List[Int])] = Some((1,List(2)))
Run Code Online (Sandbox Code Playgroud)
很好,有效.但是,这不是:
scala> (::).unapply(List(1,2,3))
<console>:6: error: type mismatch;
found : List[Int]
required: scala.collection.immutable.::[?]
(::).unapply(List(1,2,3))
Run Code Online (Sandbox Code Playgroud)
这样做:
scala> List.unapplySeq(List(1,2,3))
res5: Some[List[Int]] = Some(List(1, 2, 3))
Run Code Online (Sandbox Code Playgroud)
其实我此刻有点困惑.编译器如何在此处选择正确的unapply实现.
我有一个带泛型参数的函数,在其中我需要执行两个函数中的一个,具体取决于参数的类型.
member this.Load<'T> _path =
let hhType = typeof<HooHah>
match typeof<'T> with
| hhType -> this.LoadLikeCrazy<'T> _path
| _ -> this.LoadWithPizzaz<'T> _path
Run Code Online (Sandbox Code Playgroud)
....其中LoadLikeCrazy和LoadWithPizzaz都返回'T.
VS通知我,通配符的情况永远不会被执行,因为我显然在编译时得到泛型的类型,而不是运行时的实际类型.我该怎么做?
我已经尝试了下面的代码(相同的方法是在Scala编程之后编写的)
class Person() {
class Room(r: Int, c: Int) {
val row = r
val col = c
override def hashCode: Int =
41 * (
41 + row.hashCode
) + col.hashCode
override def equals(other: Any) =
other match {
case that: Room =>
(that canEqual this) &&
this.row == that.row &&
this.col == that.col
case _ => false
}
def canEqual(other: Any) =
other.isInstanceOf[Room]
}
val room = new Room(2,1)
}
val p1 = new …Run Code Online (Sandbox Code Playgroud) 对于一个简单的数组长度为5的问题(实际上数组长度可能是20 ..)
我有一个预定义的一套模式,如AAAAB,AAABA,BAABC,BCAAA,....每个模式与输入数组的长度相同.我需要一个函数,它接受任何整数数组作为输入,并返回它匹配的所有模式.(一个数组可能匹配几个模式)尽可能快.
" A "表示在该模式中,A位置的所有数字都相等.例如,AAAAA仅表示所有数字相等,{ 1,1,1,1,1 }与AAAAA匹配.
" B "表示位置B处的数字不等于A位置处的数字.(即,不是A的数字的通配符)B表示的数字不必相等.例如,ABBAA表示第1,第4,第5个数字等于,例如x,第2个,第3个不等于x. { 2,3,4,2,2 }与ABBAA相匹配.
" C "表示该位置可以是任何数字(即数字的通配符).{ 1,2,3,5,1 }匹配ACBBA, { 1,1,3,5,1 }也匹配ACBBA
我正在寻找一种有效的(在比较数字方面)算法.它不一定是最优的,但不应该是最佳的.我觉得它有点像决策树......
尝试将每个模式与输入匹配.说AABCA反对{a,b,c,d,e}.它检查是否(a=b=e && a!=c).
如果模式的数量是n,模式/数组的长度是m,则复杂度约为O(n*m)
请随意为这个问题提出更好的措辞,因为我不知道如何在没有混淆的情况下简单地理解问题.
理想的算法需要某种准备,比如将模式集转换为决策树.因此,对于某些特殊模式集,预处理后的复杂性可以实现为O(log n*log m).(只是猜测) …
让我思考的是为什么以下
<something> match { ... }
Run Code Online (Sandbox Code Playgroud)
无法改写为
<something>.match({ ... }) # error: identifier expected but 'match' found.
Run Code Online (Sandbox Code Playgroud)
我想这是因为它不可能match作为常规方法实现,但我不确定.或许这是出于性能原因.
此外,现在宏可用,match可以使用宏来实现吗?(不是说它应该完成,而只是假设)
编辑:这似乎与scala的实验虚拟模式匹配器有什么关系?; 感谢@ om-nom-nom指出.
在下面的代码中,三个不同的函数使用相同的模式匹配(节点n左右).如果我想添加一个模式,例如(Node n(Leaf)(Leaf))或更改我的数据类型,我必须更改所有函数.有没有办法重用这些模式,所以我只需要定义一次?
data Tree = Node Int Tree Tree
| Leaf
add :: Tree -> Int -> Tree
add (Node n left right) x = Node (n+x) (add left x) (add right x)
add (Leaf) x = Leaf
subtract :: Tree -> Int -> Tree
subtract (Node n left right) x = Node (n-x) (subtract left x) (subtract right x)
subtract (Leaf) x = Leaf
swap :: Tree -> Tree
swap (Node n left right) = …Run Code Online (Sandbox Code Playgroud) (灵感来自,无法在具有约束条件的多态元组上进行匹配,并基于我对自己答案的后续评论。)
考虑下面的最小示例:
test :: (Show a, Show b) => (a -> String, b -> String)
test = (show,show)
(resX, resY) = test
Run Code Online (Sandbox Code Playgroud)
这将导致以下错误:
• Ambiguous type variable ‘a0’ arising from a use of ‘test’
prevents the constraint ‘(Show a0)’ from being solved.
Relevant bindings include
resX :: a0 -> String (bound at so.hs:25:2)
Probable fix: use a type annotation to specify what ‘a0’ should be.
These potential instances exist:
instance Show Ordering -- Defined in ‘GHC.Show’ …Run Code Online (Sandbox Code Playgroud) pattern-matching ×10
scala ×4
algorithm ×2
c# ×2
haskell ×2
string ×2
extractor ×1
f# ×1
generics ×1
list ×1
polymorphism ×1
scala-macros ×1
search ×1
typechecking ×1
types ×1