我必须动态获取ES6类的属性和功能.这甚至可能吗?
使用for ... in循环,我只能循环遍历类实例的属性:
class Foo {
constructor() {
this.bar = "hi";
}
someFunc() {
console.log(this.bar);
}
}
var foo = new Foo();
for (var idx in foo) {
console.log(idx);
}
Run Code Online (Sandbox Code Playgroud)
输出:
bar
Run Code Online (Sandbox Code Playgroud) 我是json的新手.我有一个程序从json对象生成xml.
String str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}";
JSON json = JSONSerializer.toJSON( str );
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setTypeHintsCompatibility( false );
String xml = xmlSerializer.write( json );
System.out.println(xml);
Run Code Online (Sandbox Code Playgroud)
输出是:
<?xml version="1.0" encoding="UTF-8"?>
<o><array json_class="array"><e json_type="number">1</e><e json_type="number">2</e><e json_type="number">3</e></array><boolean json_type="boolean">true</boolean><double json_type="number">2.0</double><integer json_type="number">1</integer><name json_type="string">JSON</name><nested json_class="object"><id json_type="number">42</id></nested></o>
Run Code Online (Sandbox Code Playgroud)
我最大的问题是如何编写自己的属性而不是json_type ="number",还要编写自己的子元素.
我是Haskell的新手,但了解如何使用Monad变形金刚.然而,我仍然难以获得他们声称的优势,而不是将参数传递给函数调用.
基于wiki Monad Transformers Explained,我们基本上将Config对象定义为
data Config = Config Foo Bar Baz
Run Code Online (Sandbox Code Playgroud)
传递它,而不是用这个签名编写函数
client_func :: Config -> IO ()
Run Code Online (Sandbox Code Playgroud)
我们使用ReaderT Monad Transformer并将签名更改为
client_func :: ReaderT Config IO ()
Run Code Online (Sandbox Code Playgroud)
然后拉动Config只是一个电话ask
.
函数调用从更改client_func c
为runReaderT client_func c
精细.
但为什么这会使我的应用程序变得更简单?
1-我怀疑当你将许多功能/模块拼接在一起形成一个应用程序时,Monad变形金刚会感兴趣.但这就是我的理解停止的地方.有人可以请一些亮点吗?
2-我找不到任何关于如何在Haskell中编写大型模块化应用程序的文档,其中模块公开某种形式的API并隐藏它们的实现,以及(部分地)将其自己的状态和环境隐藏在其他模块中.有什么指针吗?
(编辑:真实世界Haskell声称"......这种方法[Monad变形金刚] ......扩展到更大的程序.",但没有明确的例子证明这种说法)
编辑关注Chris Taylor以下答案
克里斯完美地解释了为什么封装Config,State等等......在Transformer Monad中提供了两个好处:
getUserInput
函数)Writer
以提供更低级别功能的Logging)这是以更改所有功能的签名为代价的,以便它们在Transformer Monad中"运行".
所以问题1已完全涵盖.谢谢克里斯.
现在在这篇SO帖子中回答了问题2
为了减少可变性,我们应该使用
public void setValues(String[] newVals) {
this.vals = ( newVals == null ? null : newVals.clone() );
}
Run Code Online (Sandbox Code Playgroud)
要么
public void setValues(String[] newVals) {
this.vals = ( newVals == null ? null : Arrays.copyOf(newVals, newVals.length) );
}
Run Code Online (Sandbox Code Playgroud) 我使用的是Mac OS X Sierra,我发现clang(LLVM版本8.1.0(clang-802.0.38))不支持OpenMP:当我运行时clang -fopenmp program_name.c
,我收到以下错误:
clang: error: unsupported option '-fopenmp'
似乎clang不支持-fopenmp
旗帜.
我在自制软件中找不到任何openmp库.根据LLVM网站,LLVM已经支持OpenMP.但是在编译期间我找不到启用它的方法.
这是否意味着Mac中的默认clang不支持OpenMP?你能提供什么建议吗?
(当我切换到GCC编译相同的程序(使用gcc安装brew install gcc --without-multilib
)时,编译成功.)
我想提出一个模块化的程序设计,我再次请求你的帮助.
作为以下帖子的后续Monad变形金刚与Haskell中的传递参数和大规模设计,我正在尝试构建两个独立模块,使用Monad变换器但暴露Monad不可知函数,然后结合Monad不可知函数来自每个这些模块成为一个新的Monad不可知功能.
我已经无法运行相结合的功能,例如我怎么打电话mainProgram
使用runReaderT
下面的例子?
附属问题是:是否有更好的方法来实现相同的模块化设计目标?
该示例有两个模拟模块(但编译),一个执行日志记录,另一个读取用户输入并对其进行操作.组合功能读取用户输入,记录并打印它.
{-# LANGUAGE FlexibleContexts #-}
module Stackoverflow2 where
import Control.Monad.Reader
----
---- From Log Module - Writes the passed message in the log
----
data LogConfig = LC { logFile :: FilePath }
doLog :: (MonadIO m, MonadReader LogConfig m) => String -> m ()
doLog _ = undefined
----
---- From UserProcessing Module - Reads the user Input and changes it to the configured case …
Run Code Online (Sandbox Code Playgroud) 在设计定义文件时,我遇到了这个错误(TS2507).
如何将类型指定为"构造函数类型"?
我们有一个java web服务器,可以通过h2c(HTTP/2明文)提供内容
我们希望将使用h2(即SSL上的标准HTTP/2)建立的代理连接反向到h2c中的java服务器.
在nginx上启用HTTP/2非常简单,处理传入的h2连接工作正常.
我们如何告诉nginx使用h2c代替连接而不是http/1.1?
注意:非nginx解决方案可能是可以接受的
server {
listen 443 ssl http2 default_server;
server_name localhost;
ssl_certificate /opt/nginx/certificates/???.pem;
ssl_certificate_key /opt/nginx/certificates/???.pk8.key.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://localhost:8080/; ## <---- h2c here rather than http/1.1
}
}
Run Code Online (Sandbox Code Playgroud)
结论 (2016年6月)
这可以使用haproxy使用配置文件来完成,如下所示.
查询(HttpServletRequest)
req.getProtocol()
清楚回报HTTP/2.0
global
tune.ssl.default-dh-param 1024
defaults
timeout connect 10000ms
timeout client 60000ms
timeout server 60000ms
frontend fe_http
mode http
bind *:80
# Redirect to https
redirect scheme https code 301
frontend fe_https
mode …
Run Code Online (Sandbox Code Playgroud) then(res => res.json())
react-native提取中的以下代码段是什么意思?
fetch(url)
.then(res => res.json())
.then(res => {
this.setState({
data: res,
error: res.error || null,
loading: false
});
Run Code Online (Sandbox Code Playgroud) 我想将有线格式(如 JSON)反序列化为下面的结构,但我无法为相应的 rust 类型Data
编写 serde实现。Deserialize
{ "type": "TypeA", "value": { "id": "blah", "content": "0xa1b.." } }
Run Code Online (Sandbox Code Playgroud)
enum Content {
TypeA(Vec<u8>),
TypeB(BigInt),
}
struct Value {
id: String,
content: Content,
}
struct Data {
typ: String,
value: Value,
}
Run Code Online (Sandbox Code Playgroud)
困难在于选择正确的枚举值Content
,这是基于typ
值的。据我所知,serde 中的反序列化是无状态的,因此没有办法
typ
知道反序列化时的值是什么content
(即使反序列化顺序是有保证的)typ
解串器然后收集它。如何使用 serde 来实现这一点?
我看过
Deserialize
所有类型,并且我的数据模型很大现有的 SO 答案通常利用相关领域处于同一级别的事实。这里的情况并非如此:实际的数据模型很大、很深,而且字段“相距很远”
java ×3
haskell ×2
javascript ×2
clang ×1
ecmascript-6 ×1
fetch-api ×1
haproxy ×1
http2 ×1
immutability ×1
json ×1
jsx ×1
llvm ×1
macos ×1
nginx ×1
oop ×1
openmp ×1
react-native ×1
rust ×1
serde ×1
typescript ×1
xml ×1
xpath ×1