From bb1afd96507749a4abce52101c2f13e44d81ac87 Mon Sep 17 00:00:00 2001 From: rh01 <1048157315@qq.com> Date: Mon, 8 Jan 2018 12:30:11 +0800 Subject: [PATCH] scala post and add css --- 2018/01/08/scala-style/index.html | 38 +++++++++++++++---------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/2018/01/08/scala-style/index.html b/2018/01/08/scala-style/index.html index 63775a3..d793864 100644 --- a/2018/01/08/scala-style/index.html +++ b/2018/01/08/scala-style/index.html @@ -109,14 +109,14 @@

#2 Indentation

#3 Line Length and Whitespace

Make sure the lines are not too long, otherwise your code is very hard to read. Instead of writing very long lines, introduce some local value bindings. Using whitespace uniformly makes your code more readable.

Example (long line, missing spaces):

-
if(p(this.head))this.tail.filter0(p, accu.incl(this.head))else this.tail.filter0(p, accu)
+
if(p(this.head))this.tail.filter0(p, accu.incl(this.head))else this.tail.filter0(p, accu)

Better:

-
if (p(this.head))
+
if (p(this.head))
   this.tail.filter0(p, accu.incl(this.head))
 else
   this.tail.filter0(p, accu)

Even better (see #4 and #6 below):

-
val newAccu =
+
val newAccu =
   if (p(this.head)) accu.incl(this.head)
   else accu
 this.tail.filter0(p, newAccu)
@@ -130,7 +130,7 @@

#4 Use local Values to simplify complex Expressions

#5 Choose meaningful Names for Methods and Values

The names of methods, fields and values should be carefully chosen so that the source code is easy to understand. A method name should make it clear what the method does. No, temp is never a good name :-)

A few improvable examples:

-
val temp = sortFuntion0(list.head, tweet)   // what does sortFunction0 do?
+
val temp = sortFuntion0(list.head, tweet)   // what does sortFunction0 do?
 def temp(first: TweetSet, second : TweetSet): TweetSet = ...
 def un(th: TweetSet,acc: TweetSet): TweetSet = ...
 val c = if (p(elem)) accu.incl(elem) else accu
@@ -141,9 +141,9 @@ 

#5 Choose meaningful Names for Methods and Values

#6 Common Subexpressions

You should avoid unnecessary invocations of computation-intensive methods. For example

-
this.remove(this.findMin).ascending(t + this.findMin)
+
this.remove(this.findMin).ascending(t + this.findMin)

invokes the this.findMin method twice. If each invocation is expensive (e.g. has to traverse an entire data structure) and does not have a side-effect, you can save one by introducing a local value binding:

-
val min = this.findMin
+
val min = this.findMin
 this.remove(min).ascending(t + min)

This becomes even more important if the function is invoked recursively: in this case the method is not only invoked multiple times, but an exponential number of times.

@@ -157,12 +157,12 @@

#7 Don’t Copy-Paste Code!

  • The amount of work required to make changes to the code is multiplied
  • You should factor out common parts into separate methods instead of copying code around. Example (see also #3 above for another example):

    -
    val googleTweets: TweetSet = TweetReader.allTweets.filter(tweet =>
    +
    val googleTweets: TweetSet = TweetReader.allTweets.filter(tweet =>
       google.exists(word => tweet.text.contains(word)))
     val appleTweets: TweetSet = TweetReader.allTweets.filter(tweet =>
       apple.exists(word => tweet.text.contains(word)))

    This code is better written as follows:

    -
    def tweetsMentioning(dictionary: List[String]): TweetSet =
    +
    def tweetsMentioning(dictionary: List[String]): TweetSet =
       TweetReader.allTweets.filter(tweet =>
         dictionary.exists(word => tweet.text.contains(word)))
     
    @@ -172,7 +172,7 @@ 

    #7 Don’t Copy-Paste Code!

    #8 Scala doesn’t require Semicolons

    Semicolons in Scala are only required when writing multiple statements on the same line. Writing unnecessary semicolons should be avoided, for example:

    -
    def filter(p: Tweet => Boolean): TweetSet = filter0(p, new Empty);
    +
    def filter(p: Tweet => Boolean): TweetSet = filter0(p, new Empty);

    #9 Don’t submit Code with “print” Statements

    @@ -181,7 +181,7 @@

    #9 Don’t submit Code with “print” Statements

    #10 Avoid using Return

    In Scala, you often don’t need to use explicit returns because control structures such as if are expressions. For example, in

    -
    def factorial(n: Int): Int = {
    +
    def factorial(n: Int): Int = {
       if (n <= 0) return 1
       else return (n * factorial(n-1))
     }
    @@ -190,20 +190,20 @@

    #10 Avoid using Return

    #11 Avoid mutable local Variables

    Since this is a course on functional programming, we want you to get used to writing code in a purely functional style, without using side-effecting operations. You can often rewrite code that uses mutable local variables to code with helper functions that take accumulators. Instead of:

    -
    def fib(n: Int): Int = {
    -  var a = 0
    -  var b = 1
    -  var i = 0
    -  while (i < n) {
    +
    def fib(n: Int): Int = {
    +var a = 0
    +var b = 1
    +var i = 0
    +while (i < n) {
         val prev_a = a
         a = b
         b = prev_a + b
         i = i + 1
    -  }
    -  a
    +}
    +a
     }

    prefer:

    -
    def fib(n: Int): Int = {
    +
    def fib(n: Int): Int = {
       def fibIter(i: Int, a: Int, b: Int): Int =
         if (i == n) a else fibIter(i+1, b, a+b)
       fibIter(0, 0, 1)
    @@ -212,7 +212,7 @@ 

    #11 Avoid mutable local Variables

    #12 Eliminate redundant “If” Expressions

    Instead of

    -
    if (cond) true else false
    +
    if (cond) true else false

    you can simply write

    cond

    (Similarly for the negaitve case).