迭代 Scala 包装列表

发布于 2024-11-08 23:43:09 字数 1592 浏览 0 评论 0原文

我需要将列表封装在 Scala 中的一个单独的对象中,作为我的集合的包装类。我需要它来实现一些方法来处理列表(具体来说,我需要在列表中查找与列表中其他对象关联的对象)。

首先,我的代码:

object Axons {
    var axonList=List[Axon]();
    var pos=0;
    def init(al: List[Axon]) {
        axonList= al;
    }
    def reverse(): List[Axon]  = axonList.reverse
    def get(count: Int) = axonList(count)
    def getList(): List[Axon] = axonList
    def length(): Int = axonList.length

}

现在迭代的情况如下:

for (axon <- axons.getList)

这对我来说看起来有点难看,但我无法弄清楚如何实现可多次用于迭代集合的迭代器。

另一种方法,仅使用普通列表,是定义一个函数,该函数使用 Fold 来缩小列表,仅保存我想要的对象。

您认为更常见的方式是什么? 过滤到仅包含所需对象或使用迭代器的单独列表。 在我看来,从软件设计的角度来看,将我的 Axons 集合封装在一个单独的对象中会更干净。

您认为什么最适合解决该问题?

(如果你问自己我在做什么以及轴突是什么;它是神经网络的一部分 http: //en.wikipedia.org/wiki/Neural_network 和 Axons 集合包装了源神经元和目标神经元之间的连接)

感谢和亲切的问候

===================================================

解决方案来自菲利克斯

object Axons extends Traversable[Axon] {
    var axonList=List[Axon]();
    def init(al: List[Axon]) {
        axonList= al;
    }
    def reverse(): List[Axon]  = axonList.reverse
    def get(count: Int) = axonList(count)

        //look here!
    def foreach[U](f: Axon=> U): Unit = axonList.foreach(f)
    //end ... 
    def length(): Int = axonList.length
    def findAxonsBySource(sourceNeuron: Neuron): List[Axon] = {
        axonList collect { 
            case axon: Axon if axon.getSourceNeuron == sourceNeuron  => axon 
            }
    }
}

I need to encapsulate a list in a separate Object in Scala acting as a wrapper class for my collection. I need this to implement some methods to act with the list (in detail I need to find objects in the List which are associated with other object in the list).

So first of all my code:

object Axons {
    var axonList=List[Axon]();
    var pos=0;
    def init(al: List[Axon]) {
        axonList= al;
    }
    def reverse(): List[Axon]  = axonList.reverse
    def get(count: Int) = axonList(count)
    def getList(): List[Axon] = axonList
    def length(): Int = axonList.length

}

Now iterating happens like:

for (axon <- axons.getList)

This looks kinda ugly to me, but I could not figure out how to implement an Iterator which is usable multiple times to iterate over the collection.

Another approach, just using the plain list, is to define a function which uses fold to shrink the list just holding the objects I want to have.

What you think is a more common way?
Filter to a seperate list just holding needed objects or using an iterator.
In my opinion it would be cleaner to encapsulate my Axons collection in a seperate object, from a software desing point of view.

What you think fits best for the problem?

(If you ask yourself what I'm doing and what an axon is; its a part of a neural network http://en.wikipedia.org/wiki/Neural_network and the Axons collection wraps the connections between a source and a destination neuron)

thanks and kind regards

=================================================

Solution from Felix

object Axons extends Traversable[Axon] {
    var axonList=List[Axon]();
    def init(al: List[Axon]) {
        axonList= al;
    }
    def reverse(): List[Axon]  = axonList.reverse
    def get(count: Int) = axonList(count)

        //look here!
    def foreach[U](f: Axon=> U): Unit = axonList.foreach(f)
    //end ... 
    def length(): Int = axonList.length
    def findAxonsBySource(sourceNeuron: Neuron): List[Axon] = {
        axonList collect { 
            case axon: Axon if axon.getSourceNeuron == sourceNeuron  => axon 
            }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热风软妹 2024-11-15 23:43:10

我不确定这一点:

for (axon <- axons.getList)

编译。你的意思是把 Axons 写成大写,对吗?

不管怎样,你可以做这样的事情:

object Test extends Traversable[Int]{
val list = List(1,2,1337,4)
def foreach[U](f: Int=> U): Unit = list.foreach(f)
}

并像这样测试它:

Test foreach println
1
2
1337
4

我不完全确定我正确理解了这个问题,但我希望它有帮助:)

编辑

只是对你的帖子的一些想法。 Scala 的好处是有时您可以跳过在 Java 中被迫执行的步骤。不要为所有内容创建类和大类型方案,除非您认为确实合理。我的方法总是折叠/映射/过滤等等,直到我得到测试我的软件所需的东西。如果我需要提高性能/模块化程度,我可以开始重做部分软件。大多数情况下,我的程序都很短,很少需要重做。
我将 Scala 的功能视为极大减少样板代码的礼物。

I'm not sure this:

for (axon <- axons.getList)

compiles. You meant to put Axons in upper case right?

Anyway, you can do something like this:

object Test extends Traversable[Int]{
val list = List(1,2,1337,4)
def foreach[U](f: Int=> U): Unit = list.foreach(f)
}

and test it like so:

Test foreach println
1
2
1337
4

I am not totally sure I understood the problem correctly, but I hope it helps :)

edit

Just some thoughts on your post. The good thing about Scala is that you can sometimes skip steps that you would be forced to take in Java for example. Don't create classes and big type schemes for everything unless you feel it is really justifiable. My approach is always to fold/map/filter and what not until I get what I need to test out my software. If I need to increase performance/modularity I can start redoing parts of my software. Mostly I get short programs and rarely have to redo stuff.
I see the functional aspect of Scala as a gift to reduce boiler plate code immensely.

独留℉清风醉 2024-11-15 23:43:10

如果您的目标是能够在列表推导中使用 Axons,那么您只需要实现 mapflatMapwithFilter< /code> 其中的方法,并(在您的情况下)将这些调用委托给 axonList。完成此操作后,您可以像这样使用 axons

for (axon <- axons)

您可以在此答案中找到更多信息:

Scala 中的理解模拟

If your goal is to be able to use Axons in list comprehensions, then you just need to implement map, flatMap, withFilter methods in it and (in your case) delegate these calls to to axonList. When you have made this, you can use axons like this:

for (axon <- axons)

More info you can find in this SO answer:

Mock for comprehension in scala

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文