The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Doubt in Scala Program

3 replies on 1 page. Most recent reply: Jul 23, 2019 12:32 AM by bail hard

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Pradeep Sonaimuthu

Posts: 1
Nickname: pradeepert
Registered: Mar, 2017

Doubt in Scala Program Posted: Mar 17, 2017 9:45 AM
Reply to this message Reply
Advertisement
import scala.io.Source
def widthOfLength(s: String) = s.length.toString.length
if (args.length > 0) {
val lines = Source.fromFile(args(0)).getLines().toList
val longestLine = lines.reduceLeft(
(a, b) => if (a.length > b.length) a else b
)
val maxWidth = widthOfLength(longestLine)
for (line <- lines) {
val numSpaces = maxWidth - widthOfLength(line)
val padding = " " * numSpaces
println(padding + line.length +"|"+ line)
}
}
else
Console.err.println("Please enter filename")

Can anyone please help me understand the above scala program. I am getting lot of confusions inside the program. Please explain step by step with result so that i can easily understand. Thank you very much.


jinf Fei

Posts: 3
Nickname: jm8371
Registered: Mar, 2018

Re: Doubt in Scala Program Posted: Mar 15, 2018 11:52 PM
Reply to this message Reply
import scala.io.Source

def widthOfLength(s: String) = s.length.toString.length

if (args.length > 0) {
// read all line from file to list.
val lines = Source.fromFile(args(0)).getLines().toList

// reduceLeft function
// all line at list (a1, a2, a3, a4, a5, ....)
// c = a1.length max a2.length
// c = c.length max a3.length
// c = c.length max a4.length
// c = c.length max a5.length
// ...
// return c
val longestLine = lines.reduceLeft(
(a, b) => if (a.length > b.length) a else b
)

val maxWidth = widthOfLength( longestLine )

for (line <- lines) {
// calculate we need space, for example:
// line1: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// line2: | space |xxxxxxxxxxxxxxx
//
val numSpaces = maxWidth - widthOfLength(line)
// space char
val padding = " " * numSpaces

println(padding + line.length +"|"+ line)
}

Pro Advsior

Posts: 3
Nickname: proadvisor
Registered: Jan, 2019

Re: Doubt in Scala Program Posted: Jan 2, 2019 4:47 AM
Reply to this message Reply
val lines = Source.fromFile(args(0)).getLines().toList

// reduceLeft function
// all line at list (a1, a2, a3, a4, a5, ....)
// c = a1.length max a2.length
// c = c.length max a3.length
// c = c.length max a4.length
// c = c.length max a5.length
// ...
// return c
val longestLine = lines.reduceLeft(
(a, b) => if (a.length > b.length) a else b
)

https://proaccountantadvisor.com

bail hard

Posts: 1
Nickname: bailhard
Registered: Jul, 2019

Re: Doubt in Scala Program Posted: Jul 23, 2019 12:32 AM
Reply to this message Reply
Nice to see this here and you explained in a detailed way. Thanks for sharing the useful posts here. Want to know more about 192.168.1.1 login details.

Flat View: This topic has 3 replies on 1 page
Topic: Discount on 3rd edition for Coursera students Previous Topic   Next Topic Topic: What is the name of

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use