我有两个字符串,必须比较相似性.必须设计算法以找到最大相似性.在这种情况下,排序很重要,但是干预(或缺失)字符则不然.由于各种原因,在这种情况下不能使用编辑距离.
情况基本如下:
string 1: ABCDEFG
string 2: AFENBCDGRDLFG
Run Code Online (Sandbox Code Playgroud)
所产生的算法将找到的子串A,BCD,FG
我目前有一个递归解决方案,但因为这必须在大量数据上运行,所以任何改进都将非常受欢迎
我有一个看起来像这样的函数:
outputDelayCo :: Maybe Int -> (Event -> ByteString) -> [Event] -> Int -> IO ()
outputDelayCo Nothing = outputDelay Nothing
outputDelayCo (Just 1) = do exitSuccess
outputDelayCo (Just n) = outputDelay (Just (n-1))
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Couldn't match expected type ‘(Event -> ByteString)
-> [Event] -> Int -> IO ()’
with actual type ‘IO a0’
In a stmt of a 'do' block: exitSuccess
In the expression: do { exitSuccess }
Run Code Online (Sandbox Code Playgroud)
我可以通过这样做来解决它,但它更加丑陋:
outputDelayCo :: Maybe Int -> (Event -> ByteString) -> [Event] …Run Code Online (Sandbox Code Playgroud) 我目前正在使用Alamofire,我使用枚举来描述我在自述文件中建议使用的API.
端点表示如下:
public enum API {
case GetStops(stopCode:String?)
case GetPhysicalStops
case GetLinesColors
case GetNextDepartures(stopCode:String, departureCode:String?, linesCode:String?, destinationsCode:String?)
}
Run Code Online (Sandbox Code Playgroud)
可选参数是互斥的:
public var URLRequest: NSMutableURLRequest {
let result:(path:String, parameters:[String:AnyObject]?) = {
switch self {
case .GetStops(let stopCode) where stopCode != nil :
return ("GetStops.json", ["stopCode" : stopCode!])
case .GetStops(_):
return ("GetStops.json", nil)
case .GetPhysicalStops:
return ("GetPhysicalStops.json", nil)
case .GetLinesColors:
return ("GetLinesColors",nil)
case .GetNextDepartures(let stopCode, let departureCode, _, _) where departureCode != nil:
return ("GetNextDepartures", ["stopCode" : stopCode, "departureCode": departureCode!])
case .GetNextDepartures(let stopCode, …Run Code Online (Sandbox Code Playgroud) 我有这样的事情:
class ProbeActor extends Actor {
case class Probe(messageKey: String)
def receiveProbe: Receive = {
case Probe(probeKey) => println("Good probe: "+probeKey)
case x => println("Bad probe: "+ x)
}
final override def receive = receiveProbe orElse receiveOther
def receiveOther: Receive = {
case _ => println("Other")
}
}
Run Code Online (Sandbox Code Playgroud)
我称之为:
class Prober extends ProbeActor {
val definite = ActorSystem("ProbeTest").actorOf(Props[ProbeActor], name = "probed")
implicit val timeout = Timeout(5 second)
val future = definite ? Probe("key")
}
Run Code Online (Sandbox Code Playgroud)
我希望文本"Good probe: key"应该打印,但我明白了"Bad …
下面是两个片段,我无法理解为什么一个成功执行而另一个抛出运行时异常.
片段1:
val str = "HELP"
val perfectTuple: (String, String) = str match {
case "NO-HELP" => ("First Help", "Second Help")
case "OTHER-HELP" => ("I won't Help!", "Even,I won't Help!")
case "HELP" => (null,"Single Help")
case _ => throw new NoSuchMethodException
}
Run Code Online (Sandbox Code Playgroud)
Snippet2:
val str = "HELP"
val (firstPart:String, secondPart:String) = str match {
case "NO-HELP" => ("First Help", "Second Help")
case "OTHER-HELP" => ("I won't Help!", "Even,I won't Help!")
case "HELP" => (null,"Single Help")
case _ => throw new …Run Code Online (Sandbox Code Playgroud) 我发现自己经常使用模式匹配,返回一个Option无匹配情况返回None,例如
x match {
case A(a) => Some(a)
case B(b) => Some(b)
case _ => None
}
Run Code Online (Sandbox Code Playgroud)
我可以想象使用它来简化这个
object MaybeMatchImplicits {
implicit class MaybeMatcher[A](val underlying: A) extends AnyVal {
@inline
def maybeMatch[B](f: PartialFunction[A, B]): Option[B] = f.lift.apply(underlying)
}
}
Run Code Online (Sandbox Code Playgroud)
这使得
scala> import MaybeMatchImplicits._
import MaybeMatchImplicits._
scala> 5 maybeMatch { case 5 => 'good }
res0: Option[Symbol] = Some('good)
scala> 6 maybeMatch { case 5 => 'good }
res1: Option[Symbol] = None
Run Code Online (Sandbox Code Playgroud)
我想知道这种方法是否隐藏任何陷阱和/或在Scala 2.11+中是否存在更简单/更好/更惯用的机制.
更新:目标是处理匹配的rhs上的任意计算,这使得基于异常的解决方案不合需要.
我试图找出模式匹配Scala中int的字符串表示的最佳方法.我真正想要做的是这样的事情:
"1234" match {
// Some cases
case "five" => 5
case Int(i) => i // Fails
case _ => throw new RuntimeException()
}
Run Code Online (Sandbox Code Playgroud)
一种方法是使用正则表达式.一个潜在的问题是它不会检测整数是否太大而不适合int.
val ISINT = "^([+-]?\\d+)$".r
"1234" match {
case "five" => 5
case ISINT(t) => t.toInt
case _ => throw new RuntimeException()
}
Run Code Online (Sandbox Code Playgroud)
另一种方法使用toInt返回的函数Option(从这篇博文中借用).这很好,因为它使标准库弄清楚字符串是否包含整数.问题在于它迫使我将逻辑嵌套在我认为它应该是平坦的地方.
def toInt(s: String): Option[Int] = {
try {
Some(s.toInt)
} catch {
case e: Exception => None
}
}
"1234" match {
case "five" => 5 …Run Code Online (Sandbox Code Playgroud) 我有以下格式的以下java字符串.
String s = "City: [name:NYK][distance:1100] [name:CLT][distance:2300] [name:KTY][distance:3540] Price:"
Run Code Online (Sandbox Code Playgroud)
使用java.util.regex包问题和模式类,我必须得到以下格式的输出字符串:
Output: [NYK:1100][CLT:2300][KTY:3540]
Run Code Online (Sandbox Code Playgroud)
你能建议一个可以帮助我获得上述输出格式的RegEx模式吗?
我有一个嵌套的歧视联盟代表一副扑克牌:
type Symbol =
| Seven
| Eight
| Nine
| Ten
| Jack
| Queen
| King
| Ace
type Card =
| Heart of Symbol
| Diamond of Symbol
| Spade of Symbol
| Club of Symbol
Run Code Online (Sandbox Code Playgroud)
现在我想写一个函数返回给定卡的值,在我的情况下,它独立于卡片套装:
let GetValue (card : Card) =
match card with
| Heart(Seven) -> 0
| Diamond(Seven) -> 0
| Spade(Seven) -> 0
| Club(Seven) -> 0
...
Run Code Online (Sandbox Code Playgroud)
这写起来显然很乏味.有没有办法做这样的事情
let GetValue (card : Card) =
match card with
| _(Seven) | _(Eight) …Run Code Online (Sandbox Code Playgroud) 我有:
x match {
case a: SomeType => doSomething()
case _ => doSomethingElse()
}
Run Code Online (Sandbox Code Playgroud)
有没有办法检查“ a”不是给定类型?
像这样:
x match {
case !(a: SomeType) => doSomething()
case _ => doSomethingElse()
}
Run Code Online (Sandbox Code Playgroud)
编辑:我同意这个问题可能不是很清楚,但是我的兴趣主要是合乎逻辑的,并且已经得到回答。