Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reading files can't be this simple

Reading files can't be this simple

Reading a file, line-by-line.

How is such a common task, so simple here in Rakuland?

Why is such a common task, so commonly complicated elsewhere?

Who knows?

You will!

Talk tags
raku, lazy, iterator, iterable, for, while, lines, slurp, X::Control, java, dart, perl

Avatar for Bruce Gray

Bruce Gray

August 07, 2021
Tweet

More Decks by Bruce Gray

Other Decks in Programming

Transcript

  1. Reading Files
 Can’t Be This Simple (recorded in front of

    a live studio audience) 
 http://speakerdeck.com/util
 <[email protected]> <<< >>> >>> <<<
  2. I don't see the glaringly obvious similarity... • Iterator: Give

    me the next thing. • Iterable: Proceed to the adjacent thing.
  3. time raku -e ' my $mb = "abc\ndef\nghi\nEND\n" x 2**16;

    print $mb for ^1024; ' > gigabyte.txt
  4. import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class ReadAL {

    public static void main(String[] args) throws Exception { for ( String s : Files.lines(Paths.get("gigabyte.txt")) ) { if ( s.startsWith("END") ) break; if ( !s.startsWith("a") ) continue; System.out.println(s); } } }
  5. import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class ReadAL {

    public static void main(String[] args) throws Exception { for ( String s : Files.lines(Paths.get("gigabyte.txt")) ) { if ( s.startsWith("END") ) break; if ( !s.startsWith("a") ) continue; System.out.println(s); } } }
  6. import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class ReadAL {

    public static void main(String[] args) throws Exception { for ( String s : Files.lines(Paths.get("gigabyte.txt")) ) { if ( s.startsWith("END") ) break; if ( !s.startsWith("a") ) continue; System.out.println(s); } } }
  7. import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class ReadAL {

    public static void main(String[] args) throws Exception { for ( String s : Files.lines(Paths.get("gigabyte.txt")) ) { if ( s.startsWith("END") ) break; if ( !s.startsWith("a") ) continue; System.out.println(s); } } }
  8. import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class ReadAL {

    public static void main(String[] args) throws Exception { for ( String s : Files.lines(Paths.get("gigabyte.txt")) ) { if ( s.startsWith("END") ) break; if ( !s.startsWith("a") ) continue; System.out.println(s); } } }
  9. $ java ReadAL.java ReadAL.java:6: error: 
 for-each not applicable to

    expression type for ( String s : Files.lines(Paths.get("gigabyte.txt")) ) { ^ required: array or java.lang.Iterable found: Stream<String> 1 error error: compilation failed
  10. import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; import java.util.stream.Collectors; public class

    ReadAL { public static void main(String[] args) throws Exception { Stream<String> fStream = Files.lines(Paths.get("./gigabyte.txt")); for ( String s : fStream.collect( Collectors.toList() ) ) { if ( s.startsWith("END") ) break; if ( !s.startsWith("a") ) continue; System.out.println(s); } } }
  11. $ time java ReadAL.java Exception in thread "main" java.lang.OutOfMemoryError: Java

    heap space … at ReadAL.main(ReadAL.java:8) real 0m32.560s user 1m38.740s sys 0m2.037s
  12. Copyright Information: Images and Video • Camelia • © 2009

    by Larry Wall
 http://github.com/perl6/mu/raw/master/misc/camelia.txt • Barnyard Dawg and Foghorn Leghorn • © 1946, 1949, 1956 by Warner Bros. • Merrie Melodies: Walky Talky Hawky
 Looney Tunes: Henhouse Henery
 Merrie Melodies: The High and the Flighty
  13. Copyright Information: This Talk This work is licensed under a

    Creative Commons Attribution 4.0 International License. CC BY https://creativecommons.org/licenses/by/4.0/ (email me for the original Apple Keynote .key file)
  14. History • v 0.98 2021-07-30
 Test recording, incomplete. Lightning storm.

    • v 1.00 2021-08-07
 Presented final version to The Raku Conference
 (Previously recorded on 2021-08-06)