`
leonzhx
  • 浏览: 791656 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Chapter 5. Functional Programming

阅读更多

 

1.  Functions that operate on other functions are called higher-order functions. By manipulating functions, they can talk about actions on a new level.

 

2.  Functions have a method called apply, it takes two arguments. The second argument is an array containing the arguments to which a function must be applied.

 

3.  forEach function:

function forEach(array, action) {
  for (var i = 0; i < array.length; i++)
    action(array[i]);
}

See Array.prototype.forEach 

 

4.  reduce function combines an array into a single value by repeatedly using a function that combines an element of the array with a base value:

function reduce(combine, base, array) {
  forEach(array, function (element) {
    base = combine(base, element);
  });
  return base;
}

 See Array.prototype.reduce

 

5.  map function goes over an array, applying a function to every element, just like forEach. But instead of discarding the values returned by the function, it builds up a new array from these values:

function map(func, array) {
  var result = [];
  forEach(array, function (element) {
    result.push(func(element));
  });
  return result;
}

See Array.prototype.map 

 

6.  concat method on arrays creates a new array that concatenates the argument it is given with the array on which it is called. Calling concat is wasteful, because it creates a whole new array every time. 

 

7.  String.indexOf returns -1 when it doesn’t find its substring. The replace method of strings creates a new string in which all occurrences of the pattern in the first argument are replaced by the second argument.

 

8.  Creating new strings, especially big strings, is quite a bit of work for the computer. Remember that JavaScript string values never change. If you concatenate something to them, a new string is created, and the old ones stay intact. If we build up a big string by concatenating lots of little strings, new strings have to be created at every step, only to be thrown away when the next piece is concatenated to them. If, on the other hand, we store all the little strings in an array and then join them, only one big string has to be created.

 

9.  In HTML, anchors are created with the a tag, just like links. To make it an anchor instead of a link, the tag gets a name attribute, rather than of an href attribute.

 

10.  We may want something called partial application which take a function X and one or more arguments and then create a new function that calls X with both the original arguments and any newly passed ones:

function partial(func) {
  var knownArgs = arguments;
  return function() {
    var realArgs = [];
    for (var i = 1; i < knownArgs.length; i++)
      realArgs.push(knownArgs[i]);
    for (var i = 0; i < arguments.length; i++)
      realArgs.push(arguments[i]);
    return func.apply(null, realArgs);
  };
}

 

11.  The reason map takes its function argument before its array argument is that it is often useful to partially apply map by giving it a function. This “lifts” the function from operating on a single value to operating on an array of values:

map(partial(map, square), [[10], [0, 2], [3]]);

→ [[100], [0, 4], [9]]

 

12.The sum function can now simply be written like this: var sum = partial(reduce, op["+"], 0);

分享到:
评论

相关推荐

    Packt.Swift.Functional.Programming.2nd.Edition.2017

    Chapter 5. First-class functions Chapter 6. Higher-order functions Chapter 7. Function composition Chapter 8. Closures Chapter 9. Function currying Chapter 10. Recursion Chapter 11. Memoization ...

    Learning.Scala.Practical.Functional.Programming.for.the.JVM

    You don’t need to be a data scientist or distributed computing expert to appreciate this object-oriented functional programming language. This practical book provides a comprehensive yet ...

    Functional.Programming.in.JavaScript.1784398225

    The book then rounds off with an overview of methods to effectively use and mix functional programming with object-oriented programming. Table of Contents Chapter 1. The Powers of JavaScript's ...

    Reactive.Programming.with.Scala.and.Akka.1783984341.epub

    Chapter 5. Building Resilient Application with Akka Chapter 6. Akka Cluster Chapter 7. Finite State Machines with Akka Chapter 8. Akka Unit Testing Chapter 9. Distributed Systems and Key-Value Stores

    Clojure.High.Performance.JVM.Programming.epub

    Clojure is a general-purpose language from the Lisp family with an emphasis on functional programming. It has some interesting concepts and features such as immutability, gradual typing, thread-safe ...

    Swift.3.Object.Oriented.Programming.2nd.epub

    Chapter 5. Contract Programming with Protocols Chapter 6. Maximization of Code Reuse with Generic Code Chapter 7. Object-Oriented and Functional Programming Chapter 8. Extending and Building Object-...

    Learning.Reactive.Programming.With.Java.8

    Chapter 5. Combinators, Conditionals, and Error Handling Chapter 6. Using Concurrency and Parallelism with Schedulers Chapter 7. Testing Your RxJava Application Chapter 8. Resource Management and ...

    JavaScript Functional Programming for JavaScript Developers (PDF, EPUB, MOBI)

    Chapter 4: Implementing Functional Programming Techniques in JavaScript Chapter 5: Category Theory Chapter 6: Advanced Topics and Pitfalls in JavaScript Chapter 7: Functional and Object-oriented ...

    Learning Functional Data Structures and Algorithms

    Chapter 5. More List Algorithms Chapter 6. Graph Algorithms Chapter 7. Random Access Lists Chapter 8. Queues Chapter 9. Streams, Laziness, and Algorithms Chapter 10. Being Lazy - Queues and Deques ...

    Functional_Python_Programming

    techniques that characterize functional programming. We'll identify some of the ways to map these features to Python. Finally, we'll also address some ways in which the benefits of functional ...

    Pragmatic.Programming.Google.Glass.2nd.Edition

    Chapter 5. Tracking Movement and User Responses Chapter 6. Making Glass Social Chapter 7. Designing for Glass Chapter 8. Turning a Web App to Glassware Part II Glass Development Kit Chapter 9. ...

    [removed] Moving to ES2015 (AZW3格式)

    You'll get to grips with creational, structural, and behavioral patterns and get a deeper look at patterns used in functional programming, as well as model view patterns and patterns to build web ...

    Computer-Based.Problem.Solving.Process

    to the computer use for problem solving in any domain, including computer programming. Table of Contents Part 1 Systems Methodology Chapter 1. Introduction to System Software Chapter 2. Formal ...

    Packt.Reactive.Programming.in.Kotlin

    Chapter 1, A Short Introduction to Reactive Programming, helps you understand the context, thinking pattern, and principles of reactive programming. Chapter 2, Functional Programming with Kotlin and ...

    Modern.Python.Cookbook.epub

    The recipes will touch upon all the necessary Python concepts related to data structures, OOP, functional programming, as well as statistical programming. You will get acquainted with the nuances of ...

    Developing.a.Redux.Edge.B01KL5RJHU

    Having an understanding of ES6, functional programming, and React will certainly help too, but isn’t necessary. If you want to follow along with the examples you should also know your way around a ...

Global site tag (gtag.js) - Google Analytics