小编Guo*_*eng的帖子

Groovy:this.metaClass与instance.metaClass相对应

我在书中遇到了下面的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)

所以我很困惑:

  1. 为什么this.metaClass不等于instance.metaClass?
  2. 更进一步,我不能使用this.metaClass来注入新方法; groovy告诉我this.metaClass没有这样的属性,我打算注入.
  3. "org.codehaus.groovy.runtime.HandleMetaClass@245b4bdc [groovy.lang.MetaClassImpl@245b4bdc [class Person]]"是什么意思?我知道"245b4bdc"可能是对象指针.但是为什么HandleMetaClass和MetaClassImpl具有相同的指针值"245b4bdc"?

目前,我发现@ …

groovy metaclass this

8
推荐指数
1
解决办法
687
查看次数

为什么"fmap(replicate 3)Just"在Haskell中有一种"a - > [Maybe a]"

最近我正在网上学习Haskell,学习Haskell for Great Good.

我有两个问题:

  1. fmap (replicate 3)是类型的Functor f=> f a -> f [a].为什么可以应用它Just

  2. 此外,为什么是fmap (replicate 3) Just类型a -> [Maybe a],而不是类型a -> Maybe [a]

haskell types functor

7
推荐指数
1
解决办法
224
查看次数

证明两棵树的最大转数变得相等

在" 算法导论 - 创造性方法 "一书中,问题4.24:

设T1和T2为两个任意树,每个树有n个节点.证明足以将最多2n次旋​​转应用于T1,使其等于T2.

对于二叉搜索树,我想出了一个算法:

  1. 找到等于T2根的元素,我们称之为target-root.

  2. 使用AVL旋转策略,旋转target-root使其成为T1的新根.
    在此过程中,可以执行多次旋转.

  3. 对于T1和T2的左子树,以递归方式处理它们.
    对于T1和T2的右子树,以递归方式处理它们.

在最坏的情况下,该算法在O(N ^ 2)中运行.

我不太明白"任意树"这个短语,我无法弄清楚如何使T1等于T2.

有人可以帮忙解决这个问题吗?

algorithm tree avl-tree tree-rotation

5
推荐指数
1
解决办法
945
查看次数

实现平衡因子,每个节点只有1个额外位

在" 算法导论 - 创造性方法 "一书中,问题4.18:

第4.3.4节中介绍的AVL算法要求具有三个可能值的平衡因子:1,0或-1.为了表示三个值,我们需要2位.建议一种实现这些算法的方法(只需略微修改),每个节点只有1个额外的位.

我通过记录每个节点的高度而不是平衡因子来实现AVL树.

但我不知道如何只用1位表示三个值(1,0,-1).我想必须有一些其他信息可以用来表示1,0,-1和1位.

有人可以帮忙解决这个问题吗?

algorithm avl-tree

5
推荐指数
1
解决办法
485
查看次数

为什么"https://get.docker.com/ubuntu"是一个apt存储库?

今天我按照以下说明成功升级了 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中

apt docker

3
推荐指数
1
解决办法
2265
查看次数

为什么"实例Monad(Gang String)在哪里"编译错误

在我写下面代码的地方,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)

haskell typeclass

2
推荐指数
1
解决办法
114
查看次数