JavaScript Functional Programming:声明式与命令式

函数式编程属于声明式编程(declarative programming)的范畴,经常跟声明式编程一块儿讨论的是命令式编程(imperative programming),因为它们是两种不太一样的风格。

命令式编程一般就是说清楚具体要怎么样得到一个结果:先这样做,再这样做,然后再这样,如果这样,就这样做 … 声明式编程就是声明(说明)一下你想得到的结果是什么样的:把这组电影里的平均分大于 9 分的电影过滤出来给我。

比如有一组电影,你想过滤出评分 9 分以上的电影。
let movies = [
  { title: 'The Shawshank Redemption', rating: 9.6 },
  { title: 'Forrest Gump', rating: 9.4 },
  { title: 'Roman Holiday', rating: 8.9 }
]
命令式:
const imperativeMovieFilter = (movies) => {
  let result = []
  for (let i = 0; i < movies.length; i++) { 
    if (movies[i].rating >= 9) {
      result.push(movies[i])
    }
  }
  return result
}

在函数里面,先来个 result,让它等于一个空白的数组。然后我们又 for 循环,去处理 movies ,循环的时候判断当前项目里的评分(rating)是不是大于等于 9,如果是的话,就把这个项目放到 result 里面。循环完成以后,会返回处理之后的结果。命令式编程里,详细的说明了得到结果需要做的每个操作。

声明式:
const declarativeMovieFilter = (movies) => {
  return movies.filter((movie) => movie.rating >= 9)
}
在上面这个函数里,我们只是说明了一下,自己想要的结果是什么样的,就是评分在 9 分以上的电影。

————————————————
版权声明:本文为CSDN博主「longzhoufeng」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/longzhoufeng/article/details/78802836

转载于:https://www.cnblogs.com/xiaomili/p/11376679.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Title: Functional Programming in JavaScript Author: Dan Mantyla Length: 152 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2015-03-26 ISBN-10: 1784398225 ISBN-13: 9781784398224 Unlock the powers of functional programming hidden within JavaScript to build smarter, cleaner, and more reliable web apps About This Book Discover what functional programming is, why it's effective, and how it's used in JavaScript Understand and optimize JavaScript's hidden potential as a true functional language Explore the best coding practices for real-world applications Who This Book Is For If you are a JavaScript developer interested in learning functional programming, looking for the quantum leap towards mastering the JavaScript language, or just want to become a better programmer in general, then this book is ideal for you. It is aimed at programmers involved in developing reactive frontend apps, server-side apps that wrangle with reliability and concurrency, and everything in between. In Detail This is a fast-paced guide that will help you to write real-world applications by utilizing a wide range of functional techniques and styles. The book first explores the core concepts of functional programming common to all functional languages, with examples of their use in JavaScript. It's followed by a comprehensive roundup of functional programming libraries for JavaScript that minimizes the burden of digging deep into JavaScript to expose a set of tools that makes functional programming not just possible but highly convenient. 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 Functional Side – a Demonstration Chapter 2. Fundamentals of Functional Programming Chapter 3. Setting Up the Functional Programming Environment 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 Programming in JavaScript

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值