# TypeScript as a language for Angular applications

The reason is that developing in JavaScript isn’t overly productive. Say a function expects a string value as an argument, but the developer mistakenly invokes it by passing a numeric value. With JavaScript, this error can be caught only at run- time. Java or C# compilers won’t even compile code that has mismatching types, but JavaScript is forgiving because it’s a dynamically typed language.

On larger projects, good IDE context-sensitive help and support for refactoring are important. Renaming all occurrences of a variable of function name in spatially typed languages is done by IDEs in a split second, even in projects that have thousands oaf lines of code; but this isn’t the case in JavaScript, which doesn’t support types. IDEs can help with refactoring much better when the types of the variables are known.

To be more productive, you may consider developing in a statically typed language and then convert the code to Javascript for deployment. Currently there are dozens of languages that compile to JavaScript. The most popular are TypeScript, CoffeeScript, and Dart.

Why not use the Dart language?

• Interoperability with third-party Javascript libraries isn’t that great.
• Development in Dart can be done only in a specialized version of the Chrome browser (Dartium) that comes with the Dart VM.
• The generated JavaScript isn’t easily readable by a human.
• The community of Dart developers is rather small.

# JavaScript definite guide – Arrays

An array is an ordered collection of values. Each value is called an element, and each element has a numeric position in the array, known as its index.

• JavaScript arrays are untyped: an array element may be of any type, and different elements of the same array may be of different types. Array elements may even by objects o other arrays, which allows you to create complex data structures, such as arrays of objects and arrays of arrays.
• JavaScript arrays are zero-based and use 32-bit indexes: the index of the first element is `0`, and the highest possible index is 4294967294 ($2^32−2$), for a maximum as needed and there is no need to declare a fixed size for the array when you create it or to reallocate it when the size changes.
• JavaScript arrays may be sparse: the elements need not have contiguous indexes and there may be gaps.
• Every JavaScript array has a `length` property.
• For non sparse arrays, this property specifies the number of elements in the array.
• For sparse arrays, `length` is larger than the index of all elements.

JavaScript arrays are specialized form of JavaScript object, and array indexes are really little more than property names that happen to be integers. Implementations typically optimize arrays so that access to numerically indexed array elements is generally significantly faster than access to regular object properties.

# iOS development with Swift 3 – Your first iOS app

## What is Swift?

Swift is the modern language created by Apple which got the Apple developer world buzzing. While loved by iOS developers, Objective-C was at the same time seen by many others as an outmoded language – a verbose and peculiar syntax, and with an unsafe type system. Built as a modern alternative to Objective-C, Swift was designed with specific enhancements in mind, specifically safety, performance, and expressiveness.

• _Safety_ — Swift introduced several programming concepts to reduce some common programmer mistakes. These include strong typing, optionals and error handling.
• _Performance_ — Apple has introduced internal optimizations to ensure that Swift runs fast. Xcode also provides warnings to encourage you to write code that ensures your app is running optimally.
• _Expressiveness_ — Expressive code has the right balance between containing a clear meaning and being succinct.

# 日语谓语的各种形态 – “基本形”

## 动词的“基本形“

### 语法解释

“基本形”是动词的基本形式。词典中的词条都使用这一形式。各类动词“ます形”去掉“ます”的形式和“基本形”的关系对应如下：

• 一类动词：“基本形”最后的发音为“う”段，“ます形”去掉“ます”后的发音为“い”段。
• 二类动词：“基本形”为“ます形”去掉“ます”后加“る”。
• 三类动词：“基本形”分别为“来る”和“する”，“ます形”去掉“ます”后则为“来”和“し”。

# 日语谓语的各种形态 – “た形”

## 动词的“た”形

### 语法解释

“た”形的变换方式是把“で”形的“て”换成“た”，把“で”换成“だ”。

# 日语谓语的各种形态 – 敬体形和简体形

## 语法解释

### 动词的简体形

• 森さんは　毎晩　テレビを　見る。
• コーヒー　飲む？

ー　ううん、飲まない。

• 昨日　テレビ、　見た？

ー　ううん、　見なかった。

# Few data structures, many operations

This chapter covers:

• Understanding program control and flow
• Reasoning efficiently about code and data
• Unlocking the power of `map`, `reduce` and `filter`
• Discovering the Lodash.js library and function chains
• Thinking recursively

In this chapter, I’ll introduce you to a few useful and practical operations like `map`, `reduce` and `filter` that allow you to traverse and transform data structures in a sequential manner. These operations are so important that virtually all functional programs use them in one way or another. They also facilitate removing manual loops from your code, because most loops are just specific cases handled by these functions.

You’ll also learn to use a functional JavaScript library called Lodash.js. It lets you process and understand not only the structure of your application, but also the structure of your data. In addition, I’ll discuss the important role recursion plays in functional programming and the advantages of being able to think recursively. Building on these concepts, you’ll learn to write concise, extensible and declarative programs that clearly separate control flow from the main logic of your code.

## Understanding your application’s control flow

The path a program takes to arrive at a solution is known as its control flow. An imperative program describes its flow on path in great detail by exposing all the necessary steps needed to fulfill its task. These steps usually involves lots of loops and branches, as well as variables that change with each statement. At a high level, you can depict a simple imperative program like this:

``````var loop = optC()
while (loop) {
var condition = optA()
if (condition) {
optB1
}
else {
optB2()
}
loop = optC()
}
optD()
``````

Figure 3.1 shows a simple flowchart of this program.

On the other hand, declarative programs, specifically functional ones, raise the level of abstraction by using a minimally structured flow made up of independent black-box operations that connect in a simple topology. These connected operations are nothing more than higher-order functions that move state from one operation to the next, as shown in figure 3.2. Working functionally with data structures such as arrays lends itself to this style of development and treats data and control flow as simple connections between high-level components.

This produces code more or less like the following:

``````optA().optB().optC().optD() // Connecting via dots suggests the presence of a shared object that contains these methods.
``````

Chaining operation in this manner leads to concise, fluent, expressive programs that let you separate a program’s control flow from its computational logic. Thus, you can reason about your code and your data more effectively.

# Higher Order JavaScript

This chapter covers

• Why JavaScript is a suitable functional language
• JavaScript as a language that enables multi paradigm development
• Immutability and policies for change
• Understanding higher-order and first-class functions
• Exploring the concepts of closures and scopes
• Practical sue of closures

As applications get bigger, so does their complexity. No matter how good you think you are, turmoil is unavoidable if you don’t have the proper programming model in place.In chapter 1, I explained the reasons functional progrmaming is a compelling paradigm to adopt. But paradigms by themselves are just programming modes that need the right host language to come to life.

In this chapter, I take you on a fast-pass tour of a hybrid langue that mixes both object-oriented as well as functional programming: JavaScript. Of course, this is by no means an extensive study of the language; rather, I’ll focus on what allows JavaScript to be used functionally as well as where it falls short. One example of this is the lack of support for immutability. In addition, this chapter covers higher-order functions and closures, which totogethaer from the backbone that allows yo to write JavaScript in a function style. Without further ado, let’s dive in.

## Why JavaScript?

I began by answering the question, ” Why functional?” Another question that comes to mind is, “Why JavaScript?” The answer to this question is simple: omnipresence. JavaScript is a dynamically typed, object-oriented, general-purpose language with an immensely expressive syntax. It’s one of the most ubiquitous languages ever created and can be seen in the development of mobile applications, se sites, web severs, desktop and embedded applications, and even databases. Given its extraordinary adoption as the language of the web, it begs to reasons the JavaScript is by far the most widely used FP language ever created.

Despite its C-like syntax, JavaScript draws lots of inspiration from functional languages like Lisp and Scheme. The commonalities lie in their support for higher-older functions, closures, array literals, and other features that make JavaScript a superb platform for applying FP techniques. In fact, functions re the main units of work in JavaScript, which means they’re used not only to drive the behavior off our applications, but also to define objects, create modules, and handle events.

JavaScript is actively evolving and improving. Backed by the ECMAScript (ES) standard, its next major release, ES6, adds many more features to the language: arrow functions, constants, iterators, promise, and other features that suit functional programming very well.

Despite the fact that it has lots of powerful functional features, it’s important to know that javaScript is as objet-oriented as it is functional. Unfortunately, the latter is rarely seen; most developers use mutable operations, imperative control structures, and instance state changes on objects, which are all virtually eliminated when adopting a functional style. Nevertheless, I feel it’s important to spend some time talking about JavaScript as an object-oriented language first so that you can better appreciate the key differences between the two paradigms. This will allow you to leap into functional programming more easily.

# Functional Programming in JavaScript — Becoming functional

## What is functional programming?

In simple terms, functional programming is a software development style that places a major emphasis on the use of functions.

What better text to print than the good ol “Hello World”:

``````document.querySelector('#msg').innerHTML = '<h1>Hello world</h1>'
``````

This program is simple, but because everything is hardcoded, you can’t use it to display messages dynamically. Say you wanted to change the formatting, the content, or perhaps the target element; you’d need to rewrite the entire expression. Maybe you decide to wrap this code with a function and make the change points parameters, so you can write it once and use it with any configuration:

``````function printMessage(elementId, format, message) {
document.querySelector('#{elementId').innerHTML = `<\${format}>\${message}</\$Pformat}>`
}
printMessage('msg', 'h1', 'Hello World')
``````

An improvement, indeed, but still not a complete reusable piece of code. Suppose you want to write to a file instead of an HTML page. You need to take the simple thought process of creating parameterized functions to a different level, where parameters aren’t just scalar values but can also be functions themselves that provide additional functionality. Functional programming is a bit like using functions on steroids, because you sole objective is to evaluate and combine lots of functions with others to achieve greater behavior.

``````// Listing 1.1 Functional `printMessage`
var printMessage = run(addToDom('msg'), h1, echo)
printMessage('Hello World')
``````

Without a doubt, this looks a radically different than the original. For starters, `h1` isn’t a scalar anymore; it’s a function just like `addToDom` and `echo`. Visually, it feels as though you’re creating a function from smaller functions.

# AppleScript programming – Introducing AppleScript

AppleScript is for Macintosh automation; so scripts you write can automate tasks you would otherwise have to perform with the mouse and keyboard.

The main difference between small scripts and big scripts is the amount of though and planning involved.

• When writing small scripts, you start out by … writing the script. As the scope progresses, you add more functions, you add more lines, and you go back to fix and debug until the script works.
• Big scripts require planning. You have to carefully consider the users, the environment, and the different triggers for tasks the system will perform. You have to deal with where the data for the script comes from, small graphical user interfaces, and return on investment for your script or script project — all the issues that can kill your efforts just as you start to get excited.

Small scripts are the way one usually starts to become familiar with AppleScript. AppleScript lest you easily create a script, test it, save it as an application, and start using it. This quick turnaround for truly useful utilities is part of the winning recipe that makes AppleScript so great.

``````tell applicatio "QuarkXPress 4.01"
tell every page of document 1
set width of (every line box whose width < 0.25) to 0.25
end tell
end tell
``````

Although AppleScript doesn’t sweat the small stuff, it can be the base for large-scale custom integrated system. As far as what to automate, you could automate any task that has repeatable logic, and if the cost of automation is less than the amount of money you save — and it usually is — the task is worth automating. Repeatable logic is logic that can be applied successfully to similar, but not identical, subjects and that produces a predictable outcome, based on the uniqueness of the subject.

For example, here is a process that is not so suitable for automation:

Here is a better candidate for automation:

When you get a job from a client, you search for the client in the database to locate the job information. If the job is marked “urgent,” you e-mail the imaging room to expect it within ten minutes. You open the Adobe InDesign document and verify that all the fonts were provided. You make sure the document contains the number of pages indicated in the electronic job ticket; if it doesn’t, you make a note in the database and e-mail the client. When done, you make a copy of the “job” folder in the “archive” folder on the server, and you send another copy to the “in” folder on the production server. You repeat these steps for all jobs that arrived last night.

Now this is a process for which AppleScript is perfect. Besides having programmatic elements such as a repeat loop for applying the process to all jobs and some branching, the process itself s clear, is logical, and can be applied to any client job that arrives.

If you look more closely at the previous process, you can see that quite a few of the steps happen in software applications. The operator has to know ho to use FileMaker Pro for searching and entering data; how to use InDesign for checking fonts, pages, and so on; and how to use the Finder or moving folders. Controlling these and many other applications is one of AppleScript’s main strengths. AppleScript can give applications commands and get information from applications, such as retrieving the data in a FileMaker Pro record or the number of pages in an InDesign document.

AppleScript is also ideal for performing repetitive tasks. For instance, how would you like to show up at work one morning and realize that your job for the day is go through 200 TIFF images in a folder, add a black frame to each one, and export each as JPEG? Then, you must create a PDF file containing all the images, with four-up (which means having four impressions of similar size on a single printed sheet) and with the image name under each image. Does this sound like hard work? Your reward for a job Weill done just might be another folder with 200 TIFF images the next day …

Although not sophisticated, this process is repetitive. A script can do it for you while you take a walk to the cafeteria.