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 are type classes? A definition of a type class is a function that takes some type variables of a type and returns a new type that depends on thoose type variables. So it is like a function that operates on types.

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 also 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