Web Developer| Biochemist|Language Lerner|Music

Web Developer| Biochemist|Language Lerner|Music

What I learned today | Functional Javascript | Chapter 2 | First-Class Functions and Applicative Programming

What I learned today | Functional Javascript | Chapter 2 | First-Class Functions and Applicative Programming

Thanks for sharing!

Hey!! you missed me ? no..okay ..

so chapter 2

First Class Functions

According to the author, I quote, “the term first class means that soething is just a value. Is one that can go anywhere that any other value can go”. Therefore a first class function is as shown below:

  1. var number = function(){return 50;} /*we can store a number in a variable, similarly we can store a function.
  2. var number = [2, function(){return 50;}] /*a number can be stored in an array, so can a function */
  3. var numbers = {numberOne:1, numberFifty:function(){return 50;}} /*a number can be stored in an object, so can a function */
  4. function add(a,b){return a + b};
    1. add(2, function(){return 40;}) /*a function can be passed to a function as an argument*/

There are more ways by which functions can be used, but due to time and energy are not listed above. Please do find out more in the Book .

Another term addressed is the “higher-order function”. I encountered this term while learning react.js, but never fully understood the concept behind it. I even used it in my first (currently latest) full-stack javascript project . “A higher order function can accept a function as an argument and it can also return a function”. We observe this pattern while using familar Array methods like map.

the map implementation takes in the array as the first argument and a function as the second argument. From my understanding of the higher order function term, I would categorize the map method under higher order function.

Paradigms of JavaScript

Now let us treat the other “paradigms” of javaScript. I guess that means ways or different approaches to coding in javascript ?. Note functional programming is also included.

  • Imperative programming (IP): The IP approach is what I feel we beginners are most acquainted with.  Basically a direct and straigtforward approach. Due to this “straightforwardness” it is difficult to reuse.
  • The Prototype based Object oriented programming: In Java, when I remember correctly, one creates classes as a blueprint for objects. On the other hand in Javascript, existing objects are ued as basis for objects with specialization.
  • Metaprogramming:  This is the first time I am seeing such a word, honestly, truly. According to the autor metaprogramming occurs when you write code that changes the way something is interpreted. Me: “still don’t get it!”

Some of the methods / functions that are used in functional programming are as follows:

  1. reduce
    1. “reduces” a collection of values to a single value
  2. map
    1. calls a function (provided as the second argument) on every value in a collection-; returns the result
  3. filter
    1. calls a predicate function ( a function that returns a boolean) on each value in a given collection. if the function called on the value returns true, then the filter returns the value to the collection.
  4. reduceRight
    1. similar to reduce, but accumlates the value from right to left.
  5. find
    1. calls a predicate function on each value in a predicate, returns the first value on the the called function returned true
  6. reject
    1. opposite of filter. it excludes values for which the predicate called on that value returned true
  7. all
    1. calls a predicate on each value in a collection, returns true only if all returned boolean values of the predicate call on each value is true. (*maybe I should have just quoted the author, sorray*)
  8. any
    1. returns true if at least one of the values pass the predicate call (returns true)

quite a number of functions were addressed in the book, so do check it out!
How to define such functions (applicative functions)
“define a function that takes a function and then calls it” – author

Before I never really understood the need for “apply”, the method of the function prototype (basically you can access this method from any function you create). But now I grasp atleast 40%, progress.  With apply, you can call a function in context of an object. This is useful when the function has to access values in this (e.g. this.color, this.number etc).  However if this is not used in your function, you can still use it while passing null as the first argument of the method. If I am wrong, do correct me via the comment section.

What functions are not applicative:

  1. a function that takes some number of arguments and performs an action
  2. a function that does not expect another function as an argument

Yes I know, rather complicated. I am finding it very hard to comprehend this concept and it was just addressed in a  single page. Let us just use the example provided in the book and break it down step by step, shall we?

function cat(){
var head = _.first(arguments)
if(existy(head)){return head.concat.apply(head, _.rest(arguments))}
else{return []}
}

The function is to take a number arguments and concatenate them

Okay breakdown, starting from line 1 (*breathes in and out slowly*)

  1. creates a function called cat
  2. declares a variable head and instantiates it to the first argument in the collection of arguments. Note that _ is the underscore library.
  3.  checks if first argument exist using the existy function (remember? undefined and null).
    1. returns the concatenated arguments
      1. head.concat : since head is supposedly an array, it should have the concat method from the Array.prototype object
      2. head.concat.apply: remember, apply is a method accessible to all functions via the Function.prototype object. According to this article, the this object passed as the first argument of the apply method should inherit the concat method.
      3. head.concat.apply(head,…) : I suppose the head (array) object inherits the concat method. Truly I really do not understand this part.
      4. head.concat.apply(head, _.rest(arguments)): Let us assume that my statement above (assumption) was right on track, then I can safely assume that the action actually looks like this head.concat(_.rest(arguments)). One last thing rest returns an array of the elements after the first element in a collection. So basically ( _.first(array)).concat(_.rest(array)) = array (sorry for wasting your time)
    2. else if head does not exist, which means there were no arguments, then an empty array is returned. Wheew!DONE!

Current Status: I STILL DON’T UNDERSTAND!!

See the Pen Functional JS: Functions in Underscore by Ajala Comfort (@AJALACOMFORT) on CodePen.

 

Today’s date: 30-08-2017

I need a day or two to rest my brain.



shares