我目前正在玩Bryan O'Sullivan的资源池库,并且有关于扩展withResource功能的问题.我想将withResource函数的签名更改 (MonadBaseControl IO m) => Pool a -> (a -> m b) -> m b为(MonadBaseControl IO m) => Pool a -> (a -> m (Bool, b)) -> m b.
我想要实现的是,操作应该返回(Bool, b)元组,其中布尔值指示是否应该将借来的资源放回池中或销毁.
现在我的当前实现看起来像这样:
withResource :: forall m a b. (MonadBaseControl IO m) => Pool a -> (a -> m (Bool, b)) -> m b
{-# SPECIALIZE withResource :: Pool a -> (a -> IO (Bool,b)) -> IO b #-} …Run Code Online (Sandbox Code Playgroud) 我对C ++的私有继承有疑问。请参见以下代码示例:
#include <iostream>
class Foo {
public:
virtual void doSomething(int value) {
std::cout << value << std::endl;
}
};
void foobar(Foo& foo, int value) {
foo.doSomething(value);
}
class Bar : Foo {
public:
Bar() {
foobar(*this, 42); // <--- is OK despite private inheritance
}
};
int main() {
Bar b;
foobar(b, 42); // <--- is NOT OK because of private inheritance
}
Run Code Online (Sandbox Code Playgroud)
目前,我无法理解(或找到正确的C ++规范)为何尽管有私有继承foobar也可以在Bar的构造函数中调用该函数。如果我尝试调用函数对象的函数,编译器为如预期,因为私有继承的错误。*this foobarBarbmain
foobar(*this, 42)和foobar(b, …
请考虑以下代码示例
#include <iostream>
#include <experimental/optional>
std::experimental::optional<int> dflt(42);
template<const std::experimental::optional<int>& Dflt>
void foo() {
if (Dflt) {
std::cout << "default is set" << std::endl;
} else {
std::cout << "default is not set" << std::endl;
}
}
int main() {
foo<dflt>(); // <-- OK
foo<std::experimental::nullopt>(); // <-- NOT OK
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的是nullopt作为非类型函数模板参数传递但它不编译.它适用于dflt具有静态存储的全局变量.
编译器错误消息如下所示:
foo.cc: In function ‘int main()’:
foo.cc:13:34: error: no matching function for call to ‘foo()’
foo<std::experimental::nullopt>();
^
foo.cc:7:6: note: candidate: template<const std::experimental::fundamentals_v1::optional<int>& Dflt> void foo()
void …Run Code Online (Sandbox Code Playgroud) 我目前正试图通过解决一些Hackerrank问题来刷新我的Haskell知识.
例如:
https://www.hackerrank.com/challenges/maximum-palindromes/problem
我已经在C++中实现了一个必需的解决方案,它已被所有测试用例所接受.现在我试图在(合理的惯用)Haskell中提出一个纯粹的功能解决方案.
我目前的代码是
module Main where
import Control.Monad
import qualified Data.ByteString.Char8 as C
import Data.Bits
import Data.List
import qualified Data.Map.Strict as Map
import qualified Data.IntMap.Strict as IntMap
import Debug.Trace
-- precompute factorials
compFactorials :: Int -> Int -> IntMap.IntMap Int
compFactorials n m = go 0 1 IntMap.empty
where
go a acc map
| a < 0 = map
| a < n = go a' acc' map'
| otherwise = map'
where
map' = IntMap.insert a acc …Run Code Online (Sandbox Code Playgroud) 我目前正在使用Emacs作为我开发Haskell代码的主要IDE,到目前为止我真的很满意.但目前我无法弄清楚一个小细节,即如何自定义缩进宽度为4而不是2.
目前,我已经把上haskell-indentation的haskell-mode,但我想不出我有设置自定义压痕宽度什么变化.到目前为止,我已尝试设置,'(haskell-indent-spaces 4)但这似乎没有任何影响......
请提前获取任何帮助!
我目前正在尝试更深入地学习Emacs,以便我可以使用我的Emacs做更多的事情,而不仅仅是简单的编辑...我正在取得良好的进展,目前我正在尝试将'sr-speedbar'模块配置为我的喜欢,但有些细节我无法弄清楚自己:
你们有关于快速栏导航的更多有用提示吗?Emacs大师如何使用speedbar或者有更好的选择?
Thx提前为您提供帮助!
是否可以在GHCi中编写带有类型签名的多行函数定义(就像将其编写在源文件中一样)?
\n\n到目前为止我已经尝试过这样的事情:
\n\nPrelude> :{\nPrelude| let f :: Int -> Int;\nPrelude| f i = i + 1\nPrelude| :}\n\n<interactive>:9:1: parse error on input \xe2\x80\x98f\xe2\x80\x99\nRun Code Online (Sandbox Code Playgroud)\n\n但它不起作用...还有什么我可以尝试的吗?
\n我目前正在阅读(出于自学目的)Bryan O'Sullivan 的流行pool 库的源代码。
我在函数中有一个问题takeResource,我想在这里询问Haskell专家。该函数定义为:
takeResource :: Pool a -> IO (a, LocalPool a)
takeResource pool@Pool{..} = do
local@LocalPool{..} <- getLocalPool pool
resource <- liftBase . join . atomically $ do
ents <- readTVar entries
case ents of
(Entry{..}:es) -> writeTVar entries es >> return (return entry)
[] -> do
used <- readTVar inUse
when (used == maxResources) retry
writeTVar inUse $! used + 1
return $
create `onException` atomically (modifyTVar_ inUse (subtract 1))
return (resource, …Run Code Online (Sandbox Code Playgroud) 我试图const更深入地理解c ++的语义,但我无法完全理解constness保证值得的是什么.正如我所看到的,constness保证不会有变异,但请考虑以下(人为的)示例:
#include <iostream>
#include <optional>
#include <memory>
class A {
public:
int i{0};
void foo() {
i = 42;
};
};
class B {
public:
A *a1;
A a2;
B() {
a1 = &a2;
}
void bar() const {
a1->foo();
}
};
int main() {
B b;
std::cout << b.a2.i << std::endl; // output is 0
b.bar();
std::cout << b.a2.i << std::endl; // output is 42
}
Run Code Online (Sandbox Code Playgroud)
因为bar就是const,人们会认为它不会发生变异对象b.但是在它的调用b发生变异之后.如果我写的方法foo …
haskell ×5
c++ ×3
emacs ×2
c++17 ×1
const ×1
editor ×1
ghci ×1
haskell-mode ×1
inheritance ×1
monads ×1
performance ×1