Think Perl 6 |
Page 3 of 3
ISBN: 978-1491980552 Kindle:B0716P9W11 The next three chapters are on the quintessential data structures, first of all picking Arrays and Lists apart. As in Perl 5, the distinction between Lists and Arrays might not be that apparent at first. The same is true for Perl 6 as well, therefore the chapter spends the first few pages going through their differences using terminology previously defined: "Lists are ordered and immutable collections of items: you can’t change the number of items in a list and you can’t change the individual items either. Arrays, by contrast, are variables and are generally mutable: you can add elements to an array, or remove elements from it." In constructing arrays we find that Perl 5's qw is replaced by <> as in:
Nevertheless:
are transferred 'as is', but also powered on by being able to be called with the new method syntax:
The rest of the chapter goes through adding, modifying, sorting array elements and implementing Stack and Queues structures based upon arrays. Of course the chapter wouldn't be complete without a talk on mapping, reducing and filtering arrays, or in other words on the Functional Programming aspect that Perl 5 had in possession years ago before the others, (just look up in which version Java had them added), and now passes on to Perl 6.Terminology wise, while in Java arrays (collections to be precise) are treated as 'streams', in Perl 6 are treated as 'sequences'. 'Map' works the same as in Perl 5, 'filter' is just another word for 'grep', while 'reduce' is the new entry, which despite not being built into Perl 5's core it could be easily replicated with aid of the external module List::Pairswise:
The equivalent in Perl 6 becomes:
"Reduce, generates a single "combined" value from a list of values, by iteratively applying to each item of a list a function that knows how to combine two values". to a typical explanation applicable to Java and found in quality developer sites online, this one taken from Sitepoint : "The reduce method expects two arguments: an identity element, and a lambda expression. You can think of the identity element as an element which does not alter the result of the reduction operation. For example, if you are trying to find the product of all the elements in a stream of numbers, the identity element would be the number 1. The lambda expression you pass to the reduce method must be capable of handling two inputs: a partial result of the reduction operation, and the current element of the stream. If you are wondering what a partial result is, it’s the result obtained after processing all the elements of the stream that came before the current element. The following is a sample code snippet which uses the reduce method to concatenate all the elements in an array of String objects:
which of the two makes the more sense ? Of course it wouldn't be called 'Perl' if there was no TIMTOWTDI to a certain extent, as the 'reduction' operation we saw earlier could also be pulled off with the use of the reduction metaoperator : "Perl 6 also has a reduction operator, or rather a reduction metaoperator. An operator usually works on variables or values; a metaoperator acts on other operators. Given a list and an
Examples of map, filter and sort follow. The next chapter, Hashes, could easily go under "Everything you wanted to know about Hashes and Chapter 10: Case Study: Data Structure Selection is a mixed bag of material that although it "presents a case study with exercises that let you think about choosing data structures and practice using them" So among the new data structure types of Set, Bag and Mix, the chapter also examines the Ternary conditional, subroutine signatures with named and optional parameters and the 'given when' switch. The rest of the chapter is comprised of useful use cases like "Word Histogram" or "Most Common Words" aimed at readying the reader capable of choosing the right data structure for the right job. In "Building Your Own Data Structures", I will just relay the section's text because I wouldn't be able to describe it better anyway: "Perl has a number of compound types such as arrays and hashes that you can combine to construct arrays of arrays, arrays of hashes, hashes of arrays, hashes of hashes, arrays of arrays of arrays or hashes, and so on, and this is usually sufficient for most needs. Some-times, however, you need something very specific that is not built in. Further, "A linked list is a collection of items in which each item holds a value (or several values) and a link to the next item of the collection. In many programming languages, arrays have In those programming languages, a linked list is often used to represent a variable-size collection of items, the same goes with trees and binary heaps." That's the way of this book; making complex principles easy to understand in just the space of a few lines. We have reach the point where Part I of the book ends, and Part II covering more advanced topics relating to OOP commences, with Chapter 12: Classes and Objects. As usual, the Object Oriented Programming principles are always the hardest to grasp.Albeit Perl 5's OO system being quite flexible, it's also too minimal for most purposes;add the properties of weak and dynamic typing into the mix and you have yourself a mold difficult to shape since in order to adhere to the most basic of OOP principles you have to reside to more or less, hacks.For example, private methods are not inherently supported so you have to use nested closures; there's also no method signatures while polymorphism and inheritance are treated as notions too vague.What I'm trying to get across is that Perl 5 made it easy to start with OOP but otherwise did little to enforce the associated constraints. Perl 6 leaves all that behind by being more aligned to the rest of classic programming languages, but being 'Perl', a synonym for innovation, doesn't rest on its laurels but instead advances beyond the rest by introducing new ideas in the likes of Roles, which in the other conventional languages would be considered as something like an Interface type with concrete implementation.Regardless, the chapter on OOP is a strongly recommended read no matter the reader's background, since it lays the Object Orientation concepts in simple, although not simplistic, terms. Chapter 13: Regexes and Grammars could be considered the highlight of the book. Named rules, Grammar inheritance and Action objects render the construction of a Parser, once a daunting task, much easier.There's even an example in building a fully functional JSON parser, which despite serving as an advert to Perl 6's advanced regex capabilities, I think that it could be considered too much for a novice reader to get his head around. Chapter 14: Functional Programming in Perl, is a testament to Perl's vastness. Many of the concepts outlined in this chapter have their roots in Perl 5 as outlined in Higher Order Perl by Mark Jason Dominus (2005); it's just that in 6 they've been revamped as well as enriched. Finally, Chapter 15: Some Final Advice outlines a number of best practices to follow when writing programs in general. You know - Don’t repeat yourself (DRY), Don’t reinvent the wheel, Avoid hardcoded values and so on Conclusion It caters for a number of groups:
I don't know if that was the aim of the book to begin with, but it certainly can be seen that way. Nevertheless, I also think that for the most part it succeeds in properly educating its prime target group of the absolute beginner in a more direct and no-fuss way than other books of the genre do. What's best is that "Think Perl 6", is available in PDF format for free at Greenteapress so that you can start reading before deciding to buy the printed version.
Related Reviews
|
|||||||
Last Updated ( Tuesday, 18 July 2017 ) |