我应该如何解释以下内容:
(def a '(1 2 (+ 1 2)))
Run Code Online (Sandbox Code Playgroud)
因此,它评估为:
(1 2 3)
Run Code Online (Sandbox Code Playgroud)
做一个:
(def a '(1 2 ~(+ 1 2)))
Run Code Online (Sandbox Code Playgroud)
在REPL中评估如下:
(1 2 (clojure.core/unquote (+ 1 2)))
Run Code Online (Sandbox Code Playgroud)
我知道我可以做一个:
(list 1 2 (+ 1 2))
Run Code Online (Sandbox Code Playgroud)
但我想知道是否可能有某种语法用于此目的.
我是XML Schema的新手,我遇到的架构文档将许多前缀绑定到xsd:schema根元素中的各个命名空间,并且还导入了这些模式的子集.在XML Schema文档的其余部分中,他们很乐意使用xsd:schema元素中绑定的所有前缀(无论是否导入).
那么import命名空间的含义是什么意味着"只是"将该命名空间绑定到前缀?
从我阅读的Definitive XML Schema书中可以看到(第66页):
导入用于告诉处理器您将引用其他命名空间中的组件
在我的理解中,这也是一个绑定所做的,那么有什么区别?
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vr="http://www.ivoa.net/xml/VOResource/v1.0"
xmlns:ssap="http://www.ivoa.net/xml/SSA/v1.0"
xmlns:vm="http://www.ivoa.net/xml/VOMetadata/v0.1"
targetNamespace="http://www.ivoa.net/xml/SSA/v1.0"
elementFormDefault="unqualified"
attributeFormDefault="unqualified"
version="1.0pr2">
<xs:import namespace="http://www.ivoa.net/xml/VOResource/v1.0"
schemaLocation="http://www.ivoa.net/xml/VOResource/v1.0"/>
<!-- ... rest of the schema document follows -->
Run Code Online (Sandbox Code Playgroud)
http://www.ivoa.net/xml/VOResource/v1.0上述模式文档中的命名空间都绑定到vr前缀并导入.其他名称空间仅绑定到某些前缀而不导入.文档的其余部分使用来自vr(绑定和导入)和ssa(绑定但未导入)前缀的组件.有什么不同?
这个SO讨论提出了以下习语:
public interface IComparable<T extends IComparable<T>> {
int compare(T t);
}
Run Code Online (Sandbox Code Playgroud)
然后允许:
public class Foo implements IComparable<Foo> {
public int compare(Foo foo) {
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这个习惯用法不仅仅包括以上代码,因为下面的代码也可以编译:
class A implements IComparable<A> {
public int compare(A a) {return 0;}
}
public class Foo implements IComparable<A> {
public int compare(A a) {
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
因此(除非我误解了一些东西)原始成语并没有真正购买任何东西,而不是那么戏剧化:
public interface IComparesWith<T> {
int compare(T t);
}
public class A implements IComparesWith<A> {
public int compare(A a) {...}
}
Run Code Online (Sandbox Code Playgroud)
那么,有没有办法实际声明一个接口,以便任何类声明实现它有一个方法来与它自己的类的对象进行比较,没有任何漏洞,如上所述?
我显然无法将上述内容发布为评论,因此我创建了一个新帖子.
在中javax.xml.parsers.DocumentBuilder打印一条消息std:err。
下面的例子:
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class FooMain {
public static Document slurpXML(String s) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document rv = builder.parse(new ByteArrayInputStream(s.getBytes("UTF-8")));
return rv;
}
public static void main(String args[]) throws Exception {
try {
slurpXML("foo");
} catch (Exception e) {} // try to silence it - in vain
}
}
Run Code Online (Sandbox Code Playgroud)
尽管有try-catch块,但从命令行运行程序仍会产生:
$ java -classpath dist/foo.jar FooMain
[Fatal Error] :1:1: Content is not allowed in …Run Code Online (Sandbox Code Playgroud) 我有以下XML文件:
<foo:a xmlns:foo=\"http://www.foo.com\">
<foo:b foo:bar=\"zar\">
</foo:b>
</foo:a>
Run Code Online (Sandbox Code Playgroud)
要获取具有值为"zar" 的属性栏的b节点(全部在正确的命名空间中),我可以使用XPath表达式:
/foo:a/foo:b[@foo:bar=\"zar\"]
Run Code Online (Sandbox Code Playgroud)
("foo"正确绑定到" http://www.foo.com " - 请参阅末尾的代码)
但是,当我想以命名空间不知道的方式做同样的事情时,虽然我可以通过依赖local-name()函数从元素中删除命名空间,但是我无法从属性中删除它们.
这是我能想到的最好的:
/*[local-name()='a']/*[local-name()='b' and @foo:bar=\"zar\"]
Run Code Online (Sandbox Code Playgroud)
(foo令人遗憾的是,用于限定bar属性的地方).
如何在完全删除命名空间的情况下编写上述表达式?
下面的代码已经在CLASSPATH上使用和不使用Saxon-HE-9.4.jar进行了测试并产生了正确的结果,但我无法从第二个XPath表达式中获取'foo'命名空间前缀!
import java.io.*;
import java.util.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.namespace.NamespaceContext;
public class FooMain {
public static void main(String args[]) throws Exception {
String xmlSample = "<foo:a xmlns:foo=\"http://www.foo.com\"><foo:b foo:bar=\"zar\"></foo:b></foo:a>";
XPath xpath = namespaceAwareXpath("foo", "http://www.foo.com");
{
System.out.printf("[NS-aware ] Number of 'b' nodes with foo:bar attribute …Run Code Online (Sandbox Code Playgroud) SSCCE:
import java.util.Objects;
public class FooMain {
private static Exception foo() {
try {
throw new Exception();
} catch (Exception e) {
return e;
}
}
public static void main(String args[]) {
final int N = 2;
Exception es[] = new Exception[N];
for (int i = 0 ; i < N ; i++)
es[i] = foo();
System.out.printf("Exceptions are equal? %b\n", Objects.equals(es[0], es[1]));
for (int i = 0 ; i < N ; i++) {
System.out.printf("follows exception %d:\n", i);
es[i].printStackTrace();
} …Run Code Online (Sandbox Code Playgroud) 为什么非验证的DocumentBuilder在SSCCE下面尝试读取DTD文件?
public class FooMain {
private static String XML_INSTANCE = "<?xml version=\"1.0\"?> "+
"<!DOCTYPE note SYSTEM \"does-not-exist.dtd\"> "+
"<a/> ";
public static void main(String args[]) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream is = new ByteArrayInputStream(XML_INSTANCE.getBytes("UTF-8"));
Document doc = builder.parse(is);
}
}
Run Code Online (Sandbox Code Playgroud)
代码爆炸:
[java] Exception in thread "main" java.io.FileNotFoundException: /lhome/minimal-for-SO/does-not-exist.dtd (No such file or directory)
[java] at java.io.FileInputStream.open(Native Method)
[java] at java.io.FileInputStream.<init>(FileInputStream.java:146)
[java] at java.io.FileInputStream.<init>(FileInputStream.java:101)
[java] at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
[java] …Run Code Online (Sandbox Code Playgroud) 我试图创建一个展示一个例子,为什么这个成语var that = this是必要的(例如,作为描述在这里).
所以,我从一个错误的代码示例开始,该代码无法正确绑定this.但是,我写的代码给了我一些意想不到的结果(在另一个方向):
message = "message in global scope";
// the below erroneous code aims to demonstrate why we need
// the "var that = this" idiom to capture the "this" lexical scope
var createLoggingFunctionWrongWay = function () {
return function () {
console.log(this.message);
};
};
// the below *does* print "message in global scope"
// i.e. fails in the way I expected
createLoggingFunctionWrongWay.call({message:"message"})();
// I was expecting the below to also …Run Code Online (Sandbox Code Playgroud) 我正在使用UriBuilder从 URI 中删除参数:
public static URI removeParameterFromURI(URI uri, String param) {
UriBuilder uriBuilder = UriBuilder.fromUri(uri);
return uriBuilder.replaceQueryParam(param, "").build();
}
public static String removeParameterFromURIString(String uriString, String param) {
try {
URI uri = removeParameterFromURI(new URI(uriString), param);
return uri.toString();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
Run Code Online (Sandbox Code Playgroud)
上述类型的作品和修改:
http://abc/d/e/f?foo=1&bar=2&zar=3
… 进入:
http://abc/d/e/f?bar=&foo=1&zar=3
但它存在以下问题:
是否有一些标准或常用的库可以巧妙地实现上述功能,而无需自己解析和破解查询字符串?
鉴于客户端程序员既定义又使用resolve传递给Promise的执行器的函数,我天真地假设我完全控制了resolve函数的签名,因此我可以定义它接受多个参数。
然而以下代码(jsFiddle)失败了:
<html>
<script>
const promise = new Promise(function executor(resolve, reject) {
setTimeout(function () {
resolve(42, true);
}, 1000);
});
promise.then(function resolveCallback(answer, howCertain) {
console.log('callback called with arguments: ('+answer+', '+howCertain+')');
console.log('the answer is: '+answer+'. I am '+(howCertain?'very':'not very')+' certain about that');
});
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
上面的代码实际上打印在控制台上:
callback called with arguments: (42, undefined)
the answer is: 42. I am not very certain about that
Run Code Online (Sandbox Code Playgroud)
再深入一点,我写了下面的代码(jsFiddle):
<html>
<script>
const promise = new Promise(function …Run Code Online (Sandbox Code Playgroud)