我正在寻找一种方法来模拟SELECT * FROM table WHERE attr LIKE '%text%'在PostgreSQL中使用tsvector之类的东西.
我没有使用字典就创建了一个tsvector属性.现在,像...这样的查询
SELECT title
FROM table
WHERE title_tsv @@ plainto_tsquery('ph:*');
Run Code Online (Sandbox Code Playgroud)
...将返回所有标题,如'Physics','PHP'等.但是,如何创建一个返回所有标题的查询,其中标题以'Zend Fram'开头(应该返回例如'Zend Framework')?
当然,我可以使用类似的东西:
SELECT title
FROM table
WHERE title_tsv @@ to_tsquery('zend')
AND title_tsv @@ to_tsquery('fram:*');
Run Code Online (Sandbox Code Playgroud)
然而,这似乎有点尴尬.
所以,问题是:有没有办法用以下方法制定上面给出的查询:
SELECT title
FROM table
WHERE title_tsv @@ to_tsquery('zend fram:*');
Run Code Online (Sandbox Code Playgroud) 我有一个简单的类层次结构,它表示一个类似于图形的结构,其中有几个不同类型的顶点使用case类实现:
sealed trait Node
sealed abstract case class Vertex extends Node
case class Arc extends Node
case class VertexType1 (val a:Int) extends Vertex
case class VertexType2 (val b:Int) extends Vertex
Run Code Online (Sandbox Code Playgroud)
这允许我写这样的匹配块:
def test (x: Node) = x match {
case _ : Arc => "got arc"
case _ : Vertex => "got vertex"
}
Run Code Online (Sandbox Code Playgroud)
或者像这样:
def test (x: Node) = x match {
case _ : Arc => "got arc"
case c : Vertex => c match {
case _ …Run Code Online (Sandbox Code Playgroud) // But pattern matching also makes it easy.
def penultimateRecursive[A](ls: List[A]): A = ls match {
case h :: _ :: Nil => h
case _ :: tail => penultimateRecursive(tail)
case _ => throw new NoSuchElementException
}
Run Code Online (Sandbox Code Playgroud)
有人可以逐行评论这是做什么的吗?
[A]是否像c#中那样通用我们会做什么?
h似乎没有定义?
我认为算法的主要部分是递归调用:
case _ :: tail => penultimateRecursive(tail)
Run Code Online (Sandbox Code Playgroud)
似乎没有检查列表中的2个项目,然后拿第一个项目获得第二个,困惑!
recursion functional-programming scala list pattern-matching
我正在经历"实用编程Erlang",其中有一个定义如下的函数:
split("\r\n\r\n" ++ T, L) -> {reverse(L), T};
split([H|T], L) -> split(T, [H|L]);
split([], _) -> more.
Run Code Online (Sandbox Code Playgroud)
我感兴趣的是第一场比赛,即"\r\n\r\n" ++ T- 这样的模式和类似模式之间是否存在性能差异,我想出了:[13,10,13,10|T]?或者他们是等同的?
我知道这是一个非常简单的问题,我可以(可能)自己检查一下,但如果有差异,我想知道为什么会这样.
谢谢!
例如,如果我想写
1 -> 2 match {
case 1 -> 2 => "matched"
case _ => "not matched"
}
// error: not found: value ->
Run Code Online (Sandbox Code Playgroud)
而不是稍微不那么明显
1 -> 2 match {
case (1, 2) => "matched"
case _ => "not matched"
}
Run Code Online (Sandbox Code Playgroud) 这个pattern()方法和toString()方法有什么区别Pattern?
医生说:
public String pattern()
Run Code Online (Sandbox Code Playgroud)
返回编译此模式的正则表达式.
public String toString()
Run Code Online (Sandbox Code Playgroud)
返回此模式的字符串表示形式.这是编译此模式的正则表达式.
即使他们的实现返回相同的结果:
import java.util.regex.*;
class Test {
public static void main(String[] args) {
Pattern p = Pattern.compile("[a-zA-Z]+\\.?");
String s = p.pattern();
String d = p.toString();
System.out.println(s);
System.out.println(d);
}
}
Run Code Online (Sandbox Code Playgroud)
我认为没有区别,为什么有两种方法呢?或者我错过了什么?
在ML中,可以为匹配模式的每个元素指定名称:
fun findPair n nil = NONE
| findPair n (head as (n1, _))::rest =
if n = n1 then (SOME head) else (findPair n rest)
Run Code Online (Sandbox Code Playgroud)
在这段代码中,我为第一对列表定义了一个别名,并匹配了该对的内容.Scala中是否有等效的构造?
是否可以将有区别的联合标记的类型传递给另一个函数,以便它可以用于模式匹配?
我的意思是非工作的例子:
type Animal = Pig of string | Cow of string | Fish of string
let animals = [Pig "Mike"; Pig "Sarah"; Fish "Eve"; Cow "Laura"; Pig "John"]
let rec filterAnimals animalType animals =
if animals = [] then
[]
else
let rest = filterAnimals animalType (List.tail animals)
match List.head animals with
|animalType animal -> animal::rest // <- this doesn't work
|_ -> rest
printfn "%A" (filterAnimals Pig animals)
Run Code Online (Sandbox Code Playgroud) 我想在OpenCV上编写自己的内核用于Image分类.
但对于SVM(Opencv的内置函数),内核已经定义.
我的问题是,OpenCV中有什么东西可以让我定义我的内核吗?
实际上,我想为图像分类实现多核学习.
假设我们想sum在Haskell中编写自己的函数:
sum' :: (Num a) => [a] -> a
sum' [] = 0
sum' (x:xs) = x + sum' xs
Run Code Online (Sandbox Code Playgroud)
为什么我们不能这样做:
sum' :: (Num a) => [a] -> a
sum' [] = 0
sum' (xs++[x]) = x + sum' xs
Run Code Online (Sandbox Code Playgroud)
换句话说,为什么我们不能++在模式匹配中使用?
pattern-matching ×10
scala ×4
list ×2
case-class ×1
cons ×1
erlang ×1
extractor ×1
f# ×1
haskell ×1
inheritance ×1
java ×1
opencv ×1
postgresql ×1
recursion ×1
regex ×1
semantics ×1
sml ×1
tsvector ×1