Home Page Icon
Home Page
Table of Contents for
Cover
Close
Cover
by Allen B. Downey, Laurent Rosenfeld
Think Perl 6
Preface
The Aim of This Book
The History of This Book
Conventions Used in This Book
Using Code Examples
O’Reilly Safari
How to Contact Us
Acknowledgments
Contributor List
I. Starting with the Basics
1. The Way of the Program
What Is a Program?
Running Perl 6
The First Program
Arithmetic Operators
Values and Types
Formal and Natural Languages
Debugging
Glossary
Exercises
2. Variables, Expressions, and Statements
Assignment Statements
Variable Names
Expressions and Statements
Script Mode
One-Liner Mode
Order of Operations
String Operations
Comments
Debugging
Glossary
Exercises
3. Functions
Function Calls
Functions and Methods
Math Functions
Composition
Adding New Functions (a.k.a. Subroutines)
Definitions and Uses
Flow of Execution
Parameters and Arguments
Variables and Parameters Are Local
Stack Diagrams
Fruitful Functions and Void Functions
Function Signatures
Immutable and Mutable Parameters
Functions and Subroutines as First-Class Citizens
Why Functions and Subroutines?
Debugging
Glossary
Exercises
4. Loops, Conditionals, and Recursion
Integer Division and Modulo
Boolean expressions
Logical Operators
Conditional Execution
Alternative Execution
Chained Conditionals
Nested Conditionals
if Conditionals as Statement Modifiers
Unless Conditional Statement
for Loops
Recursion
Stack Diagrams for Recursive Subroutines
Infinite Recursion
Keyboard Input
Program Arguments and the MAIN Subroutine
Debugging
Glossary
Exercises
5. Fruitful Subroutines
Return Values
Incremental Development
Composition
Boolean Functions
A Complete Programming Language
More Recursion
Leap of Faith
One More Example
Checking Types
Multi Subroutines
Debugging
Glossary
Exercises
6. Iteration
Assignment Versus Equality
Reassignment
Updating Variables
The while Statement
Local Variables and Variable Scoping
Control Flow Statements (last, next, etc.)
Square Roots
Algorithms
Debugging
Glossary
Exercises
7. Strings
A String Is a Sequence
Common String Operators
String Length
Searching for a Substring Within the String
Extracting a Substring from a String
A Few Other Useful String Functions or Methods
String Traversal with a while or for Loop
Looping and Counting
Regular Expressions (Regexes)
Using Regexes
Building your Regex Patterns
Literal Matching
Wildcards and Character Classes
Quantifiers
Anchors and Assertions
Alternation
Grouping and Capturing
Adverbs (a.k.a. Modifiers)
Exercises on Regexes
Putting It All Together
Extracting Dates
Extracting an IP Address
Substitutions
The subst Method
The s/search/replace/ Construct
Using Captures
Adverbs
Debugging
Glossary
Exercises
8. Case Study: Word Play
Reading from and Writing to Files
Reading Word Lists
Exercises
Search
Words Longer Than 20 Characters (Solution)
Words with No “e” (Solution)
Avoiding Other Letters (Solution)
Using Only Some Letters (Solution)
Using All Letters of a List (Solution)
Alphabetic Order (Solution)
Another Example of Reduction to a Previously Solved Problem
Debugging
Glossary
Exercises
9. Arrays and Lists
Lists and Arrays Are Sequences
Arrays Are Mutable
Adding New Elements to an Array or Removing Some
Stacks and Queues
Other Ways to Modify an Array
Traversing a List
New Looping Constructs
Map, Filter, and Reduce
Reducing a List to a Value
The Reduction Metaoperator
Mapping a List to Another List
Filtering the Elements of a List
Higher-Order Functions and Functional Programming
Fixed-Size, Typed, and Shaped Arrays
Multidimensional Arrays
Sorting Arrays or Lists
More Advanced Sorting Techniques
Debugging
Glossary
Exercises
10. Hashes
A Hash Is a Mapping
Common Operations on Hashes
Hash as a Collection of Counters
Looping and Hashes
Reverse Lookup
Testing for Existence
Hash Keys Are Unique
Hashes and Arrays
Memos
Hashes as Dispatch Tables
Global Variables
Debugging
Glossary
Exercises
11. Case Study: Data Structure Selection
The Ternary Conditional Operator
The given ... when “Switch” Statement
Subroutine Named and Optional Parameters
Named Parameters
Optional Parameters
Word Frequency Analysis
Random Numbers
Word Histogram
Most Common Words
Optional Parameters
Hash Subtraction
Constructing New Operators
Sets, Bags, and Mixes
Random Words
Markov Analysis
Data Structures
Building Your Own Data Structures
Linked Lists
Trees
Binary Heaps
Debugging
Glossary
Exercises: Huffman Coding
Variable-Length Codes
The Frequency Table
Building the Huffman Code
II. Moving Forward
12. Classes and Objects
Objects, Methods, and Object-Oriented Programming
Programmer-Defined Types
Attributes
Creating Methods
Rectangles and Object Composition
Instances as Return Values
Inheritance
The Pixel Class
The MovablePoint Class
Multiple Inheritance: Attractive, but Is It Wise?
Roles and Composition
Classes and Roles: An Example
Role Composition and Code Reuse
Roles, Classes, Objects, and Types
Method Delegation
Polymorphism
Encapsulation
Private Methods
Constructing Objects with Private Attributes
Interface and Implementation
Object-Oriented Programming: A Tale
The Fable of the Shepherd
The Moral
Debugging
The Perl 6 Debugger
Getting Some Help
Stepping Through the Code
Stopping at the Right Place with Breakpoints
Logging Information with Trace Points
Stepping Through a Regex Match
Glossary
13. Regexes and Grammars
A Brief Refresher
Declarative Programming
Captures
Named Rules (a.k.a. Subrules)
Grammars
Grammar Inheritance
Actions Objects
A Grammar for Parsing JSON
The JSON Format
Our JSON Sample
Writing the JSON Grammar Step by Step
The JSON Grammar
Adding Actions
Inheritance and Mutable Grammars
Debugging
Glossary
Exercise: A Grammar for an Arithmetic Calculator
14. Functional Programming in Perl
Higher-Order Functions
A Refresher on Functions as First-Class Objects
Anonymous Subroutines and Lambdas
Closures
List Processing and Pipeline Programming
Feed and Backward Feed Operators
The Reduction Metaoperator
The Hyperoperator
The Cross (X) and Zip (Z) Operators
List Operators: A Summary
Creating New Operators
Creating Your Own Map-Like Functions
Custom versions of map, grep, etc.
Our Own Version of a Sort Function
An Iterator Version of map
An Iterator Version of grep
The gather and take Construct
Lazy Lists and the Sequence Operator
The Sequence Operator
Infinite Lists
Using an Explicit Generator
Currying and the Whatever Operator
Creating a Curried Subroutine
Currying an Existing Subroutine with the assuming Method
Currying with the Whatever Star Parameter
Using a Functional Programming Style
The Merge Sort Algorithm
A Nonfunctional Implementation of Merge Sort
A Functional Implementation of Merge Sort
Debugging
Glossary
Exercise: Quick Sort
15. Some Final Advice
Make It Clear, Keep It Simple
Dos and Don’ts
Use Idioms
What’s Next?
A. Solutions to the Exercises
Exercises of Chapter 3: Functions and Subroutines
Exercise 3-1: Subroutine right-justify
Exercise 3-2: Subroutine do-twice
Exercise 3-3: Subroutine print-grid
Exercises of Chapter 4: Conditionals and Recursion
Exercise Suggested in “Stack Diagrams for Recursive Subroutines”: Subroutine do-n-times
Exercise 4-1: Days, Hours, Minutes, and Seconds
Exercise 4-2: Fermat’s Theorem
Exercise 4-3: Is It a Triangle?
Exercise 4-4: The Fibonacci Numbers
Exercise 4-5: The recurse Subroutine
Exercises of Chapter 5: Fruitful Functions
Exercise: Compare
Exercise: Hypotenuse
Exercise: Chained Relational Operators
Exercise 5-2: The Ackermann Function
Exercise 5-3: Palindromes
Exercise 5-4: Powers
Exercise 5-5: Finding the GCD of Two Numbers
Exercises of Chapter 6: Iteration
Exercise 6-1: Square Root
Exercise 6-2: Pi Estimate
Exercises of Chapter 7: Strings
Exercise: String Traversal
Exercise: The Ducklings
Exercise: Counting the Letters of a String
Exercise: Simulating a Regex with a Loop
Exercise: Regex Exercises
Exercise: is-reverse Subroutine
Exercise 7-1: Counting Letters
Exercise 7-2: Lowercase Letters
Exercise 7-3: Caesar’s Cipher
Exercises of Chapter 8: Word Play
Exercise 8-7: Consecutive Double Letters
Exercise 8-8: Palindromes in Odometers
Exercise 8-9: Palindromes in Ages
Exercises of Chapter 9: Arrays and Lists
Exercise: Implementing a Queue
Exercise: Other Ways to Modify an Array
Exercise: Mapping and Filtering the Elements of a List
Exercise: Advanced Sorting Techniques
Exercise 9-1: Nested Sum
Exercise 9-2: Cumulative Sum
Exercise 9-3: Middle
Exercise 9-4: Chop
Exercise 9-5: Subroutine is-sorted
Exercise 9-6: Subroutine is-anagram
Exercise 9-7: Subroutine has-duplicates
Exercise 9-8: Simulating the Birthday Paradox
Exercise 9-9: Comparing push and unshift
Exercise 9-10: Bisection Search in a List
Exercise 9-11: Reverse Pairs
Exercise 9-12: Interlocking Words
Exercises of Chapter 10: Hashes
Exercise: A Hash Is a Mapping
Exercise 10-1: Storing the Word List into a Hash
Exercise 10-2: Memoizing the Ackermann Function
Exercise 10-3: Finding Duplicates with a Hash
Exercise 10-4: Rotate Pairs
Exercise 10-5: Homophones
Exercises of Chapter 11
Exercise: The given ... when switch statement
Exercise: Constructing New Operators
Exercise: Sets, Bags, and Mixes
Exercise: Random Words
Exercise: Markov Analysis
Exercise: The Huffman Code
Exercises of Chapter 13: Regexes and Grammars
Exercise 13-1: Getting the February Dates Right
Exercise: A Grammar for an Arithmetic Calculator
Exercises of Chapter 14: Functional Programming
Exercise: Making a Functional Implementation of Quick Sort
Index
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Next
Next Chapter
Praise for Think Perl 6
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset