CIS-3030 Homework #5: Functional Programming

Due: Wednesday, March 20, 2019

In PiS read Chapter 8.

The purpose of this assignment is to give you practice using functional approaches for manipulating information. Specifically you will practice using the higher order methods filter, map, flatMap, foreach, and foldLeft. In all of the functions I ask you to write below you should not use any vars or while loops. Make use of Scala's syntactic abbreviations where appropriate (they may not always be appropriate).

Note: In this assignment you will use the library version of the higher order methods I mention above and not the versions you wrote in the last assignment. The difference is that the library versions are methods of the various container classes.

Note also: If you are doing this assignment right most (all?) of the methods can be defined easily in one line.

Implement the functions described below. I invite you to create test cases for them.

    object Hw05 {
  
      // The following method takes a list of strings and trims leading and trailing white space
      // from each string in the list. Given the input:
      //
      // List("  Hello  ",
      //      "Hello    ",
      //      "    Hello")
      //
      // the method should produce List("Hello", "Hello", "Hello")
      //
      def trimAll(text: List[String]): List[String] = ???
  
      // The following method takes a list of strings and eliminates strings that are entirely
      // empty. Given List("Hello", "", "World") the method should produce
      // List("Hello", "World")
      //
      def removeEmpty(text: List[String]): List[String] = ???
  
      // The following method removes all blanks from a string. Given "Hello World" the method
      // should produce "HelloWorld". HINT: In Scala Strings have higher order methods too.
      //
      def removeBlanks(line: String): String = ???
  
      // The following method joins all the strings in a given list of strings into a single,
      // long string. Given List("Hello", "World") the method should produce "HelloWorld". Use
      // foldLeft.
      //
      def joinLines(text: List[String]): String = ???
  
      // The following method removes all blanks from the given text and returns a single long
      // string containing the non-blank characters. Given:
      //
      // List("  He llo  ",
      //      "He llo    ",
      //      "    He llo")
      //
      // the method should produce "HelloHelloHello". Do this *without* directly calling the
      // methods you already defined above. Can you do this in one line of code?
      //
      def removeAllBlanks(text: List[String]): String = ???
  
      // The following method applies the given function twice to the given argument. For
      // example, if given Math.sqrt and 2.0, it computes Math.sqrt(Math.sqrt(2.0)).
      //
      def applyTwice[A](f: A => A, argument: A) = ???
  
      // The following method takes two functions and returns a function that applies one
      // followed by the other. For example if given functions f(x) and g(x) it returns a
      // function that does f(g(x)). To see the method in action consider these functions:
      //
      //   def stringLength(s: String): Int = s.length
      //   def factor(n: Int): List[Int] = // computes prime factors of n.
      //
      // Now you can use the method below to build a new function:
      //
      //   val myWackyFunction = compose(factor, stringLength)
      //
      // What does myWackyFunction do?
      //
      def compose[A, B, C](f: B => A, g: C => B): C => A = ???
  
      // The following method "de-options" a list of Option[Int]. It removes all elements that
      // are None and extracts the contents of the Some elements into a separate list. Given:
      //
      // List(Some(5), None, None, Some(-10)) the method produces List(5, -10)
      //
      def deOption(values: List[Option[Int]]): List[Int] = ???
  
      def main(args: Array[String]): Unit = {
        println("Put your tests here (not required, but highly recommended).")
      }
  
    }
    

Submit your file Hw05.scala only (put your name in comments).


Last Revised: 2019-03-13
© Copyright 2019 by Peter C. Chapin <pchapin@vtc.edu>