Slide 1

Slide 1 text

Reading Files Can’t Be This Simple
 The Raku Conference
 
 2021-08-07

Slide 2

Slide 2 text

Reading Files
 Can’t Be This Simple (recorded in front of a live studio audience) 
 http://speakerdeck.com/util
 <<< >>> >>> <<<

Slide 3

Slide 3 text

This lightning talk was recorded in an actual
 lightning storm.

Slide 4

Slide 4 text

This lightning talk was recorded in an actual
 lightning storm. ( but *this* video is a re-recording )

Slide 5

Slide 5 text

/me
 
 Bruce Gray
 
 'Util'

Slide 6

Slide 6 text

Reading Files Can’t Be This Simple

Slide 7

Slide 7 text

my $path = 'myFile.txt'; for $path.IO.lines { … }

Slide 8

Slide 8 text

my $path = 'myFile.txt'; for $path.IO.lines { … }

Slide 9

Slide 9 text

my $path = 'myFile.txt'; for $path.IO.lines { … }

Slide 10

Slide 10 text

my $path = 'myFile.txt'; for $path.IO.lines { … }

Slide 11

Slide 11 text

I don't see the glaringly obvious similarity... • Iterator: Give me the next thing. • Iterable: Proceed to the adjacent thing.

Slide 12

Slide 12 text

Big file line by line

Slide 13

Slide 13 text

time raku -e ' my $mb = "abc\ndef\nghi\nEND\n" x 2**16; print $mb for ^1024; ' > gigabyte.txt

Slide 14

Slide 14 text

$ head -8 gigabyte.txt abc def ghi END abc def ghi END

Slide 15

Slide 15 text

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); } } }

Slide 16

Slide 16 text

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); } } }

Slide 17

Slide 17 text

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); } } }

Slide 18

Slide 18 text

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); } } }

Slide 19

Slide 19 text

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); } } }

Slide 20

Slide 20 text

$ 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 1 error error: compilation failed

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

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 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); } } }

Slide 23

Slide 23 text

$ 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

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

for ( String s : (Iterable)Files.lines(Paths.get(
 "gigabyte.txt"))::iterator ) {

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

someObjects.forEach( obj -> { … })

Slide 28

Slide 28 text

someObjects.forEach( obj -> { … // Cannot stop before the last obj! })

Slide 29

Slide 29 text

https://docs.raku.org/routine/lines

Slide 30

Slide 30 text

https://docs.raku.org/routine/lines lines()

Slide 31

Slide 31 text

https://docs.raku.org/routine/lines lines() $string.lines

Slide 32

Slide 32 text

https://docs.raku.org/routine/lines lines() $string.lines $filehandle.lines

Slide 33

Slide 33 text

https://docs.raku.org/type/Seq

Slide 34

Slide 34 text

.say for "gigabyte.txt".IO.lines .grep({ last if /^END/; /^a/ })

Slide 35

Slide 35 text

.say for "gigabyte.txt".IO.lines .grep({ last if /^END/ }).grep(/^a/)

Slide 36

Slide 36 text

for "gigabyte.txt".IO.lines .grep({ /^END/ or last }) { .say if /^a/; }

Slide 37

Slide 37 text

for "gigabyte.txt".IO.lines { last if /^END/; .say if /^a/; }

Slide 38

Slide 38 text

for "gigabyte.txt".IO.lines { last if /^END/; next if !/^a/; .say; }

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Much Thanks
 to You All!

Slide 41

Slide 41 text

Copyrights

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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)

Slide 44

Slide 44 text

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)