该MonadTrans文件说:
每个monad变换器还带有一个操作runXXX来打开变换器,暴露内部monad的计算.
所以我想知道为什么MonadTrans没有定义为
class MonadTrans t where
type p :: *
type r :: * -> *
lift:: Monad m => m a -> t m a
run :: t m a -> p -> m (r a)
Run Code Online (Sandbox Code Playgroud)
消除上述条款?那上面的定义不够通用吗?如果是这样,哪个monad变压器不适合这个定义?
UPDATE
调整一点以启用不同的结果类型.
该混帐svn的文件说:
--username =对于SVN处理(http,https和plain svn)身份验证的传输,请指定用户名.对于其他传输(例如svn + ssh://),您必须在URL中包含用户名,例如svn + ssh://foo@svn.bar.com/project
但是,在我的电脑(Windows 7,git版本1.9.4.msysgit.0)中,当我使用时
git svn clone https://server/svn/repo
Run Code Online (Sandbox Code Playgroud)
它首先询问我的Windows用户名的密码,然后(失败,因为服务器使用svn auth)询问服务器用户名和密码.一切正常.
当我尝试克隆另一个repo时会发生问题.由于此repo仅对其他用户可用,因此我使用以下命令.
git svn clone https://server/svn/repo1 --username xxx
Run Code Online (Sandbox Code Playgroud)
我原本应该要求密码,但事实并非如此.它只是因403错误而失败.
我还尝试克隆了可供两个用户使用的第三个repo,但我想指定用户为后者,
git svn clone https://server/svn/repo2 --username xxx
Run Code Online (Sandbox Code Playgroud)
它没有任何密码提示成功.由于密码不同,因此必须使用以前的用户凭证.
后来我认识到我将使用
git svn clone https://server/svn/repo2 --username=xxx
Run Code Online (Sandbox Code Playgroud)
但它仍然是一回事.
所以它看起来--username=xxx没有做任何事情.如何解决这个问题?
在正常的C#中我们写
int DoSomething(){/*...*/)};
lock(mutex)
{
return DoSomething();
}
Run Code Online (Sandbox Code Playgroud)
确保在所有情况下都mutex被释放.
但如果签名DoSomething改为
Task<int> DoSomeThingAsync(){/*...*/};
Run Code Online (Sandbox Code Playgroud)
是否有以下代码
return Task.Factory.StartNew(() =>
{
Monitor.Enter(mutex);
return DoSomethingAsync();
}).Unwrap().ContinueWith(t =>
{
Monitor.Exit(mutex);
return t;
}).Unwrap();
Run Code Online (Sandbox Code Playgroud)
做类似的事情?是否保证mutex在输入时释放?有没有更简单的方法呢?(我无法使用async关键字,所以请继续考虑TPL)
显然,MonadConts更受限制,并且比普通的Monads 更强大,这要归功于它callCC.这意味着它的实例越来越少,你可以用它做更多的事情.
当看的定义的实例MonadCont,它看起来像一切所列要求或者Cont或者ContT或者已经存在的MonadCont实例.这意味着我们必须开始与一些Cont或者ContT,特别是不能把IO成MonadCont.
但是,我认为callCC在IO上下文中使用是有意义的,因此我们可以简化以下内容(根据官方Hackage页面 callCC示例进行调整):
whatsYourName :: IO ()
whatsYourName = do
name <- getLine
let response = flip runCont id $ do
callCC $ \exit -> do
when (null name) (exit "You forgot to tell me your name!")
return $ "Welcome, " ++ name ++ "!"
print response
Run Code Online (Sandbox Code Playgroud)
成
whatsYourName' :: …Run Code Online (Sandbox Code Playgroud) 我听说在Haskell中我们可以MonadFix用来访问将来要评估的值.但我认为Monads只是语法糖,所以应该有类似的东西,可以在纯函数中实现.所以我尝试了以下方法:
timemachine :: [a] -> (a -> Int -> b -> b) -> b -> b
timemachine al f b = result where
~(total, result) = foldr app (0,b) al
app a (i,b1) = (i+1, f a (total - i) b1)
main :: IO ()
main = print $ timemachine "ddfdfeef" (\x i y -> (x,i):y) []
Run Code Online (Sandbox Code Playgroud)
但预计不会产出:
[('d',1),('d',2),('f',3),('d',4),('f',5),('e',6),('e',7),('f',8)]
Run Code Online (Sandbox Code Playgroud)
理想情况下,结果应该是
[('d',8),('d',7),('f',6),('d',5),('f',4),('e',3),('e',2),('f',1)]
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
我想挑战GHC编译器,所以我编写了这段代码(代码的细节实际上并不重要,只是为了表明必须付出一些努力来获得这个无限列表的每个元素):
hardwork :: [Int]
hardwork = step1 where
-- start with value 1
step1 = step2 1
-- force the hard work being done
step2 i = step3 i $! step4 i
-- construct the current result, start next
step3 i result = result : step2 (i+1)
-- start the work with n=0 and j=1
step4 i = step5 i 0 1
-- if n=1048576, finish work and return j
step5 _ 1048576 j = j
-- otherwise increase n, …Run Code Online (Sandbox Code Playgroud) 我目前正在使用.NET 4和Visual Studio 2010.MSDN表示Task.Delay在此版本的.NET框架中不可用.
但是,在我的系统中,ILdasm显示Task.Delay在mscorlib(4.0.0.0)中存在.所以我测试使用反射:
typeof (Task).GetMethod("Delay", new []{typeof (int)})
.Invoke(null, new object[]{1000});
Run Code Online (Sandbox Code Playgroud)
这似乎适用于我的电脑.所以我的问题是:
这是否意味着微软以某种方式提供了这种方法,但它们只是隐藏了它?
使用包装函数以上述方式使用它是否安全?更确切地说,这是否适用于仅安装了.NET 4的其他计算机?(我的系统是Windows 7)
UPDATE
我忘了天气或者没有安装.NET 4.5.所以我从控制面板检查.是的,安装了.NET 4.5.1.
我编写了以下简单测试:
[Test]
public void TestUTF8()
{
var c = "abc?def";
var b = Encoding.UTF8.GetBytes(c);
Assert.That(b.Length, Is.EqualTo(9));
//Assuming, you are reading a byte stream and got partial result with the first 5 bytes
var p = Encoding.UTF8.GetChars(b, 0, 5);
Trace.WriteLine(new string(p));
Assert.That(p.Length, Is.EqualTo(3));
}
Run Code Online (Sandbox Code Playgroud)
在Trace输出abc?最后断言失败,因为p.Length是4。
但是,我需要Trace输出abc和最后一个断言传递,因为实际上我知道流将具有有效的字符,并且当最后几个字节不是这种情况时,只需将它们留在那里等待更多数据来临。
那么如何在C#中实现呢?
以下没有编译:
use std::any::Any;
pub trait CloneBox: Any {
fn clone_box(&self) -> Box<dyn CloneBox>;
}
impl<T> CloneBox for T
where
T: Any + Clone,
{
fn clone_box(&self) -> Box<dyn CloneBox> {
Box::new(self.clone())
}
}
struct Foo(Box<dyn CloneBox>);
impl Clone for Foo {
fn clone(&self) -> Self {
let Foo(b) = self;
Foo(b.clone_box())
}
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
error[E0495]: cannot infer an appropriate lifetime for pattern due to conflicting requirements
--> src/lib.rs:20:17
|
20 | let Foo(b) = self;
| ^
|
note: …Run Code Online (Sandbox Code Playgroud) 我有一个F#函数,如下所示:
open System.IO
open Microsoft.FSharp.Control.CommonExtensions
let rec copyData (ins:Stream) (outs:Stream) = async {
let! bytes = ins.AsyncRead(1)
do! outs.AsyncWrite(bytes)
return! moveData ins outs
}
Run Code Online (Sandbox Code Playgroud)
当ins流到达结尾时,它会抛出AtEndOfStream异常.所以我必须在调用函数中捕获它.如何通过检测流目前结束来防止此异常?