How to implement Scala's Above,beside and ToString

Source: Internet
Author: User
Tags arrays tostring zip

In the next step, we will implement the method above in the class element. Putting one element above another means concatenating the contents values of these two elements. So the first draft of method above might look like this:

def above (that:element): Element =
new Arrayelement (this.contents + + that.contents)

The + + operator concatenates two arrays. The array in Scala is represented as a Java array, but it supports more methods. In particular, the array in Scala inherits from the class Scala. Seq, which can form a sequence of phenomena like this and contains many methods of accessing and converting sequences. This chapter will explain some of the array methods, and a more comprehensive discussion will be in chapter 17th.

In fact, the code shown earlier is not entirely sufficient because it does not allow you to stack elements of different lengths together. However, in order to keep things simple in this section, we will allow the state and simply pass the elements of the same length to above. In verse 10.14, we will make an improvement for above so that customers can use it to combine elements of different lengths.

The next method to implement is beside. By putting two elements together, we will create a new element in which each row comes from a concatenation of the corresponding rows of two elements. As mentioned earlier, in order to keep things simple we'll start with the assumption that two elements are the same height. This produces the following design of the method beside:

def beside (that:element): Element = {
val contents = new Array[string] (this.contents.length) for
(i < 0 un til this.contents.length)
contents (i) = This.contents (i) + that.contents (i)
new arrayelement (contents)

The beside method first assigns a new array, contents, and concatenates the corresponding array elements in This.contents and that.contents. Eventually a new arrayelement was created that contained a new contents.

Although this implementation of beside can work, it is the command style that the slip is exposed in our indexed array loop. This method can be substituted for the reduction of an expression:

New Arrayelement (
for (
line1, line2) <-this.contents zip that.contents
) yield line1 + line2

Here, the this.contents and that.contents two arrays are converted to an array of pairs using the zip operator (which can be called Tupele2). The Zip method picks up the corresponding element from its two parameters and organizes it into a pair array.

For example, an expression:

Array (1, 2, 3) Zip array ("A", "B")

Will build:

Array ((1, "a"), (2, "B"))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.