Raku One-Liners 2
Total Page:16
File Type:pdf, Size:1020Kb
Andrew Shitov Raku One-Liners Getting the most of Raku’s expressive syntax for your daily routines DeepText — 2019 Raku One-Liners Getting the most of Raku’s expressive syntax for your daily routines © Andrew Shitov, author, 2019 In this book, you will find a lot of short programs, so short that they can be written in a single line of code. The seven chapters will guide you through Raku’s syntax elements that help to create short, expressive, but still useful programs. It is assumed that the reader knows the basics of the Raku programming language and understands programming in general. Published on 18 October 2019 Published by DeepText, Amsterdam www.deeptext.media ISBN 978-90-821568-9-8 Contents Chapter 1 Command-Line Options Using command-line options ....................................................... 10 -e ............................................................................................ 10 -n ........................................................................................... 10 -p ........................................................................................... 11 Examples of short one-lines ........................................................ 12 Double-space a file .................................................................. 12 Remove all blank lines ............................................................. 12 Number all lines in a file .......................................................... 12 Convert all text to uppercase ................................................... 12 Strip whitespace from the beginning and end of each line ......... 12 Print the first line of a file ....................................................... 13 Print the first 10 lines of a file ................................................. 13 Reading files with $*ARGFILES ................................................. 13 $*ARGFILES and MAIN ........................................................ 14 Chapter 2 Working with Files Renaming files ............................................................................ 16 Merging files horizontally ............................................................ 17 3 Reversing a file ........................................................................... 19 Chapter 3 Working with Numbers Grepping multiples of 3 and 5..................................................... 22 Generating random integers ........................................................ 23 Working with big numbers .......................................................... 26 Testing palindromic numbers ...................................................... 27 Adding up even Fibonacci numbers ............................................. 29 Playing with Fibonacci numbers .................................................. 30 Distance between two points ...................................................... 31 Playing with prime numbers ........................................................ 32 Using map and Seq to compute the value of π ............................. 33 Computing totals ........................................................................ 36 Sum of the numbers equal to the sum of factorials of digits ......... 37 42 via the cubes .......................................................................... 38 Chapter 4 Working with Strings Generating random passwords .................................................... 42 The joy of Unicode ..................................................................... 43 Chapter 5 Working with Dates What’s the date today? ............................................................... 46 4 How many days in the century match the condition? ................... 47 Another solution of the same problem ......................................... 49 Chapter 6 Raku Syntax More on X, .., and … .................................................................. 52 Reduction operator ..................................................................... 54 Example 1: factorial ................................................................ 54 Example 2: using a function ..................................................... 55 Example 3: matrices ................................................................ 55 All the stars of Raku ................................................................... 56 Multiplication operator............................................................ 56 Exponentiation operator .......................................................... 57 A regex repetition quantifier.................................................... 57 Min to max repetitions ............................................................ 57 Slurpy arguments .................................................................... 58 Slurpy-slurpy .......................................................................... 59 Twigil for dynamic scope ......................................................... 60 Compiler variables .................................................................. 61 All methods named as this ....................................................... 61 Whatever ................................................................................ 63 WhateverCode ........................................................................ 64 Homework ............................................................................. 68 Additional assignments ............................................................ 69 The EVAL routine ...................................................................... 69 5 Chapter 7 Raku Golf The first test .............................................................................. 74 The second test .......................................................................... 75 Tips and ideas for the Raku Golf code ......................................... 77 Omitting semicolons ............................................................... 77 Omitting topic variable ............................................................ 77 Using postfix forms ................................................................. 77 Using ranges for making loops .................................................. 77 Choosing between a range and a sequence ................................ 78 Using map instead of a loop ..................................................... 78 Omitting parentheses and quotes ............................................. 78 Using chained comparisons ...................................................... 79 Choosing between methods and functions................................ 79 Using Unicode characters ........................................................ 80 Using superscripts ................................................................... 80 Using \ to make sigilless variables............................................. 80 Using default parameters ......................................................... 80 Using && instead of if ............................................................. 81 Choosing between put and say ................................................. 81 Appendix on Compiler Internals What’s behind 0.1 + 0.2 ............................................................. 84 6 Preface Dear reader, You are reading a book about the Raku programming language. This language has appeared as a rename of Perl 6 in October 2019. Like its parent, Perl 5, the Raku language keeps the spirit of being a powerful tool in many areas, from devops programs for configuration management through different command-line applications to concurrent web servers. In this book, you will find a number of short programs that you may want to use in your daily practice. You will also find a number of one-line snippets that can enter into your bigger programs. The goal of the book is not to give a copy-and-paste list of coding examples, but to explain the various bits of Raku that help to use the language more efficiently. To run the program examples from the rest of the book, you need to down- load and install the most recent Rakudo Star compiler pack from its website, rakudo.org. If you are using the previous, Perl 6-based compiler, create an alias in your .profile file so that you can use the raku command to run the compiler: alias raku=perl6 I wish you a pleasant journey in the magic Raku language. Andrew Shitov Amsterdam, 18 October 2019 Chapter 1 Command-Line Options Using command-line options Let us talk about the command-line options that the Rakudo1 compiler of- fers to us. -e The first option to know when working with Raku is -e. It takes a string with your Perl 6 one-liner and executes it immediately. For example, print the name of the current user: $ perl6 -e'$*USER' ash -n This option repeats the code for each line of input data. This is quite handy when you want to process a file. For example, here’s a one-liner that adds up the values in a row and prints the sum: $ raku -ne'say [+] .split(" ")' data.txt If the data.txt file contains the following: 10 20 30 40 1 2 3 4 5 6 7 8 1 Rakudo (rakudo.org) is an implementation of Raku. The rest of the book assumes you are using the Rakudo compiler and run it as raku from command line. If you have an older version, which has the perl6 executable, make an alias in your .pro- file: alias raku=perl6. 10 then the result of the one-liner is: 100 10 26 There’s no difference whether you use shell’s input redirection or not; the following line also works: $ raku -ne'say [+] .split(" ")' < data.txt Make sure you place the