Category: Scala getlines

This page is no longer maintained — Please continue to the home page at www. I am trying to convert to scala from java. I found that the transition is much harder than I thought.

Ecoflam high efficiency

I am now stocking at the error message:. The compiler infers that line is of type String since you are operating on an object that inherits from Iterator[String].

As Adam Tistler noted, this specific error message is because foreach requires a function as an argument.

A function type is written paramtype[, Your other examples which should otherwise work all have the same problem. That's swell but it doesn't address why the OP got an error message. It should be pointed out that these will get the same error message if println is replaced with println "test" as in the original, because the former is convertable to a function while the latter isn't.

Android fragment example source code

What I really want to do is to read from a big file and get the top 10 lines with some condition e. Do I have to break the code into two functions and passing the data back and forth?

scala getlines

You really should use the simple, straightforward approach that everyone else has described instead of mucking around with breakable. I think what you're missing is that the filter function on an Iterator is non-strict, meaning that the resulting filtered Iterator won't ask for any data from the underlying Iterator until the filtered Iterator needs it to satisfy a request.

For that matter, the take function on an Iterator is also non-strict. Anyway, the generator causes the take10 Iterator to ask for lines from the filtered Iterator, which in turn asks for lines from the lines Iterator, which in turn asks for characters from the BufferedSource.

The take10 Iterator will stop the reading once it's found the top 10 lines if indeed there are 10 to be found. And now a surprise. If we go back and use take10 a second time, nothing happens. If you try to create another "filtered. The resulting List can be reused as often as you want. Now, suppose you want to have the results stored for later use, but you don't want the lines read in if nobody ever needs them.

For that you'd use something like this:. A Stream won't read the data until it's needed, but once the data's been read, the Stream stores it for repeated use.

One of the challenges of converting to Scala from non-functional languages is getting comfortable with the different evaluation strategies that are available: strict evaluate immediatelynon- strict re-evaluate every time it's neededand lazy evaluate when needed, then save the result for later re-use.

Consider these:. The value of strictTime is the initialization time, and is constant.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. My understanding of the "Programming in Scala" book is that the following should return an Array[String] when instead it returns an Iterator[String].

What am I missing? Nope, it returns an Iterator.

The village of mincioni, municipality of torrice (fr) lazio

If you want to convert an Iterator to an Array as mentioned in your commentthen try the following after you've yielded your Iterator :.

The code in your example calls calls Source. Then you call getLines which returns an iterator.


That iterator is then yielded and stored as data. Learn more. Scala getLines with yield not as I expect Ask Question.

Asked 8 years, 7 months ago. Active 8 years, 7 months ago. Viewed 2k times. Thanks in advance. Jay Daley Jay Daley 2 2 silver badges 10 10 bronze badges. Active Oldest Votes. BufferedSource But the following should work if an Array is your goal: Source. The code I omitted extracts bits from each line so that the final Array is only made up of those bits.

In the solution you provide, the entire file is made into an Arraywhich I would then need to cut down, which is what I wanted to avoid. Does that help?One obvious approach was to count the newline characters in the file:. As the comment shows, this took seconds to read a file that has 10M lines.

AutoCAD - Import Raster Image & Scale for Tracing (AUTOCAD 2012)

An Apache access log file for this website. I ran the Unix wc -l command on the same file, and it ran in about 22 seconds, so I thought I better see if I could do better. Another obvious approach is to use the getLines method from the Source object my method receives a BufferedSource. Would it run much faster? Oh yeah. It took just 22 seconds:. Taking advantage of the BufferedSource by calling getLines makes a huge difference in performance.

type mismatch error

Quick update: Here's a link to the current Scala BufferedSource code. One cool thing about this result: It's just as fast as the Unix word count program wc -l.

I originally thought this algorithm was a little faster than the wc program, but then I remembered that I start the timer inside the Scala code -- after the initial Java startup time -- while I timed the wc command with the time command.

In case you think the performance difference is the result of looking at each character in the file, this third approach combines the two techniques. I use getLines to get each line, and the loop over each character in each line.

It only takes 23 seconds:. I share the rewritten loop in my upcoming book, the Scala Cookbook. Scala file reading performance: Line counting algorithms. By Alvin Alexander. Last updated: July 28, Using getLines Another obvious approach is to use the getLines method from the Source object my method receives a BufferedSource.

Index of justice league war

Counting all chars In case you think the performance difference is the result of looking at each character in the file, this third approach combines the two techniques. Five good ways and two bad ways to read large text files with Scala. Scala code to read a text file to an Array or Seq. Scala: Read a text file that has comment tags. How to open and read text files in Scala. Scala: How to read input from one file while writing output to another file.

If you want to understand all the Buddhas The most difficult yoga asana is Anchorage, Alaska: Cruise ships canceled. A dementia story. Cousins the movie.Keeping you updated with latest technology trends, Join DataFlair on Telegram. In this tutorial Scala File io, we will learn how to Open, read and write files in Scala. Or you could import java. File and java. To create a new file to write to, we create a new instance of PrintWriter, and pass a new File object to it.

Garmin g5 experimental installation manual

At this point, nothing is really visible in the file. To see these changes reflect in the file demo1. Learn: Scala Variables with Examples. Now Scala does provide a class to read files. This is the class Source. We use its companion object to read files. To read the contents of this file, we call the fromFile method of class Source- with the filename as argument. To read individual lines instead of the whole file at once, we can use the getLines method.

When we talked iterators, we saw the use of the method take n to return the first n values from the iterator.

scala getlines

So this is how you read and write a Scala file io. In this article, we saw use of methods writeclosefromFilegetLinestakeand slice. Furthermore, if you have any query, feel free to ask in the comment section.Hi Jesse Thanks a lot for this valuable sample!

I pushed the sample further and tried to go for binary copies in scala. So after a while I got the following snippet to do the job in scala: [code] import java.

File Handling in Scala

Thanks a lot Patrick. I have a couple points for you. The reason you get a zero length copy is because you are comparing 0 and Unit which is always false.

As for a way to do this that is more "functional" would be to use recursion. It is totally unnecessary but if you want to learn to program in a functional manner then you would probably do: import java. It should be turned into a loop thanks to tail recursion so you dont get a stack overflow.

I think I may have to ask the scala list. Ah I figured out the bug with some help. The copy method could be overridden by a subclass so it can't be optimized. It has to be final or it has to be an inner function: def copyFile. Although I would still like to do a speed test to see if the multiple allocations of the array is expensive. I have been told that the JVM should reuse the same object so the difference should be minimal.

I will demonstrate this functionality by copying data from a URL to a file and then making a copy of that file. Important Note: Scala 2. It is quite nice to use. That is just the most basic of what you can expect. I will do a couple topics on that when it gets closer to being finalized.

Labels: beginnercopy filefileScalaSourceurl. Unknown August 13, at AM. Unknown August 14, at AM. Henry Buckley October 19, at AM. Newer Post Older Post Home. Subscribe to: Post Comments Atom.This is an excerpt from the Scala Cookbook partially modified for the internet. This is Recipe However, be aware that this code may be slow on large files.

For instance, the following method that counts the number of lines in a file takes seconds to run on my current computer on an Apache access logfile that is ten million lines long:.

The time can be significantly reduced by using the getLines method to retrieve one line at a time, and then working through the characters in each line. The following line-counting algorithm counts the same ten million lines in just 23 seconds on the same computer:.

Both algorithms work through each byte in the file, but by using getLines in the second algorithm, the run time is reduced dramatically. Notice that there are the equivalent of two for loops in the second example. How to process every character in a text file in Scala. By Alvin Alexander. Last updated: November 29, How to write text files in Scala. How to process a CSV file in Scala.

How to list subdirectories beneath a directory in Scala. How to execute external system commands in Scala. If you want to understand all the Buddhas The most difficult yoga asana is Anchorage, Alaska: Cruise ships canceled. A dementia story. Cousins the movie.This is an excerpt from the Scala Cookbook partially modified for the internet.

This is Recipe You want to open a plain-text file in Scala and process the lines in that file. In Scala shell scripts, where the JVM is started and stopped in a relatively short period of time, it may not matter that the file is closed, so you can use the Scala scala. As a variation of this, use the following approach to get all of the lines from the file as a List or Array :.

The getLines method of the Source class returns a scala. The iterator returns each line without any newline characters. An iterator has many methods for working with a collection, and for the purposes of working with a file, it works well with the for loop, as shown. So while this approach has this flaw, it can be used in short-lived JVM processes, such as a shell script. You can demonstrate the same result using a Scala shell script. Just add a Thread. To demonstrate this library, create an SBT project, and then add the following line to its build.

scala getlines

Next, create a file named TestARM. This code prints all of the lines from the file named example. The managed method from the ARM library makes sure that the resource is closed automatically when the resource goes out of scope.

The ARM website shows several other ways the library can be used. A second way to demonstrate the Loan Pattern is with the using method described on the Loan Pattern website. Both the ARM library and the using method end up with the same result, implementing the Loan Pattern to make sure your resource is closed automatically. The following code demonstrates how the fromFile method can be used with using to create a method that returns the entire contents of a file as a List[String]wrapped in an Option :.

It can be used in the following ways:. If the process of opening and reading a file fails, you may prefer to return a Try or an empty List[String]. See Recipes The benefit of using Try is that you can get the cause of the exception back when you call this method and an exception occurs.

In the Success case you handle the condition where the file was opened properly, and in the Failure case you deal with the exception however you want to.

scala getlines

In Scala 2. For instance, you can specify an expected character encoding for a file like this:. See the Scaladoc for the scala. Source object not the Source class, which is an abstract class for more information.

How to open and read text files in Scala. By Alvin Alexander. Last updated: December 8, Solution There are two primary ways to open and read a text file: Use a concise, one-line syntax. This has the side effect of leaving the file open, but can be useful in short-lived programs, like shell scripts.

Use a slightly longer approach that properly closes the file. This solution shows both approaches. A simple Scala Try, Success, Failure example reading a file.