我在书中遇到了下面的groovy脚本代码.它给我带来了一些奇怪的输出.
class Person{
def work(){
println "work()"
}
def sports=['basketball','football','voleyball']
def methodMissing(String name, args){
if(name in sports){
println "injected ${name} into Person class"
Person instance=this
println "this.metaClass:\t\t${this.metaClass}"
println "instance.metaClass:\t${instance.metaClass}"
assert this.metaClass==instance.metaClass
}else{
println "no such method:${name}() in Person class"
}
}
}
def jack=new Person()
jack.football()
Run Code Online (Sandbox Code Playgroud)
它的输出如下:
injected football into Person class
this.metaClass: groovy.lang.MetaClassImpl@245b4bdc[class Person]
instance.metaClass: org.codehaus.groovy.runtime.HandleMetaClass@245b4bdc[groovy.lang.MetaClassImpl@245b4bdc[class Person]]
Caught: Assertion failed:
//I did not paste the detailed assertion here for simplicity
Run Code Online (Sandbox Code Playgroud)
所以我很困惑:
目前,我发现@ …
最近我正在网上学习Haskell,学习Haskell for Great Good.
我有两个问题:
fmap (replicate 3)是类型的Functor f=> f a -> f [a].为什么可以应用它Just?
此外,为什么是fmap (replicate 3) Just类型a -> [Maybe a],而不是类型a -> Maybe [a]?
在" 算法导论 - 创造性方法 "一书中,问题4.24:
设T1和T2为两个任意树,每个树有n个节点.证明足以将最多2n次旋转应用于T1,使其等于T2.
对于二叉搜索树,我想出了一个算法:
找到等于T2根的元素,我们称之为target-root.
使用AVL旋转策略,旋转target-root使其成为T1的新根.
在此过程中,可以执行多次旋转.
对于T1和T2的左子树,以递归方式处理它们.
对于T1和T2的右子树,以递归方式处理它们.
在最坏的情况下,该算法在O(N ^ 2)中运行.
我不太明白"任意树"这个短语,我无法弄清楚如何使T1等于T2.
有人可以帮忙解决这个问题吗?
在" 算法导论 - 创造性方法 "一书中,问题4.18:
第4.3.4节中介绍的AVL算法要求具有三个可能值的平衡因子:1,0或-1.为了表示三个值,我们需要2位.建议一种实现这些算法的方法(只需略微修改),每个节点只有1个额外的位.
我通过记录每个节点的高度而不是平衡因子来实现AVL树.
但我不知道如何只用1位表示三个值(1,0,-1).我想必须有一些其他信息可以用来表示1,0,-1和1位.
有人可以帮忙解决这个问题吗?
今天我按照以下说明成功升级了 docker:https://askubuntu.com/questions/472412/how-do-i-upgrade-docker.
但是,当我在浏览器中打开存储库url https://get.docker.com/ubuntu/时,它只是一个文本页面,其中包含一个列表bash commands.
我的问题是:
这个文本页面的URL如何适用?
apt只是运行bash命令吗?
如果是这样,为什么有相同的命令:
" echo deb https://get.docker.com/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
在 https://askubuntu.com/questions/472412/how-do-i-upgrade-docker中
在我写下面代码的地方,Haskell不会编译.
data Gang b a=Gang{getGang::(a,b)}
instance Monad (Gang String) where
return x = Gang (x,"")
(Gang(x,log)) >>=f = let Gang(x1,log1)= f x in Gang(x1,log++log1)
Run Code Online (Sandbox Code Playgroud)
编译器输出:
Illegal instance declaration for `Monad (Gang String)'
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use -XFlexibleInstances if you want to disable this.)
In the instance declaration for `Monad (Gang String)' …Run Code Online (Sandbox Code Playgroud)