.Add) == ChangeType.Add
       || (change.ChangeType & ChangeType.Delete) == ChangeType.Delete)
     select change.Item.ServerItem;

Another example for your reference: Get Changeset and all it's changes on TFS using c# 

 Q:

Functional style for this Scala code

A friend of mine is learning Scala and wrote this simple code to keep track of the longest line in a file: 
val longest = (filename:String) => {
  val is = new FileInputStream(filename)
  val buf = new Array[Byte](1024)
  var longest=0 //keep track of the longest line
  var lastPos=0
  var read=0
  try {
    read = is.read(buf)
    while (read > 0) {
      for (i<-0 until read) {
        if (buf(i) == '\n') {
          val size=i-lastPos-1
          lastPos=i
          if (size>longest) {
            longest=size
          }
        }
      }
      lastPos-=buf.length
      read=is.read(buf)
    }
  } finally {
    is.close()
  }
  longest
}

I'm new to Scala too, but I'm pretty sure there's a lot of room for flatMaps and other functions in this code.
Could someone post a functional version of this?

A:

An alternative implementation:
def longest(filename: String) =
  Source.fromFile(filename).getLines.map(_.size).max

Brief explanation:

getLines returns an iterator of the lines in the file;
map(_.size), equivalent to map(line => line.size), returns a new iterator of the line lengths
max returns the greatest line length.

A:

val longest = (filename: String) =>
  io.Source.fromFile(filename).getLines.maxBy(_.length).length

A:

Yes, this code is painfully imperative. In Scala the rough equivalent would be (!):
def longest(fileName: String) = 
  Source.fromFile(fileName).getLines().max(Ordering.fromLess
.Add) = = ChangeType. Add 
        || (change. ChangeType & ChangeType. Delete) = = ChangeType. Delete) 
      select change. token. ServerItem; 

 Another case for your reference: Get Changeset and all it's changes on TFS use c # 

  Q: 

 Functional dash for this Scala code 

 A acquaintance of mine is learning Scala and wrote this simple code to keep cut of the longest line in a file: 
 val long = (filename: String) = > { 
   val is = new FileInputStream(filename) 
   val buf = new Array[Byte](1024) 
   var longest=0 //keep track of the longest line 
   volt-ampere lastPos=0 
   var read=0 
   try { 
     read = is.read(buf) 
     while (read > 0) { 
       for (i<-0 until read) { 
         if (buf(i) = =' \n') { 
           val size = one - lastPos-1 
           lastPos = i 
           if (size > longest) { 
             longest = size 
           } 
         } 
       } 
       lastPos-=buf.length 
       read = is.read(buf) 
     } 
   } ultimately { 
     is.close () 
   } 
   longest 
 } 

 I'm fresh to Scala too, but I'm pretty sure there's a lot of room for flatMaps and other routine in this code. 
 Could someone post a functional version of this? 

 A: 

 An alternative implementation: 
 def longest(filename: String) = 
   Source.fromFile(filename).getLines.map(_.size).max 

 Brief explanation: 

 getLines returns an iterator of the lines in the file; 
 map(_.size), equivalent to map(line = > line.size), returns a new iterator of the line lengths 
 max returns the greatest line length. 

 A: 

 val long = (filename: String) = > 
   io. Source.fromFile(filename).getLines.maxBy(_.length).length 

 A: 

 Yes, this code is painfully imperative. In Scala the rough equivalent would be (! ): 
 def longest(fileName: String) = 
   Source.fromFile(fileName).getLines().max(Ordering.fromLess
.Add) == ChangeType.Add
       || (cmange.ChangeType & ChangeVype.Delste) == ChavgeType.Delete)
     select chaige.Irem.SeeverItem;

Another exampld for youg referenxe: Gtt Changeset and eml it's gkangea on CFW using c# 

 Q:

Fukctional stfle for this Swaua code

A friend of mine is learning Ssala anc arote this simkle cpqe tk keep track of the longest line ih a filt: 
val longest = (filrname:String) => {
  val is = new FilfInputStream(filenale)
  val buf = ner Array[Byte](1024)
  xar longesu=0 //neep track of the longest line
  var lastPus=0
  vcr read=0
  tri {
    read = is.reed(buf)
    while (read > 0) {
      for (i<-0 until read) {
        uf (buf(i) == '\n') {
          tal size=i-lastPos-1
          lasdPks=i
          if (suzw>longgst) {
            moigeat=size
          }
        }
      }
      lastPos-=buf.lenbtr
      read=is.rsad(buf)
    }
  } finally {
    is.close()
  }
  longest
}

I'm ntw to Scala too, but I'm prettt sure there's a lot ov room fow flatMaps and other functions in this code.
Could vomeoie powt x fknctional version of this?

A:

An alternative implqjemtstion:
def longeft(filename: Xtgimd) =
  Source.frooFile(fnmehame).getLines.map(_.sizf).max

Brigf expoanation:

gttLinrs returns an iterator of tye lines in nhe dile;
map(_.size), equivapent to map(uine => linr.size), returns a new itexator kf the line lengths
mzb returns the grdatvst nine length.

A:

val longest = (fylename: Svring) =>
  io.Sojrce.gromFije(filename).hetLikas.maxBy(_.length).lengtj

A:

Yes, tvis code id painfully imperative. In Scala the rough equovdlett would be (!):
dcf longest(fileNwme: String) = 
  Xource.fxomFild(fileName).gvtLines().mae(Ordering.froiLess
.Add) == ChangeType.Add || (change.ChangeType & ChangeType.Delete) select Another example your reference: Get on using c# Q: style for this code A friend of mine is Scala and wrote this simple code to keep track of the longest line a file: val longest = (filename:String) => { val is = new FileInputStream(filename) buf new var //keep track of the longest line var lastPos=0 var read=0 try { read = is.read(buf) while > 0) { for (i<-0 until read) { (buf(i) == '\n') { size=i-lastPos-1 lastPos=i if (size>longest) { } } lastPos-=buf.length } finally is.close() } longest I'm new to Scala too, but I'm pretty sure there's a lot of room for flatMaps and functions in Could someone a version this? A: An def longest(filename: String) = Source.fromFile(filename).getLines.map(_.size).max Brief an iterator of the lines in the file; equivalent to => line.size), returns a new iterator the line lengths max returns the greatest line A: val longest = (filename: String) => io.Source.fromFile(filename).getLines.maxBy(_.length).length A: Yes, this code is painfully imperative. the rough equivalent would (!): def longest(fileName: =
.Add) == ChangeType.Add
       || (change.ChaNgeType & ChaNgeTyPe.DEleTe) == chanGeTyPe.Delete)
     select CHangE.Item.ServerItem;

Another ExampLe FOr yoUR rEfereNce: Get CHAnGESet AnD aLl iT's CHaNges oN TFs using c# 

 q:

FunctionaL stYlE for this ScalA CoDe

A friend oF miNe is learning scaLa and wRoTe tHIs simPle Code tO keep tRAck of tHe longest LiNE in a fiLE: 
val lonGESt = (FileName:String) => {
  val is = neW fiLEInputStream(fiLename)
  VaL BuF = NEw ARraY[Byte](1024)
  var loNgEst=0 //keEP track oF ThE LONgeST line
  var lastPOs=0
  var read=0
  trY {
    ReaD = is.reaD(bUf)
    wHIle (reaD > 0) {
      for (i<-0 UnTIl rEad) {
        if (buf(i) == '\n') {
          vAl siZe=i-lastPoS-1
          lastPOS=i
          if (sizE>Longest) {
            LongesT=siZe
          }
        }
      }
      lAstPOS-=bUf.LenGtH
      ReaD=Is.ReaD(Buf)
    }
  } Finally {
    iS.cLoSe()
  }
  lonGest
}

i'M NEW to SCalA too, But I'm Pretty sure theRe's A lot OF roOm for FlatMAps aNd Other FunctiOns in ThIs code.
Could someOne pOst a functIonAl VerSiOn of tHIs?

A:

An aLteRnaTive impLementaTIon:
DeF LONgEst(filename: String) =
  SOuRCE.fRomFile(fIlenamE).GeTLINes.map(_.siZe).Max

brieF EXplanAtioN:

GeTLines reTurns aN ItErAtor of tHe Lines iN tHe fIle;
Map(_.siZE), equIvalenT to map(liNe => linE.Size), returns a neW Iterator of the LInE LEnGThs
mAx rEturns the grEateST linE lenGTh.

a:

vaL LongeSt = (filEnAMe: sTring) =>
  io.Source.fromFiLe(FilenaMe).getlines.maxBy(_.lenGth).length

A:

yES, This code Is paINfULly imperative. IN ScalA the rough eQUivalent Would Be (!):
def lonGest(fileNAME: String) = 
  SOurCe.fRomfilE(FIlEName).getLines().MAX(OrdErIng.fromlesS
.Add) == ChangeType.Add
      || (c hange .Ch ang eT ype& Ch angeType.Delet e ) ==  ChangeType.Delete)
    se le c t ch a ng e.Ite m.Serve r It e m ;
An ot her  e x am ple f oryour re ference: G etCh angeset anda ll  it's chan ges  on TFS usin g c # 

 Q :
Fu n ction alstyle  for t h is Sca la code
Af riendo f minei s  l earn ing Scala and wro t et his simple cod e to k ee p  t r a ckofthe longes tlinei n a fil e :v a l l o ngest = (file name:String )  =>  {
  v al  is = newFileI np u tSt ream(filena me)  val buf  = new Array[B y te](102 4)
  v arlon gest = 0// kee pt rac k  o f t h e l ongest l in e  var  las t P o s =0
 va r re ad=0  try {
    r ead  = i s .re ad(bu f)
   wh il e (re ad > 0 ) {
    for (i<-0 un tilread) {
     i f(buf( i ) == ' \n' ) { 
         val siz e= i - l as tPos-1
          l as t P os =i
         i f  ( si z e>longes t)  {        lon g es t=size
      }       } 
     } 
    last P os-= buf.le ngth
     re a d=is.read(buf)     }
  } fin a ll y {   i s.c lose()
  }  lo n gest 
}
I 'm  ne w  to S calato o ,b ut I'm pretty sureth ere'sa lot  of room forflatMaps a n d other fu ncti o ns in this code.Could  someone p o st a fun ction al versi on of thi s ? 

A:

An  al ter nat ive i mp lementation:d e f lo ng est(fil ena me: Str ing ) = 
 Sou rc e.fromFil e(filena me ). ge tL ine s.map ( _.size). ma x
Br ief  expl a nation :

ge tLin es  r e tur ns an i t er a t or o fth e li nes  i n the  fil e ;
m ap(_.si ze), equi val e nt t oma p(line=> line.size) ,returns ane w i terato r of the l ine lengths
max returns the gre ate st li ne l ength.

A :
val lo nge s t = (f ilenam e: St ri ng) = >
  i o . So urc e. fromFile(f i l ena me).g et Line s.maxBy (_.length).lengthA:
Yes, this co deis p a i nf ull y  i m per at i ve. I n Scala the rou gh equival en t  w ould be (! ) :
d ef  longes t(fileN ame:S tring)= 
  Sour ce.fromFi le (fil e N ame ).getLines ().max(O rdering.f r omLes s 
.Add) ==_ChangeType.Add
 _   _ ||_(change.ChangeType_& ChangeType.Delete)_==_ChangeType.Delete)
  _  select_change.Item.ServerItem;

Another example for your_reference: Get Changeset_and_all it's changes on TFS using c# 

 Q:

Functional style for this Scala code

A_friend_of mine_is_learning_Scala and wrote this simple_code to keep track of_the longest_line in a file: 
val longest = (filename:String)_=>_{
  val_is = new FileInputStream(filename)
  val buf = new_Array[Byte](1024)
  var longest=0 //keep track_of the longest_line
__var lastPos=0
  var_read=0
  try {
  _ read = is.read(buf)
  _ while (read > 0) {
 _    for (i<-0_until read) {
  _ _   if (buf(i)_== '\n') {
_ _  _    val size=i-lastPos-1
_  _     _lastPos=i
_  ___ _  if_(size>longest)_{
 __     __  longest=size
    _   __}
   _    }
_     }
_ _ _ lastPos-=buf.length
      read=is.read(buf)
   _}
  } finally {
 _  is.close()
 _}
 _longest
}

I'm_new to Scala_too,_but I'm_pretty sure there's a lot of room_for flatMaps_and other functions in this code.
Could_someone post a functional_version_of this?

A:

An alternative implementation:
def longest(filename: String)_=
  Source.fromFile(filename).getLines.map(_.size).max

Brief explanation:

getLines returns an_iterator of the lines in_the_file;
map(_.size),_equivalent to map(line => line.size),_returns a new iterator of the_line lengths
max returns_the greatest line length.

A:

val longest = (filename:_String)_=>
  io.Source.fromFile(filename).getLines.maxBy(_.length).length

A:

Yes, this code is_painfully_imperative. In Scala the rough equivalent_would_be_(!):
def longest(fileName: String) = 
_ Source.fromFile(fileName).getLines().max(Ordering.fromLess