我想用c ++(或c ++ 0x)编写一个指针,指向一个类的运算符,让我们说A或B.有什么方法可以做到吗?
当然有一种语法
int (A::*_p) ();
Run Code Online (Sandbox Code Playgroud)
但它并没有解决这个问题.我想制作通用指针,而不是为它指定基类 - 只有"运算符函数"的指针
#include <thread>
#include <iostream>
using namespace std;
class A
{
public:
int operator()()
{
return 10;
}
};
class B
{
public:
int operator()()
{
return 11;
}
};
int main()
{
A a;
int (*_p) ();
_p = a.operator();
cout << _p();
B b;
_p = b.operator();
cout << _p();
}
Run Code Online (Sandbox Code Playgroud) 我想问你是什么导致这种差异.如果我编译以下程序并运行相同的二进制文件 - 在某些平台上,由C++代码生成的程序比Haskell程序快得多,在其他情况下则相反.
另外,根据它们构建的平台,最终二进制文件的性能有很大差异.(每个平台使用相同的标志和相同版本的LVM和clang)
代码已经过优化,应该可以使用相同的性能 - 请参阅:Haskell能否以与Clang/GCC相同的方式优化函数调用?.
我想问你,怎么可能.
C++代码:
#include <cstdio>
#include <cstdlib>
int b(const int x){
return x+5;
}
int c(const int x){
return b(x)+1;
}
int d(const int x){
return b(x)-1;
}
int a(const int x){
return c(x) + d(x);
}
int main(int argc, char* argv[]){
printf("Starting...\n");
long int iternum = atol(argv[1]);
long long int out = 0;
for(long int i=1; i<=iternum;i++){
out += a(iternum-i);
}
printf("%lld\n",out);
printf("Done.\n");
}
Run Code Online (Sandbox Code Playgroud)
用.编译 clang++ -O3 main.cpp
haskell代码:
module Main …Run Code Online (Sandbox Code Playgroud) 是否可以编写Cabal包含多个配置文件的配置文件Library sections?
我在文档中找到了Library 部分和Executables 部分的描述,看起来似乎不可能在一个Cabal配置文件中放入更多的Library部分.
但是,如果我
同时开发几个Haskell库和几个可执行文件并想要编译和测试它们,我该怎么办?
问题
你好!我在Cloud Haskell中编写了一个简单的Server-Worker程序.问题是,当我尝试创建时ManagedProcess,在服务器发送步骤之后,我的示例即使在使用时也会永久挂起callTimeout(在100毫秒后应该会中断).代码很简单,但我发现它没有任何问题.
我也在邮件列表上发布了这个问题,但就我所知的SO社区而言,我在这里可以更快地得到答案.如果我从邮件列表中得到答案,我也会在这里发帖.
源代码
的Worker.hs:
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Network.Transport (EndPointAddress(EndPointAddress))
import Control.Distributed.Process hiding (call)
import Control.Distributed.Process.Platform hiding (__remoteTable)
import Control.Distributed.Process.Platform.Async
import Control.Distributed.Process.Platform.ManagedProcess
import Control.Distributed.Process.Platform.Time
import Control.Distributed.Process.Platform.Timer (sleep)
import Control.Distributed.Process.Closure (mkClosure, remotable)
import Network.Transport.TCP (createTransport, defaultTCPParameters)
import Control.Distributed.Process.Node hiding (call)
import Control.Concurrent (threadDelay)
import GHC.Generics (Generic)
import Data.Binary (Binary)
import Data.Typeable (Typeable)
import Data.ByteString.Char8 (pack)
import System.Environment …Run Code Online (Sandbox Code Playgroud) 我在Haskell中使用SBV(带有Z3后端)来创建一些理论证明.我想检查forall x和y给定的约束(比如x + y = y + x,哪里+是"加上运算符",而不是添加)其他一些术语是有效的.我想定义关于+表达式的公理(比如关联性,身份等),然后检查是否有进一步的等式,比如检查是否a + (b + c) == (a + c) + b有效正式a,b和c.
我试图通过以下方式实现它:
main = do
let x = forall "x"
let y = forall "y"
out <- prove $ (x .== x)
print "end"
Run Code Online (Sandbox Code Playgroud)
但似乎我们不能.==在符号值上使用运算符.这是缺少的功能还是错误的用法?我们能够以某种方式使用SBV吗?
I've noticed that in the newest MacOS the sandbox-exec command is deprecated. According to it's manual:
The sandbox-exec command is DEPRECATED. Developers who wish to sandbox an app should instead adopt the App Sandbox feature described in the App Sandbox Design Guide. [...]
Moreover, a few commands were removed, like sandbox-simplify. Also, it seems that the trace function is not working anymore, the following config just does not produce output anymore (while it did in earlier versions):
(version 1) …Run Code Online (Sandbox Code Playgroud) 我想用纯C++ 11等效替换顶点或边上的BGL迭代.BGL代码(来自:http://www.boost.org/doc/libs/1_52_0/libs/graph/doc/quick_tour.html)是:
typename boost::graph_traits<Graph>::out_edge_iterator out_i, out_end;
typename boost::graph_traits<Graph>::edge_descriptor e;
for (std::tie(out_i, out_end) = out_edges(v, g);
out_i != out_end; ++out_i)
{
e = *out_i;
Vertex src = source(e, g), targ = target(e, g);
std::cout << "(" << name[get(vertex_id, src)]
<< "," << name[get(vertex_id, targ)] << ") ";
}
Run Code Online (Sandbox Code Playgroud)
我从这里尝试了几个建议:用"纯"C++ 11替换替换BOOST_FOREACH?但没有运气.
我希望能够写出如下内容:
for (auto &e : out_edges(v, g))
{ ... }
Run Code Online (Sandbox Code Playgroud)
或类似的东西:
for (std::tie(auto out_i, auto out_end) = out_edges(v, g);
out_i != out_end; ++out_i)
{...}
Run Code Online (Sandbox Code Playgroud)
可能吗?
我正在阅读很多关于Haskell Parser Combinators的内容,并发现了许多主题,如:
但是,所有这些话题比较Parser Combinators有Parser Generators.
我想问你哪个Parser Combinator最适合以下条件:
我发现,最流行的解析器组合器是:
我喜欢镜头库,我喜欢它的工作方式,但有时它引入了很多问题,我后悔自己开始使用它.让我们看看这个简单的例子:
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens
data Data = A { _x :: String, _y :: String }
| B { _x :: String }
makeLenses ''Data
main = do
let b = B "x"
print $ view y b
Run Code Online (Sandbox Code Playgroud)
它输出:
""
Run Code Online (Sandbox Code Playgroud)
现在想象 - 我们有一个数据类型,我们通过改变一些名称来重构它.这个名称不再适用于特定的数据构造函数,而是获得错误(在运行时,与普通访问器一样),镜头使用memptyfrom Monoid来创建默认对象,因此我们得到奇怪的结果而不是错误.调试这样的东西几乎是不可能的.有没有办法解决这个问题?我知道有一些特殊的操作员可以获得我想要的行为,但是所有来自镜头的"正常"外观功能都非常糟糕.我应该用我的自定义模块覆盖它们还是有更好的方法?
作为旁注:我希望能够使用镜头语法读取和设置参数,但只删除字段缺失时自动结果创建的行为.
haskell ×6
c++ ×3
c++11 ×2
performance ×2
attoparsec ×1
boost ×1
boost-graph ×1
build ×1
build-system ×1
cabal ×1
cloud ×1
deprecated ×1
haskell-lens ×1
html5 ×1
iterator ×1
javascript ×1
macos ×1
optimization ×1
parsec ×1
parsing ×1
pointers ×1
sandbox ×1
sbv ×1
security ×1
three.js ×1
webgl ×1
z3 ×1