Skip to content

Instantly share code, notes, and snippets.

@yashk
Created June 2, 2012 13:48
Show Gist options
  • Save yashk/2858491 to your computer and use it in GitHub Desktop.
Save yashk/2858491 to your computer and use it in GitHub Desktop.
scala for impatient exercise chapter 4 Maps and tuples
/*
Set up a map of prices for a number of gizmos that you covet. Then produce
a second map with the same keys and the prices at a 10 percent discount.
*/
val gizmos = Map("Laptop" -> 100 ,"Mobile" -> 200)
val tenPec = for((g,p) <- gizmos) yield (g,p-p*0.1)
/*
Write a program that reads words from a file. Use a mutable map to count
how often each word appears.
*/
def countWords :Unit ={
val in = new java.util.Scanner(new java.io.File("C:/text.txt")).useDelimiter("\\s")
val map = new scala.collection.mutable.HashMap[String, Int]
while(in.hasNext) {
val s = in.next()
map(s) = map.getOrElse(s,0)+1
print(s +" ")
}
println()
println(map)
}
/*Repeat the preceding exercise with an immutable map.*/
def countWords :Unit ={
val in = new java.util.Scanner(new java.io.File("C:/text.txt")).useDelimiter("\\s")
var map = Map[String,Int]()
while(in.hasNext) {
val s = in.next()
map = map + (s -> (map.getOrElse(s,0)+1))
print(s +" ")
}
println()
println(map)
}
def countWordsSorted :Unit ={
val in = new java.util.Scanner(new java.io.File("C:/text.txt")).useDelimiter("\\s")
var map = scala.collection.immutable.SortedMap[String,Int]()
while(in.hasNext) {
val s = in.next()
map = map + (s -> (map.getOrElse(s,0)+1))
print(s +" ")
}
println()
println(map)
}
/*
Print a table of all Java properties, like this:
java.runtime.name | Java(TM) SE Runtime Environment
sun.boot.library.path | /home/apps/jdk1.6.0_21/jre/lib/i386
java.vm.version | 17.0-b16
java.vm.vendor | Sun Microsystems Inc.
java.vendor.url | http://java.sun.com/
path.separator | :
java.vm.name | Java HotSpot(TM) Server VM
You need to find the length of the longest key before you can print the table.
*/
val props: scala.collection.Map[String, String] = System.getProperties()
val keyToLength = for((k,v) <- props) yield (k -> k.length)
var max:(String,Int) = ("",Int.MinValue)
for((k,v) <- keyToLength) if(v > max._2) max = (k,v)
for((k,v) <- props) println(k+(" "*(maxLength+5-k.length)+"|"+v))
@AndrewLu1992
Copy link

AndrewLu1992 commented May 10, 2017

More concise code for the last one:

def printProperties() {
        val props = System.getProperties
        var max = props.keys().map(_.asInstanceOf[String].length()).max
        for ((k, v) <- props) println(k + " "*(max - k.length()) + " | " + v)
        
    }

@asgs
Copy link

asgs commented Jul 2, 2017

Instead of using string multiplication and doing a (maxlength - keylength) to do the alignment right, String.format("%-", k) is readable. Of course, this is a non-Scala solution, but if there is a tool built for it (like how you used map and max to find the maxlength), why not?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment