# paradigms

# imperative programming

Imperative programming

In computer science, imperative programming is a programming paradigm that uses statements that change a program's state.

Imperative programming focuses on describing how a program operates.

# declarative programming

Declarative programming

is a programming paradigm [...] that expresses the logic of a computation without describing its control flow.

Many languages that apply this style attempt to minimize or eliminate side effects by describing what the program must accomplish in terms of the problem domain, rather than describe how to accomplish it as a sequence of the programming language primitives (the how being left up to the language's implementation). This is in contrast with imperative programming, which implements algorithms in explicit steps.

# structured programming

Structured programming

don't use unrestrained GOTO (Robert C. Martin)

aimed at improving the clarity, quality, and development time of a computer program by making extensive use of subroutines, block structures, for and while loops—in contrast to using simple tests and jumps such as the go to statement which could lead to "spaghetti code" causing difficulty to both follow and maintain.

# functional programming

Functional programming : en.wikipedia.org

Functional programming : wiki.haskell.org

Functional programming is a style of programming which models computations as the evaluation of expressions. This article is meant to describe it briefly; however, the best way to understand functional programming is to learn the basics of one of the functional programming languages

Very quick TLDR : avoids shared state & mutable data, functions always returns a value & a function with given args will always returns the same value.

don't use assignment (Robert C. Martin)

No assignment means var are immutables.

a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions or declarations instead of statements.

N.B. : An expression is not a statement but a valid unit of code that resolves to a value. Not all statements resolve to a value.

N.B.2. :

  • An expression is a chunk of code that evaluates to a single value.
  • A statement is a chunk of code that may not evaluate to a value at all.

cf Nested Ternaries are Great - Eric Elliott - 20180126

Mostly adequate guide to FP (in JavaScript) gitbook

Learning Functional Programming with JavaScript - JSUnconf 2016 : Anjana Vakil - slides

Immutable data structures for functional JS : Anjana Vakil

Why Curry Helps - hughfdjackson.com (2015 ?)

Favoring Curry - Scott Sauyet - 20140618

An introduction to functional programming : Mary Rose Cook

Michael Feathers tweet - 20101103

OO makes code understandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts.

A Birds Eye View of Functional Programming TL;DR - medium.com - Brooklyn Zelenka - 20151030

Functional programming (FP) helps you write robust, powerful, and maintainable programs well suited to multicore and cloud computing by focusing on controlling state and effects.

Functional Programming Principles

Own Your Context with Explicit State Some people call it “stateless”, but I prefer “explicit state”. Instead of having data implicitly “somewhere over there” and reading that (implicit & shared) data while in the body of a function, simply pass all data that the function needs explicitly as arguments.

Limit Side Effects This is the output side of explicit state. As much as possible, avoid talking to the world outside of the function, except for the final return of the function.

Referential Transparency When you limit yourself to simple input/output for every function (with no changes to the world outside it), you can swap out a function for its return value.

Composition With (top-down) inheritance, you specialize a general class into more specific subsets. For example, a general Car class can be used to make more specific SportsCar and Minivan classes. This works well for a certain problems, but fails when you want to mix and match functionality. You can quickly end up with an endless regress of more and more general classes, and huge messaging interfaces between different parts of your application. With (bottom-up) function composition, we take small, single responsibility functions, and combine them with other functions to build up larger, more complex behaviours. We do this either by passing the result of one function as the argument of another, or by calling them in sequence (if we’re side-effecting).

Robert C Martin - Functional Programming; What? Why? When?

  • 4:07 "Functional Programming: What? When? Why?" or "The Failure of State"
  • 5:10 Rich Hickey is the author of Clojure. Listen to his talks.
  • 5:40 What is state? Variables.
  • 11:15 Structure and Interpretation of Computer Programs. This is a fascinating book. For the first 250 pages, the book uses no assignment statements.
  • 14:15 Here's how SICP's model of computing worked before they introduced an assignment statement. Simply replace a function call with its implementation.
  • 15:58 Once you introduce assignment. You can no longer replace a function call with its implementation. Why? Because the state of the system may have changed. An assignment statement introduces the concept of time.
  • 18:04 Side effect: an assignment statement. If there's no assignment, there's no side effect.
  • 20:22 What "hack" have we done to protect us from memory leaks? Garbage collection.
  • 31:46 Functional programming was invented in 1957 before OO and structured. But memory was too expensive to make it practical. But memory is cheap now.
  • 32:53 Should we change how we program? We should because:
    1. Functional programs are simpler - which makes them easier to write and maintain
    2. There's no temporal coupling - no worrying if some function was called before another function.
    3. Fewer concurrency issues. In a purely functional program, there's no concurrency because there is no state.
    4. No asking, "What's the state?"
  • 38:38 We're using multicore CPU's now because we can't increase clock rate anymore. And hardware makers are doing bizarre tradeoffs. They're making individual processors slower but putting more processors in. So individual cores slow down but the chip throughput goes up if you can take advantage of all the cores.
  • 42:00 How are you going to work with an abundance of cores? Maybe we need to walk away from the assignment statement.
  • 49:49 OO = procedure + state. OO is exposed procedure but hidden state (encapsulation). It's possible to write functional programs using an OO style. All of the objects become immutable.

# functionnal programming concepts

Why using _.chain is a mistake. - medium.com - Izaak Schroeder - 20160217

# partial application

Partial application means taking a function with n arguments and returning a new function with n-i arguments (given i<n), where the i arguments are bound to the new function.

const add = (a, b) => a + b;
const add5 = _.partial(add, 5); // now `a` is bound (locked-in) to 5
const add5 = (b) => 5 + b; // this is what it looks like inside

add5(3); // equivalent to invoking add(5, 3);
1
2
3
4
5

# currying

Function currying is the process of successive partial applications, until the last argument is given at which point the result of the function is returned.

const add = (a) => (b) => a + b;

add(1)(2); // Repeated partial application.
1
2
3

# Composition

A composed version of:

const add8 = (x) => add5(add3(x));
1

Could be written with more visual clarity using composition:

const add8 = compose(add5, add3);
1

Note : Since lodash@4 _.compose has been removed in flavor of _.flowRight.

# functionnal programming and DI

Functional approaches to dependency injection 20161205

TLDR : Build functions with partial application at the root level of your app (here is a concrete example)

Dependency Injection without classes - Fun Fun Function - www.youtube.com - 20170904

Advanced Dependency Injection without classes - Fun Fun Function - www.youtube.com - 20170911

Is Functional Programming a viable alternative to dependency injection patterns? : stackexchange.com 20150310

Functional Dependency Injection == Currying 201003xx

How to Trick OO Programmers into Loving Functional Programming 20130603

# object-oriented programming

Object-oriented programming

don't use pointers to functions (Robert C. Martin) (He is talking about pointers in C++)

based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. A feature of objects is that an object's procedures can access and often modify the data fields of the object with which they are associated (objects have a notion of "this" or "self"). In OOP, computer programs are designed by making them out of objects that interact with one another.

# imperative programming versus functionnal programming

Functional Programming vs. OOP [closed] : softwareengineering.stackexchange.com 20101005

TLDR : imperative versus functional === control flow versus data flow

For example, in imperative programming variables and loops are common when handling state, while in functional programming the state is handled via parameter passing, which avoids side-effects and assignments.

# object oriented programming versus functionnal programming

Functional programming vs Object Oriented programming [closed] : stackoverflow.com 20100116

  • When do you choose functional programming over object oriented?

TDLR : If logic evolves more then the model : choose FP and if model evolves more then the logic : choose OOP

OOP is good when you have a fixed set of operations on things. Your code evolves, you primarily add new things. Adding new classes which implement existing methods (operations).

FP is good when you have a fixed set of things Your code evolves, you primarily add new operations (functions) on existing things (data types).

When evolution goes the wrong way, you have problems (The Expression Problem : Philip Wadler 19981112):

Adding a new operation to an OOP program may require editing many class definitions to add a new method.

Adding a new kind of thing to a FP program may require editing many function definitions to add a new case.

  • What are the typical problem definitions where functional programming is a better choice?

Functional languages excel at manipulating symbolic data in tree form.