Functional.js part 2

Type clases

(This article will not contain much javascript. It contains more the consepts of functional programming that will be important when discussing more advance topice later)

What is type classes? A definition of a type class is a function that takes type variables of a type and returns a new type that depends on thoose type variables.

This means that you have a free type variable that you can define some type constraint on a “class” of functions. This free type variable is called parametrically polymorphism. If this sound familiar to some Java and C# developers this is called generics.

In haskell:

1
2
3
class Eq a where
(eq) :: a -> a -> Bool
(neq) :: a -> a -> Bool

In C# code:

1
2
3
4
public interface Eq<T> {
bool eq(T x, T y);
bool neq(T x, T y);
}

In javscript code:

1
2
3
4
//+ eq :: T -> T -> Boolean
//+ neq :: T -> T -> Boolean
//No type checker :(

Read More

Functional.js part 1

Introduction

This blog series I will cover the how and why functional in js. This part is about how to compose functions. The next parts will cover Type Classes, fmap, map, fold, monoids, functors, moands and more goodies.

I will not cover higher order function in this series. If you do not know what they are or how they work take a look at this post. Nor will I cover basic array manipulation like map, reduce and filter.

The first thing to understand functional programming is to understand types. When I say types I don’t mean the traditional types of C static languages. I mean a inferred type system like Haskell. The types make easier to understand the functions and analyzes them. (For a more detaild explonation).

We want typechecking so we don’t accidentally try to shove a square peg into a round hole and create bugs in the process.

Read More