A mostly mathless discussion of a common functional pattern
The best article ever about monoids. And here are the next two parts:
https://fsharpforfunandprofit.com/posts/monoids-part2/
https://fsharpforfunandprofit.com/posts/monoids-part3/

seen from Malaysia
seen from China

seen from United States
seen from Canada
seen from Russia
seen from China
seen from Venezuela

seen from Australia
seen from Russia

seen from Türkiye
seen from Philippines

seen from Germany
seen from France
seen from Japan
seen from United States

seen from Singapore

seen from Germany
seen from South Korea
seen from Japan

seen from Canada
A mostly mathless discussion of a common functional pattern
The best article ever about monoids. And here are the next two parts:
https://fsharpforfunandprofit.com/posts/monoids-part2/
https://fsharpforfunandprofit.com/posts/monoids-part3/
# 忙しい人向け TLDRってやつです。3行でまとめました * 「関数型プログラミング」は**厳密にどういうプログラミングスタイルだと決まっているわけではない**が、大まかには関数を組み合わせてプログラミングするスタイルのことであ...
Javascript at server side (part I)
Many of us have in some way used Javascript inside a browser. We know what the DOM is, how to use CSS selectors to operate on DOM elements using JQuery and how to manage Ajax requests. That's the standard stack at the client side of web applications throughout the first decade of 21th century. But with the arise of cloud computing, SaaS and big data challenges we are rethinking the way we code at both client and server side. Regarding server side, asynchronous server architectures driven by the Reactor Pattern have become a must have in order to achieve systems able to scale out and remain responsive to global audiences.
We have several alternatives at server side to achieve that goal. It's a non trivial issue to make a decision on one of them. At the time of this writing a very popular one is node.js. Node.js is a [V8](http://en.wikipedia.org/wiki/V8_(JavaScript_engine) Javascript runtime that implements the reactor pattern. Today we are going to start a series of posts about how to adapt our Javascript (client targeted) knowledge to the server side in order to implement maintainable, clean code for node.js. I assume you already have used Javascript at client side. If you have done so but you are not a Javscript Jedi, nor you have it crystal clear how to use it at server side this series of posts are for you.
Introduction to functions
Javascript is a dynamic, functional featured language. Therefore functions are a core part of it. Our first stop will be trying to gain a deep understanding of what a function is in Javascript. Good resources for this topic are detailed at the end of this post.
Functions as a first class citizens
Functions are a core construct in Javascript. As any other type, they can be defined and invoked anywhere, and passed around as parameters to other functions. We might have also have functions inside other functions:
function foo() { function sqr(n) { return n * n; } return sqr(9); } foo();//81
Function declarations
We declare functions with a name:
//A function called foo function foo() { //Some code here }
An we can invoke them writing () after them or invoking call() on them:
//Both execute foo function foo(); foo.call();
The scope of a function declaration is it's own and the parent (where the function itself is defined).
Function expressions
Function expressions are functions as part of a larger expression. It can be for example a variable assignment or a parameter passed into another function.
//A function inside a variable declaration which refer to the function itself var square = function(x) { return x * x; }; //A function as a parameter to the foo function foo(function(){ /* Some code here */ });
The above showed functions are call anonymous functions because they don't have a name. A function expression could carry a non anonymous function as well.
var foo = function bar() { //Code here };
In case we have a named function in an expression assigned to a variable the function will only be referable through the variable. It's name attribute will be the name of the function.
var foo = function bar() { return 10; }; foo();//returns 10 bar();//ReferenceError: bar is not defined foo.name;//returns 'bar'
The name attribute of an anonymous function is an empty string.
var foo2 = function() { return 100; } foo2.name;//returns ''
The scope of function expressions is the same as variables. Take a look at the next section for details.
Declaration Vs. evaluation
This is an important concept in functional programming. When an expression is declared (or defined) it's not evaluated yet. Only gets evaluated when invoked.
//Declaration of a function expression var sqr = function(x) { return x * x; } //The above defined functions has not been executed yet //Evaluation of the expression sqr(2);//returns 4
Functions can be used either as input or output in other functions
Yes; we can pass a function as a parameter to another function (as we have seen before) or even return a function within another function:
var sqr_function = function() { var actual_function = function(x) { return x * x; } return actual_function; } sqr_function()(3);//Returns 9
Ok, this is a pointless example, but you get the idea, right?
Scoping of variables in Javascript
An important feature of Javascript functions is that they are the only construct in the language allowed to delimitate scopes for variables. In Java and C family languages we can limit an scope with any code block. In Javascript only functions create a new scope. But, contrary to for example Java, it allows us to declare the same variable in the same scope or a nested one without error. See these examples taken from here:
var x = 1; console.log(x); // 1 if (true) { var x = 2; console.log(x); // 2 } console.log(x); // 2
function foo() { var x = 1; if (x) { (function () { var x = 2; // some other code }());//This is a nested self calling function } // x is still 1. }
As you can see a workaround to create a scope inside a function is to create a nested self calling function.
This kind of variable overriding is possible due to the use of the var reserved word. It's the way to define a local variable in Javascript (local to the current scope, that as we have mentioned can be only defined by functions). If we want to define global variables (global to the current scope) we just declare them without the var keyword.
var local = "I'm local"; global = "And I'm global";
Let's see some examples of local and global use of variables in different scopes:
var foo = function() {//Outer scope var x = 5; var bar = function() {//Inner scope var x = 10; return x; } bar(); return x;//returns 5; the second x variable is in another scope }
var foo = function() {//Outer scope var x = 5; var bar = function() {//Inner scope var x = 10; return x; } return bar();//returns 10 }
var foo = function() {//Outer scope x = 5; var bar = function() {//Inner scope x = 10; return x; } bar(); return x;//returns 10; the x is global, so function bar() changes it's value }
var foo = function() {//Outer scope var x = 5; var bar = function() {//Inner scope var y = 10; return x; } bar(); return y;//ReferenceError: y is not defined. Local variables defined with var are not //visible outside their scope (here, the function expression assigned to bar //variable) }
What's next
In the next post of this series I'll get into objects in Javascript and how to adopt an object oriented paradigm.
Resources
Function declarations vs. function expressions.
Javascript scoping and hoisting.
Javascript basics.