Think Perl 6
Think Perl 6
Article Index
Think Perl 6
Chapters 4 to 8
Concluding

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:

my @weekdays = <mon tue wed thu fri>;

Nevertheless: 

  • shift: removes the first item of an array and returns it
  • pop: removes the last item of an array and returns it
  • unshift: adds an item or a list of items to the beginning of an array
  • push: adds an item or a list of items to the end of an array 

are transferred 'as is', but also powered on by being able to be called with the new method syntax:

> my @numbers = <2 4 6 7>;
[2 4 6 7]

> @numbers.push(8, 9)
[2 4 6 7 8 9]

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:

use List::Pairwise qw(mapp grepp);
my $c;
my @sum = mapp {$c=$c+ $a + $b; } 1..10;

print $c;
#55

The equivalent in Perl 6 becomes:

> my $sum = reduce { $^a + $^b }, 1..10;
#55


Compare the following two line long compact explanation of 'reduce' found in the book:

"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:

String[] myArray = { "this", "is", "a", "sentence" };
String result = Arrays.stream(myArray)
                .reduce("", (a,b) -> a + b);
"

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
operator, the [...] metaoperator iteratively applies the operator to all the values of the list to produce a single value.For example, the following also prints the sum of all the elements of a list:

say [+] 1, 2, 3, 4; # -> 10"

Examples of map, filter and sort follow. 

The next chapter, Hashes, could easily go under "Everything you wanted to know about Hashes and
were afraid to ask", that is in a basic level.Common hash operations, looping over, lookups, dispatch tables etc all included.

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"
it also includes material peripheral to the subject.

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 later construct as well as the whole smartmatch family were introduced back in Perl 5 version 10 but they faced a lot of issues so they were put back to sleep and experimental state from Perl 5.18 onwards.Perl 6 re-invents them as per the 'Perl 5 done right' moto.

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.

Over the years, computer science has studied and used scores of specific data structures such as linked lists, stacks, queues, circular lists, trees of numerous kinds, etc. We will briefly study a couple of them."

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 
In the end, whom does this title address? In a sense it's polymorphic in nature.

It caters for a number of groups:

  • the absolute beginner in programming by going through CS concepts and their associated philosophy and by utilizing Perl 6 as the medium

  • the intermediate programmer in another language who's looking for the next new, cool and exciting toy

  • the visionary looking for innovative thinking and the seamless blending of the notions of OOP and FP

  • the scientist looking to perform his task as fast and as efficiently possible

  • and finally the Perl 5 hacker who wants to know what Perl 6 has in store

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.
 
Finishing the book made me ponder if indeed the time has come to embrace Perl 6 as a first taught language in schools and colleges. This title could definitely make it happen.

 

Related Reviews

Modern Perl, 4th Ed

 

Banner


97 Things Every Programmer Should Know

Author: Kevlin Henney
Publisher: O'Reilly, 2010
Pages: 258
ISBN: 978-0596809485
Print: 0596809484
Kindle: B0039OVIAK
Aimed at: Practising programmers
Rating: 5
Reviewed by: Alex Armstrong

 

The 97 Things series presents a well-chosen collection of short essays in a highly accessible way. Th [ ... ]



Creating A Data-Driven Organization

Author: Carl Anderson
Publisher: O'Reilly
Pages: 302
ISBN: 978-1491916915
Print:1491916915
Kindle: B012UDK3KG
Audience: IT managers
Rating: 4
Reviewer: Kay Ewbank

If you need to explain to someone how data can be useful, this book shows how organizations should use data to make decisions. 


More Reviews

 



Last Updated ( Tuesday, 18 July 2017 )
 
 

   
Banner
RSS feed of book reviews only
I Programmer Book Reviews
RSS feed of all content
I Programmer Book Reviews
Copyright © 2017 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.