scala - Creating a list of ints from a txt file -


i have external list in txt file, need grab first string , use key, thats fine works, , need list of numbers afterwards. first, have done wrong? current output sk1, 9 - sk2, 0 etc when need full list not first number. using scala on intelije

/**   * created andre on 10/11/2016.   */ import scala.io.source import scala.io.stdin.readint import scala.io.stdin.readline import scala.collection.immutable.listmap     object stockmarket extends app{    // application logic   // reads data text file   val mapdata = readfile("data.txt")   // print data check it's been read in correctly   println(mapdata)     // *******************************************************************************************************************   // utility functions    // reads data file - comma separated file   def readfile(filename: string): map[string, int] = {     // create buffer build map read each line     var mapbuffer: map[string, int] = map()     try {       (line <- source.fromfile(filename).getlines()) {     // each line       val splitline = line.split(",").map(_.trim).tolist     // split line @ , , convert list          // add element map buffer         // splitline line file list, e.g. list(bayern munich, 24)         // use head key         // tail list, need first (only in case) element, use head of tail , convert int         mapbuffer = mapbuffer ++ map(splitline.head -> splitline.tail.head.toint)        }     } catch {       case ex: exception => println("sorry, exception happened.")     }     mapbuffer   } } 

my external list

sk1, 9, 7, 2, 0, 7, 3, 7, 9, 1, 2, 8, 1, 9, 6, 5, 3, 2, 2, 7, 2, 8, 5, 4, 5, 1, 6, 5, 2, 4, 1 sk2, 0, 7, 6, 3, 3, 3, 1, 6, 9, 2, 9, 7, 8, 7, 3, 6, 3, 5, 5, 2, 9, 7, 3, 4, 6, 3, 4, 3, 4, 1 sk4, 2, 9, 5, 7, 0, 8, 6, 6, 7, 9, 0, 1, 3, 1, 6, 0, 0, 1, 3, 8, 5, 4, 0, 9, 7, 1, 4, 5, 2, 8 sk5, 2, 6, 8, 0, 3, 5, 5, 2, 5, 9, 4, 5, 3, 5, 7, 8, 8, 2, 5, 9, 3, 8, 6, 7, 8, 7, 4, 1, 2, 3 sk6, 2, 7, 5, 9, 1, 9, 8, 4, 1, 7, 3, 7, 0, 8, 4, 5, 9, 2, 4, 4, 8, 7, 9, 2, 2, 7, 9, 1, 6, 9 sk7, 6, 9, 5, 0, 0, 0, 0, 5, 8, 3, 8, 7, 1, 9, 6, 1, 5, 3, 4, 7, 9, 5, 5, 9, 1, 4, 4, 0, 2, 0 sk8, 2, 8, 8, 3, 1, 1, 0, 8, 5, 9, 0, 3, 1, 6, 8, 7, 9, 6, 7, 7, 0, 9, 5, 2, 5, 0, 2, 1, 8, 6 sk9, 7, 1, 8, 8, 4, 4, 2, 2, 7, 4, 0, 6, 9, 5, 5, 4, 9, 1, 8, 6, 3, 4, 8, 2, 7, 9, 7, 2, 6, 6 

here code minimal changes:

// split on 2 functions facilitate testing: def readfile(filename: string): map[string, list[int]] = {   processinput(source.fromfile(filename).getlines) }  def processinput(lines: iterator[string]): map[string, list[int]] = {   var mapbuffer: map[string, list[int]] = map()   try {     (line <- lines) {       val splitline = line.split(",").map(_.trim).tolist        // here instead of taking .tail.head, map on tail (all numbers):       mapbuffer = mapbuffer + (splitline.head -> splitline.tail.map(_.toint))     }   } catch {     case ex: exception => println("sorry, exception happened.")   }   mapbuffer } 

and here solution, believe, more idiomatic scala code:

import scala.util.try  def processinput(lines: iterator[string]): map[string, list[int]] = {   try {     lines.foldleft( map[string, list[int]]() ) { (acc, line) =>        val splitline = line.split(",").map(_.trim).tolist       acc.updated(splitline.head, splitline.tail.map(_.toint))     }   }.getorelse {     println("sorry, exception happened.")     map()   } } 

the differences are

  • not using var
  • not using mutable map (by way, don't need var mutate it)
  • using foldleft iterate , accumulate map instead of for
  • using scala.util.try instead of try-catch.

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -