Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Mastering Functional Programming
Mastering Functional Programming

Mastering Functional Programming: Functional techniques for sequential and parallel programming with Scala

eBook
€28.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Mastering Functional Programming

The Declarative Programming Style

Declarative programming is tightly connected to functional programming. Modern functional languages prefer to express programs as algebra and not as algorithms. This means that programs in functional languages are combinations of certain primitives with operators. The technique where you express your programs by specifying what to do, but not how to do it, is referred to as declarative programming. We will explore why declarative programming appeared and where it can be used.

In this chapter, we will cover the following topics:

  • Principles of declarative programming
  • Declarative versus imperative collections
  • Declarative programming in other languages

Technical requirements

To run the examples in this book, you will need the following software, as well as basic understanding of how to use it:

To run the samples:

  1. Clone the https://github.com/PacktPublishing/Mastering-Functional-Programming repository on your machine.
  2. From its root, compose and run the Docker set of images specified in docker-compose.yml. If you are on a Linux/Mac machine, you can run ./compose.sh to complete this step. If you are on Windows, open compose.sh in text editor and run each command from your terminal manually.
  3. Run shell (Bash) on the Docker service called mastering-functional-programming_backend_1. You can complete this step by running ./start.sh on a Linux/Mac machine from a separate terminal window. If you are on a Windows machine, run docker exec -ti mastering_backend bash. Then cd Chapter1 for Chapter 1 examples, or cd ChapterN for Chapter N examples.
  4. The cpp folder contains C++ sources. You can run them with ./run.sh <name-of-the-source> from that directory.
  5. The jvm folder contains Java and Scala sources. You can run them by running sbt run from that directory.

Note that it is necessary to run the examples under Docker. Some chapters run examples against a live database, which is managed by Docker, so make sure to get the above procedure working.

The codes presented in this book are available at: https://github.com/PacktPublishing/Mastering-Functional-Programming

Principles of declarative programming

Why declarative programming? How did it appear? To understand declarative programming, we need to first understand how it is different from imperative programming. For a long time, imperative programming has been a de facto industry standard. What motivated people to start switching to the functional style from the imperative style?

In imperative programming, you rely on a set of primitives that your language provides. You combine them in a certain way so as to achieve a functionality that you need. We can understand different things under primitives. For example, these can be loop control structures, or, in the case of collections, operations specific to collections, such as creating a collection and adding or removing elements from a collection.

In declarative programming, you also rely on primitives. You use them to express your program. Yet, in declarative programming, these primitives are much closer to your domain. They can be so close to your domain that the language itself can be regarded as a domain-specific language (DSL). With declarative programming, you are able to create primitives as you go.

In imperative programming, you usually don't create new primitives, but rely on the ones the language provides you with. Let's go through some examples to understand the importance of declarative programming.

Example – go-to versus loops

How imperative turns into declarative is best understood by means of an example. Most likely, you already know the go-to statement. You have heard that using the go-to statement is bad practice. Why? Consider an example of a loop. It is possible to express a loop using only the go-to statement:

#include <iostream>
using namespace std;
int main() {
int x = 0;
loop_start:
x++;
cout << x << "\n";
if (x < 10) goto loop_start;
return 0;
}

From the preceding example, imagine you need to express a while loop. You have the variable x and you need to increment it in a loop by one until it reaches 10. In modern languages such as Java, you would be able to do this using the while loop, but it is also possible to do that using the go-to statement. For example, it is possible to have a label on the increment statement. A conditional statement after it will check whether the variable reached the necessary value. If it did not, we perform a go-to on the line of code to increment a variable.

Why is go-to a bad style in this case? A loop is a pattern. A pattern is an arrangement of two or more logical elements in your code that repeats in different places of your program. In our case, the pattern is the loop. Why is it a pattern? First, it consists of three parts:

  1. The first part is the label that is the entry point to the body of the loop—the point where you jump from the end of the loop to reiterate the loop.
  2. The second part is the condition that must be true in order for the loop to reiterate.
  3. The third part is the statement to reiterate the fact that it is a loop. It is the end of the body of the loop.

Besides being composed of three parts, it also describes an action that is ubiquitous in programming. The action is repeating a chunk of code more than once. The fact that loops are ubiquitous in programming needs no explanation.

If you re-implement the loop pattern each time you need it, things can go wrong. As the pattern has more than one part to it, it can be corrupted by misusing one of the parts, or you could make a mistake when arranging the parts into a whole. It is possible to forget to name the label to which to jump, or to name it incorrectly. You may also forget to define the predicate statement that guards the jump to the beginning of the loop. Or, you could misspell the label to which to jump in the q statement itself. For example, in the following code, we forgot to specify the predicate guard:

int main() {
int x = 0;
loop_start:
x++;
cout << x << "\n";
goto loop_start;
return 0;
}

Example – nested loop

It's pretty hard to get such a simple example wrong, but consider a nested loop. For example, you have a matrix, and you want to output it to the console. This can be done with a nested loop. You have a loop to iterate on with every entry of the 2D array. Another loop nested in that loop examines the row the outer loop is currently working on. It iterates on every element of that row and prints it to the console.

It is also possible to express these in terms of the go-to statement. So, you will have the first label to signify the entry point into the large loop, another label to signify the entry point to the small loop, and you will call the go-to statement at the end of each loop to jump to the beginning of the respective loop.

Let's see how to do that. First, let's define a 2D array as follows:

 int rows = 3;
int cols = 3;
int matrix[rows][cols] = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};

Now, we can loop over it as follows:

 int r = 0;
row_loop:
if (r < rows) {
int c = 0;
col_loop:
if (c < cols) {
cout << matrix[r][c] << " ";
c++;
goto col_loop;
}
cout << "\n";
r++;
goto row_loop;
} return 0;}

You can already see an increase in complexity here. For example, you can perform a go-to from the end of the inner loop to the beginning of the outer loop. This way, only the first item of each column receives output. The program becomes an infinite loop:

 int r = 0;
row_loop:
if (r < rows) {
int c = 0;
col_loop:
if (c < cols) {
cout << matrix[r][c] << " ";
c++;
goto row_loop;
}
cout << "\n";
r++;
goto row_loop;
}

Don't Repeat Yourself (DRY)

One of the fundamental rules of engineering is to create abstractions for logic that repeats. The pattern of the loop is ubiquitous. You can experience it in almost any program. Hence, it is reasonable to abstract away. This is why contemporary languages, such as Java or C++, have their own built-in mechanisms for loops.

The difference it makes is that, now, the entire pattern consists of one component only, that is, the keyword that must be used with a certain syntax:

#include <iostream>
using namespace std;
int main() {
int rows = 3;
int cols = 3;
int matrix[rows][cols] = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) cout << matrix[r][c] << " ";
cout << "\n";
}
}

What happened here is that we gave a name to the pattern. Every time we need this pattern, we do not implement it from scratch. We call the pattern by its name.

This calling by name is the main principle of declarative programming: implement patterns that repeat only once, give names to those patterns, and then refer to them by their name anywhere we need them.

For example, while or for loops are patterns of loops. They are abstracted away and implemented on a language level. The programmer can refer to them by their names whenever they need a loop. Now, the chance of making an error is much less likely because the compiler is aware of the pattern. It will perform a compile-time check on whether you are using the pattern properly. For example, when you use the while statement, the compiler will check whether you have provided a proper condition. It will perform all the jump logic for you.

So, you do not need to worry whether you jumped to the correct label, or that you forgot to jump at all. Therefore, there is no chance of you jumping from the end of the inner loop to the start of the outer loop.

What you have seen here is the transition from an imperative to a declarative style. The concept you need to understand here is that we made the programming language aware of a certain pattern. The compiler was forced to verify the correctness of the pattern at compile time. We specified the pattern once. We gave it a name. We made the programming language enforce certain constraints on the programmer that uses this name. At the same time, the programming language takes care of the implementation of the pattern, meaning that the programmer does not need to be concerned with all the algorithms that were used to implement the pattern.

So, in declarative programming, we specify what needs to be done without specifying how to do it. We notice patterns and give them names. We implement these patterns once and call them by name afterward whenever we need to use them. In fact, modern languages, such as Java, Scala, Python, or Haskell do not have the support of the go-to statement. It seems that the vast majority of the programs expressed with the go-to statement can be translated into a set of patterns, such as loops, that abstract away go-to statements. Programmers are encouraged to use these higher-level patterns by name, rather than implementing the logic by themselves using lower-level go-to primitives. Next, let's see how this idea develops further using the example of declarative collections and how they differ from imperative ones.

Declarative versus imperative collections

Another great illustration of how the declarative style works can be seen in collection frameworks. Let's compare the collection frameworks of an imperative and functional programming language, for example, Java (imperative) collections and Scala (functional) collections.

Why a collection framework? Collections are ubiquitous in any programming project. When you are dealing with a database-powered application, you are using collections. When you are writing a web crawler, you are using collections. In fact, when you are dealing with simple strings of text, you are using collections. Most modern programming languages provide you with the implementation of collection frameworks as part of their core library. That is because you will need them for almost any project.

We'll go into more depth about how imperative collections are different from declarative collections in the next chapter. However, for the purpose of an overview, let's briefly discuss one of the major differences between the imperative and declarative approaches to collections here. We can see such a difference using the example of filtering. Filtering is an ubiquitous operation that you most likely will find yourself doing pretty often, so let's see how it differs across the two approaches.

Filtering

Java is a classic example of a very imperative approach to programming. And hence, in its collections, you will encounter operations that are typical of imperative programming. For example, consider that you have an array of strings. They are the names of the employees of your company. You want to create a separate collection with only those employees whose names start with the letter 'A'. How do you do that in Java?

// Source collection
List<String> employees = new ArrayList<String>();
employees.add("Ann");
employees.add("John");
employees.add("Amos");
employees.add("Jack");
// Those employees with their names starting with 'A'
List<String> result = new ArrayList<String>();
for (String e: employees)
if (e.charAt(0) == 'A') result.add(e);
System.out.println(result);

First, you need to create a separate collection to store the result of your computation. So, we create a new ArrayList of strings. Afterward, you will need to check every employee's name to establish whether it starts with the letter 'A'. If it does, add this name to the newly created array.

What could possibly go wrong? The first issue is the very collection where you want to store your results. You need to call result.add() on the collection – but what if you have several collections, and you add to the wrong one? You have the freedom to add to any collection at that line of code, so it is conceivable that you add to the wrong one – not the dedicated one you have created solely for the purpose of filtering the employees.

Another thing that can go wrong here is that you can forget to write the if statement in the large loop. Of course, it is not very likely in such a trivial example, but remember that large projects can bloat and code bases can become large. In our example, the body of the loop has fewer than 10 lines. But what if you have a code base where the for loop is up to 50 lines, for example? It is not as obvious there that you won't forget to write your predicate, or to add the string to any collection at all.

The point here is that we have the same situation as in the loop versus go-to example. We have a pattern of an operation over a collection that might repeat itself in the code base. The pattern is something that is composed of more than one element, and it goes as follows. Firstly, we create a new collection to store the result of our computation. Secondly, we have the loop that iterates on every element of our collection. And finally, we have a predicate. If it is true, we save the current element into the result collection.

We can imagine the same logic executed in other contexts as well. For example, we can have a collection of numbers and want to take only those that are greater than 10. Or, we can have a list of all our website users and want to take the age of those users visiting the site over a particular year.

The particular pattern we were discussing is called the filter pattern. In Scala, every collection supports a method defined on it that abstracts away the filter pattern. This is done as follows:

// Source collection
val employees = List(
"Ann"
, "John"
, "Amos"
, "Jack")
// Those employees with their names starting with 'A'
val result = employees.filter ( e => e(0) == 'A' )
println(result)

Notice that the operation remains the same. We need to create a new collection, then incorporate the elements from the old collection into the new collection based on some predicate. Yet, in the case of the pure Java solution, we need to perform three separate actions to get the desired result. However, in the case of the Scala declarative style, we only need to specify a single action: the name of the pattern. The pattern is implemented in the language internals, and we do not need to worry about how it is done. We have a precise specification of how it works and of what it does, and we can rely on it.

The advantage here is not only that the code becomes easier to read, and thus easier to reason about. It also increases reliability and runtime performance. The reason is that the filter pattern here is a member of the core Scala library. This means that it is well tested. It was used in a large number of other projects before. The subtle bugs that could have existed in such a situation were likely caught and fixed.

Also observe that the notion of anonymous lambdas gets introduced here. We pass one as an argument to the filter method. They are functions that are defined inline, without the usual tedious method syntax. Anonymous lambdas are a common feature of functional languages, as they increase your flexibility for abstracting logic.

Declarative programming in other languages

In other modern languages, such as Haskell or Python, a similar declarative functionality is also present out of the box. For example, you can perform filtering in Python—it is built into the language, and you have a special function in Haskell to perform the same filtering. Also, the functional nature of Python and Haskell makes it easy to implement the same control structure as filtering by yourself. Both Haskell and Python support the notion of the lambda function and higher-order functions, so they can be used to implement declarative control structures.

In general, you can spot whether a language is declarative programming-friendly by looking at the capabilities it provides. Some of the features you can look for are anonymous functions, functions as first-class citizens, and custom operator specifications.

Anonymous lambda gives you a great advantage because you can pass functions to other functions inline, without first defining them. This is particularly useful when specifying control structures. A function expressed in this way is, first and foremost, to specify a transformation that is supposed to transform an input into an output.

Another feature that you can look for in programming languages is support for functions as first-class citizens. This means that you are able to assign a function to a variable, refer to the function by that variable's name, and pass that variable to other functions. Treating functions as if they are ordinary variables allows you to achieve a new level of abstraction. This is because functions are transformations; they map their input values to some output values. And, if the language does not allow you to pass transformations to other transformations, this is a limitation of flexibility.

Another feature that you can expect from declarative languages is that they allow you to create custom operators; for example, the synthetic sugar available in Scala allows you to define new operators very easily, as methods in classes.

Summary

The declarative style is a style of programming where you call the operations you want to perform by name, instead of describing how to execute them in an algorithmic fashion via lower-level primitives provided by the programming language. This naturally aligns with the DRY principle. If you have a repeating operation, you want to abstract it away, and then refer to it by name later on. In other words, you need to declare that the operation has a certain name. And, whenever you want to use it, you need to declare your intent, without specifying directly how it should be fulfilled.

Modern functional programming goes hand in hand with the declarative style. Functional programming provides you with a better level of abstraction, which can be used to abstract away the repeating operations.

In the next chapter, we will see how first-class citizen support for functions can be useful for the declarative programming style.

Questions

  1. What is the principle behind declarative programming?
  2. What does DRY stand for?
  3. Why is using go-to bad style?
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn functional programming from scratch
  • Program applications with pure functions to rule out side effects
  • Gain expertise in working with array tools for functional programming

Description

Functional programming is a paradigm specifically designed to deal with the complexity of software development in large projects. It helps developers to keep track of the interdependencies in the code base and changes in its state in runtime. Mastering Functional Programming provides detailed coverage of how to apply the right abstractions to reduce code complexity, so that it is easy to read and understand. Complete with explanations of essential concepts, practical examples, and self-assessment questions, the book begins by covering the basics such as what lambdas are and how to write declarative code with the help of functions. It then moves on to concepts such as pure functions and type classes, the problems they aim to solve, and how to use them in real-world scenarios. You’ll also explore some of the more advanced patterns in the world of functional programming such as monad transformers and Tagless Final. In the concluding chapters, you’ll be introduced to the actor model, which you can implement in modern functional languages, and delve into parallel programming. By the end of the book, you will be able to apply the concepts of functional programming and object-oriented programming (OOP)in order to build robust applications.

Who is this book for?

If you are from an imperative or OOP background, this book will guide you through the world of functional programming, irrespective of which programming language you use.

What you will learn

  • Write reliable and scalable software based on the principles of functional programming
  • Explore advanced functional concepts such as lambdas, generic type parameters, and higher-order functions
  • Effectively solve complex architectural problems
  • Avoid unwanted outcomes such as errors or delays and focus on business logic
  • Write parallel programs in a functional style using the actor model
  • Use functional data structures and collections in your day-to-day work
Estimated delivery fee Deliver to Cyprus

Premium delivery 7 - 10 business days

€32.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 31, 2018
Length: 380 pages
Edition : 1st
Language : English
ISBN-13 : 9781788620796
Category :
Languages :

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Cyprus

Premium delivery 7 - 10 business days

€32.95
(Includes tracking information)

Product Details

Publication date : Aug 31, 2018
Length: 380 pages
Edition : 1st
Language : English
ISBN-13 : 9781788620796
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total €84.96 €95.97 €11.01 saved
Software Architect’s Handbook
€41.99
Hands-On Serverless Applications with Go
€36.99
Mastering Functional Programming
€41.99
Total €84.96€95.97 €11.01 saved Stars icon

Table of Contents

16 Chapters
The Declarative Programming Style Chevron down icon Chevron up icon
Functions and Lambdas Chevron down icon Chevron up icon
Functional Data Structures Chevron down icon Chevron up icon
The Problem of Side Effects Chevron down icon Chevron up icon
Effect Types - Abstracting Away Side Effects Chevron down icon Chevron up icon
Effect Types in Practice Chevron down icon Chevron up icon
The Idea of the Type Classes Chevron down icon Chevron up icon
Basic Type Classes and Their Usage Chevron down icon Chevron up icon
Libraries for Pure Functional Programming Chevron down icon Chevron up icon
Patterns of Advanced Functional Programming Chevron down icon Chevron up icon
Introduction to the Actor Model Chevron down icon Chevron up icon
The Actor Model in Practice Chevron down icon Chevron up icon
Use Case - A Parallel Web Crawler Chevron down icon Chevron up icon
Introduction to Scala Chevron down icon Chevron up icon
Assessments Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(1 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Amazon Customer May 21, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Me ha gustado conocer algo más sobre esta manera de programación, la programación funcional aplicada en Scala. Volveré a vendedor, ya que sus servicios están a la altura de nuestros días. Gracias por todo.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela