Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
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
Opa Application Development
Opa Application Development

Opa Application Development: A rapid and secure web development framework to develop web applications quickly and easily in Opa

eBook
€13.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
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

Billing Address

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

Opa Application Development

Chapter 2. Basic Syntax

In this chapter, we will introduce the basic syntax of Opa. This chapter will not cover every little tiny thing about Opa, but it is something you should know. It's also assumed that you have some basic knowledge about computer programming.

Basic datatypes


Datatypes are the shapes of data manipulated by an application. Opa uses datatypes to perform sanity and security checks on your application. Opa also uses datatypes to perform a number of optimizations. There are three basic datatypes in Opa: integers, floats, and strings. Also, you can define your type with the keyword type:

type Student = {string name, int age, float score}
Student stu = { name:"li", age:28, score:80.0}

Actually, thanks to a mechanism of type inference, Opa can work in most cases even if you do not provide any type information. For example:

x = 10        // the same as: int x = 10
x = {a:1,b:2} // the type of x is: {a:int, b:int}

So in the rest of this chapter, we will not address type information before variable, but you should know what type it is in your mind. In actual coding, a best practice is to provide the datatypes of our main functions and to let the inference engine pick up the datatypes of all the local variables and minor functions.

Integers

It...

Functions


Opa is a functional language. One of its features is that functions are regular values, which means a function may be passed as a parameter or returned as a result. As such, they follow the same naming rules as any other value.

function f(x,y){      // function f with the two parameters x and y
  x + y + 1
}
function int f(x,y){  // explicitly indicates the return type
  x + y + 1
}

Last expression return

You may notice that there is no return inside the body of a function. That's because Opa uses last expression return, which means the last expression of a function is the return value. For example:

function max(x,y){
  if(x >= y) x else y
}

If x is greater than or equal to y, then x is the last expression and x will be returned; if y is greater than x, then y is the last expression and y will be returned.

Modules

Functionalities are usually regrouped into modules; for example:

module  M {
  x = 1
  y = x
  function test(){ jlog("testing") }
}

We can access the content of a module by...

Data structures


The only way to build data structures in Opa is to use records, which we will talk about later on. All other data structures, such as tuples and lists, are based on records. Opa provides different modules to help the user to manipulate lists and maps. Let's first have a look at records.

Records

Simply speaking, a record is a collection of data. Here is how to build a record:

x = {} // the empty record
x = {a:2,b:3} //a record with field "a" and "b"

The empty record,{}, has a synonym, void, which means the same thing. There are a number of syntactic shortcuts available to write records concisely. First, if you give a field name without the field value, it means the value of this field is void:

x = {a}      // means {a:void}
x = {a, b:2} // means {a:void b:2}

The second shorthand we always use is the sign ~. It means if the field value is left empty, assign it with a variable having the same name as the field name:

x = {~a, b:2}    // means {a:a, b:2}
x = ~{a, b}      // means {a...

Pattern matching


Pattern matching is a generalization of C language or Java's switch statement. In C language and Java, the switch statement only allows you to choose from many statements based on an integer (including char) or an enum value. While in Opa, pattern matching is more powerful than that. The more general syntax for pattern matching is:

match(<expr>){
case <case_1>: <expression_1>
case <case_2>: <expression_2>
case <case_n>: < expression_n>
}

When a pattern is executed, <expr> is evaluated to a value, which is then matched against each pattern in order until a case is found. You can think about it this way:

if (case_1 matched) expression_1 else {
  if (case_2 matched) expression_2 else {
    ...
         if (case_n matched) expression_n else no_matches
         ...
  }
}

The rules of pattern matching are simple and are as follows:

  • Rule 1: Any value matches the pattern _

  • Rule 2: Any value matches the variable pattern x, and the value...

Text parsers


Parsing is something that web apps need to do quite often. Opa features a built-in syntax for building text parsers, which are first class values just as functions. The parser is based on parsing expression grammar (http://en.wikipedia.org/wiki/Parsing_expression_grammar), which may look like regular expressions at first, but do not behave anything like them. One big advantage of text parsers over regular expressions is that you can easily combine parsers. A good example is parsing URLs. Let's start right away with our first Opa parser:

first_parser = parser { 
case "Opa"  : 1 
}

For first_parser, the expressions are just literal strings, which means this parser will succeed only if fed with the string "Opa". Then how to use this parser? The module Parser (http://doc.opalang.org/module/stdlib.core.parser/Parser) has a bunch of functions to deal with parsers. The most important one is:

Parser.try_parse : Parser.general_parser('a), string -> option('a)

It takes a parser and a string...

Summary


This chapter has introduced you to the basic syntax in Opa programming, including datatypes, functions, records, tuples, lists, maps, patterns, and parsers. This is the basic knowledge that we should know to make a good Opa program. With the previous knowledge, we will see how to develop a web application in the next chapter.

Left arrow icon Right arrow icon

Key benefits

  • Discover the Opa framework in a progressive and structured way
  • Build secure, powerful web applications with Opa.
  • Create three complete web application demos with Opa.

Description

Opa is a full-stack Open Source web development framework for JavaScript that lets you write secure and scalable web applications. It generates standard Node.js/MongoDB applications, natively supports HTML5 and CSS and automates many aspects of modern web application programming. It handles all aspects of web programming written in one consistent language and compiled to web standards.Opa Application Development is a practical,hands-on guide that provides you with a number of step-by-step exercises. It covers almost all aspects of developing a web application with Opa, which will help you take advantage of the real power of Opa, as well as building a secure, powerful web application rapidly.Opa Application Development dives into all concepts and components required to build a web application with Opa. The first half of this book shows you all of the basic building blocks that you will need to develop an Opa application, including the syntax of Opa, web development aspects, client and server communication and slicing, plugin, database, and so on. By the end of the book you will have yourself created a complete web application along with a game: Pacman!

Who is this book for?

If you are a web developer who wants to get started with the Opa framework and build web applications with it? Then this book is for you. Web development experience is assumed and would be helpful.

What you will learn

  • Set up Opa development environment
  • Develop a web application, manipulate DOM and CSS.
  • Use bootstrap in Opa, including classes, icons and widgets.
  • Slice client and server code.
  • Bind JavaScript and Nodejs to Opa and build a plugin.
  • Store, update and query data in database with MongoDB as the backend.
  • Build a chat application, LiveRoom application in Opa.
  • Handle image and audio with canvas to build a Pacman game

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 12, 2013
Length: 116 pages
Edition : 1st
Language : English
ISBN-13 : 9781782163756
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
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

Billing Address

Product Details

Publication date : Jun 12, 2013
Length: 116 pages
Edition : 1st
Language : English
ISBN-13 : 9781782163756
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 44.98
Instant MongoDB
€19.99
Opa Application Development
€24.99
Total 44.98 Stars icon

Table of Contents

11 Chapters
Getting Started with Opa Chevron down icon Chevron up icon
Basic Syntax Chevron down icon Chevron up icon
Developing Web Applications Chevron down icon Chevron up icon
Using Bootstrap Chevron down icon Chevron up icon
Communicating between Client and Server Chevron down icon Chevron up icon
Binding with Other Languages Chevron down icon Chevron up icon
Working with Databases Chevron down icon Chevron up icon
Internationalization Chevron down icon Chevron up icon
Building a Chat Application Chevron down icon Chevron up icon
Building a Game – Pacman Chevron down icon Chevron up icon
Developing a Social Mobile Application – LiveRoom Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4
(5 Ratings)
5 star 40%
4 star 60%
3 star 0%
2 star 0%
1 star 0%
Doug Duncan Jul 07, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
'Opa Application Development' by Li Wenbo is a well written introductory book that can take a person with no experience of the Opa Language to feeling comfortable with the language and allowing them to start programming right away.The book is short (only about 100 pages to read through), but it is packed with examples of the topics discussed. While it doesn't cover everything about the language, it does give you enough of the basics for building your application and gives pointers where to further research on your own when you need more advanced topics.I would highly recommend this book to anyone looking to learn about the Opa language.
Amazon Verified review Amazon
SuJo Jul 15, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Opa Application Development[...]This guide/book has really explored the Opa Framework. While there are plenty to choose from after reading this I would give it a shot, I really appreciate the how to features the Author uses.You should be intermediate in your web development skills to get the most out of this book. I would recommend this to anyone looking to use this framework, it's not a cut n paste from the manual!
Amazon Verified review Amazon
Amazon Customer Jul 22, 2013
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This is the second book I've read on Opa (the other being O'Reilly's Opa: Up and Running). I think the contents of the book have been great but the book's example code could have been better. The code itself is fine but it's organized a little strange (naming convention for example). You can download the code before hand to check it out.
Amazon Verified review Amazon
David Moser Jul 05, 2013
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This is a clear, straight-forward introduction to the Opa web-programming language. It is full of original and useful content. (Disclaimer: I was given a free copy for this review.) I am an enthusiastic admirer of the Opa language project, and found this book to be a great addition to my library. To help future buyers with their own purchasing decision, the rest of this review has been split into two parts; the bad, and the good. The bad: Not much was bad about this book, but I didn't give it all five stars, because it really is not quite as much of a book for those new to the language as it could be. I used it to help some students of mine learn about coding in Opa, and found that, really, they needed a little more background than the HTML/CSS/.js suggested as prerequisites. However, anyone with experience with Ruby on Rails, one of the Python web frameworks (Django, I'm lookin' at you ...), or any of the Javascript frameworks could easily jump right into the book and, by the end, have enough of a background to code up a pretty good, lean web app. Which brings me to. ... The good: This was a really fun way to learn Opa! (Another disclaimer: I also have read and will be reviewing another book on Opa.) I had no trouble at all using the examples in this book as a way to help me and others (my students) learn how to work the Opa web-programming language. This book is a series of guidelines, with all code explicitly spelled out. That's perfect for me, as I find the "textbook" approach, of giving problem-sets and quizzes irritating and pedantic. "Opa Application Development" is pleasantly informative and easy to understand. I am quite confident in my abilities to use Opa for production websites/web apps in the future, thanks to this book. To recap: Yes, buy this book, especially if you like (as I do) to have lots of examples presented as a guideline.
Amazon Verified review Amazon
Pirx Danford Jul 26, 2013
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
I read this book as introduction to Opa, because the language catched my eye and I was interested to learn more about it.While writing this review I have 15 years of working as developer under my belt, since the new millenium mostly with PHP.The versatility of PHP is a double-edged sword, its tempting to just stick to it, because for nearly every problem there is a solution available, but there are issues I am not happy with, so I am looking around for possible alternatives."Opa Application Development" helped me a lot in getting my head into the depths of Opa, but it is not an unconditional recommendation.Firstly you really have to have some experience in programming, because the book does expect at least basic knowledge and then some.Secondly you must expect some trouble getting Opa up and running, that the book uses more than 10% of its pages on explaining how to set everything up is indeed necessary and already the required steps are changing. For example when I installed Node.js it was in version 0.10.12 and from 0.10 and above it already includes npm, so trying to install npm separately gave me the funniest kind of errors.Thirdly there is a number of typos, also in the source code examples, but that is not so bad if you download the source examples and use the provided files.Putting this all aside, if you persevere and work completely through the book, you will have the tools and knowledge to successfully start your own project in Opa.At this moment in time there are not too many books about Opa and while this book is by no means easy to digest, its is not a bad pick to get started in Opa if you have a professional background.For me it really whet my appetite for more and I will try out if I can use Opa in one of my private projects.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.