scala - How to read different lines from a text file simultaneously -
i need read file line line such reads first line, it, takes second line, , on.
i know how read text file line line:
for(line <- source.fromfile("file.txt").getlines()) { insert(line) **use first line of file in function reverse(line) **use second line of file in function }
in insert function, first want use first line of file, , in reverse function want use second line, in second iteration of loop, want use 3rd line in insert function , 4th line in reverse function , on. how that?
edit: example. want general thing, suppose if want use first line, second line, third line , iterate loop, how that?
using sliding
group lines pairs of two.
for(pairs <- source.fromfile("file.txt").getlines().sliding(2, 2)) { insert(pairs.head) reverse(pairs.last) }
obviously you'll need handle condition don't have list of length.
Comments
Post a Comment