Skip to content

Instantly share code, notes, and snippets.

@jmlavoier
Last active November 28, 2019 16:16
Show Gist options
  • Save jmlavoier/d5b46eb2e83b95dc47f21de9970f886a to your computer and use it in GitHub Desktop.
Save jmlavoier/d5b46eb2e83b95dc47f21de9970f886a to your computer and use it in GitHub Desktop.
functional-programming-in-javascript

page 12

Listing 1.4

in the book

is missed the function

var csv = (student) {
  return `${student.ssn}, ${student.firstname}, ${student.lastname}`;
};

it suppose to

be function (student)

var csv = function (student) {
  return `${student.ssn}, ${student.firstname}, ${student.lastname}`;
};

page 14

Functional version

in the book

is calling run again

var plus2 = run(increment, increment);
print(run(0)); // -> 2

it suppose to

call the plus2(0)

var plus2 = run(increment, increment);
print(plus2(0)); // -> 2

page 19

Listing 1.5

in the book

the _.pluck and _.average are deprecated

_.chain(enrollment)
  .filter(student => student.enrolled > 1)
  .pluck('grade')
  .average()
  .value(); //-> 90

it suppose to

use meanBy

_.chain(enrollment)
  .filter(student => student.enrolled > 1)
  .meanBy(student => student.grade)

page 25

Object-oriented JavaScript quote

in the book

is written oriented-oriented*

When I define a relationship between one object and another by saying it’s a subtype or derived type, I’m referring to the prototypal relationship that exists between the objects. It’s important to clarify that although JavaScript is oriented-oriented, it doesn’t have classical inheritance as you may have seen in other languages like Java.

it suppose to

be object-oriented

When I define a relationship between one object and another by saying it’s a subtype or derived type, I’m referring to the prototypal relationship that exists between the objects. It’s important to clarify that although JavaScript is object-oriented, it doesn’t have classical inheritance as you may have seen in other languages like Java.

page 27

Code example about instantiating Student

in the book

is written p.fullname

var person = new Student('Alonzo', 'Church', '444-44-4444', 'Princeton');
p.fullname; // -> Alonzo Church

it suppose to

be person.fullname

var person = new Student('Alonzo', 'Church', '444-44-4444', 'Princeton');
person.fullname; // -> Alonzo Church

page 27

Code example about instantiating Student

in the book

is written (such as Student from Parent)

Figure 2.2 The focus of OOP is to create inheritance hierarchies (such as Student from Parent) with methods and data tightly bound together. Functional programming, on the other hand, favors general polymorphic functions that crosscut different data types and avoid the use of this.

it suppose to

be (such as Student from Person)

Figure 2.2 The focus of OOP is to create inheritance hierarchies (such as Student from Person) with methods and data tightly bound together. Functional programming, on the other hand, favors general polymorphic functions that crosscut different data types and avoid the use of this.

page 34

It said about using const but didn't use it

in the book

is declared _lat and _long using let

In conjunction with const, you can create objects with semantics similar to those of a string or number. Let’s consider another example:

function coordinate(lat, long) {
   let _lat = lat;
   let _long = long;
   return {
      latitude: function () {
         return _lat;
      },
      longitude: function () {
         return _long;
      },
      translate: function (dx, dy) {
         return coordinate(_lat + dx, _long + dy);
      },
      toString: function () {
         return '(' + _lat + ',' + _long + ')';
      }
  };
}

it suppose to

be declared _lat and _long using const

In conjunction with const, you can create objects with semantics similar to those of a string or number. Let’s consider another example:

function coordinate(lat, long) {
   const _lat = lat;
   const _long = long;
   return {
      latitude: function () {
         return _lat;
      },
      longitude: function () {
         return _long;
      },
      translate: function (dx, dy) {
         return coordinate(_lat + dx, _long + dy);
      },
      toString: function () {
         return '(' + _lat + ',' + _long + ')';
      }
  };
}

page 34

Second code example

in the book

is calling fistname method, but it wasn't created before

var person = Object.freeze(new Person('Haskell', 'Curry', '444-44-4444')); 
person.firstname = 'Bob';

it suppose to

call _firstname attribute

var person = Object.freeze(new Person('Haskell', 'Curry', '444-44-4444')); 
person._firstname = 'Bob';

page 37

Second code example

in the book

is passing 'lastName' to R.lenseProp

var person = new Person('Alonzo', 'Church', '444-44-4444');
var lastnameLens = R.lenseProp('lastName');

it suppose to

pass 'lastname' to R.lensProp

var person = new Person('Alonzo', 'Church', '444-44-4444');
var lastnameLens = R.lensProp('lastname');

page 38

Third code example

in the book

is inverted the order of R.set params

var newPerson = R.set(zipLens, person, zipCode('90210', '5678'));

it suppose to

be zipCode first than person

var newPerson = R.set(zipLens, zipCode('90210', '5678'), person);

page 64

Listing 3.1

in the book

the code is incrementing idx before the first iteration, it's resulting in an array with the fist element empty

function map(arr, fn) {
  let idx    = 0,
      len    = arr.length, 
      result = new Array(len);

   while (++idx < len) {
       result[index] = fn(array[idx], idx, arr);
   }
   return result;
}

it suppose to

increment idx only after result assignment

function map(arr, fn) {
  let idx    = 0,
      len    = arr.length, 
      result = new Array(len);

   while (idx < len) {
       result[index] = fn(array[idx], idx, arr);
       idx++; 
   }
   return result;
}

page 65

Listing 3.2

in the book

the reduce function isn't working without initial accumulator, it's destructuring a accumulator and doesn't have any default value.

function reduce(arr, fn,[accumulator]) { 
  let idx = -1,
      len = arr.length;
  if (!accumulator && len > 0) {
    accumulator = arr[++idx];
  }
  while (++idx < len) {
    accumulator = fn(accumulator,
    arr[idx], idx, arr);
  }
  return accumulator;
}

it suppose to

have a default initial accumulator to not throw error

function reduce(arr, fn,[accumulator] = []) { 
  let idx = -1,
      len = arr.length;
  if (!accumulator && len > 0) {
    accumulator = arr[++idx];
  }
  while (++idx < len) {
    accumulator = fn(accumulator,
    arr[idx], idx, arr);
  }
  return accumulator;
}

page 67

Last code example

in the book

the code is missing the lodash _

([1, 3, 4, 5]).reduce(_.divide) !== ([1, 3, 4, 5]).reduceRight(_.divide) 

it suppose to

be

_([1, 3, 4, 5]).reduce(_.divide) !== _([1, 3, 4, 5]).reduceRight(_.divide) 

page 68

Fisrt code example

in the book

is calling validate instead of notAllValid and the results commented are the inverse

const isNotValid = val => _.isUndefined(val) || _.isNull(val); 
const notAllValid = args => (_(args).some(isNotValid));

validate (['string', 0, null, undefined]) //-> false 
validate (['string', 0, {}]) //-> true 

it suppose to

be

const isNotValid = val => _.isUndefined(val) || _.isNull(val); 
const notAllValid = args => (_(args).some(isNotValid));

notAllValid(['string', 0, null, undefined]) //-> true 
notAllValid(['string', 0, {}]) //-> false 

page 69

Listing 3.5

in the book

is passing this to predicate

function filter(arr, predicate) { let idx = -1,
  len = arr.length, 
        result = [];

  while (++idx < len) {
    let value = arr[idx];
    if (predicate(value, idx, this)) {
      result.push(value); 
    }
  }
  return result;
}

it suppose to

be arr

function filter(arr, predicate) { let idx = -1,
  len = arr.length, 
        result = [];

  while (++idx < len) {
    let value = arr[idx];
    if (predicate(value, idx, arr)) {
      result.push(value); 
    }
  }
  return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment