每周分享:函数式编程之递归模式

函数式编程(FP)就是利用纯函数来构建软件的过程,避免共享状态,可变数组以及一些副作用。而 ES6 带来了很多的特性帮助我们更方便地进行 JavaScript 函数式编程。

今天我们就来看看 ES6 的剩余参数语法结合递归实现如何实现函数式编程,用函数式思想实现一些我们常用的方法。

返回数组的第一个元素

const head = ([x]) => x

使用

const arr = [1, 2, 3]
head(arr) // 1

Tail

返回数组中除第一个元素外的其它元素

const tail = ([, ...xs]) => xs

上面写法中省略了第一个参数,所以也等价于

const tail = ([x, ...xs]) => xs

使用

const arr = [1, 2, 3]
tail(arr) // [2, 3]

Def

如果存在参数则返回 true,否则返回 false

const def = x => typeof x !== "undefined"

使用

const name = "tom"
def(name) // true
def(age) // false

Copy

复制数组

const copy = arr => [...arr]

使用

let arr = [1, 2, 3]
let copied = copy(arr)
copied.push(4)
arr // [1, 2, 3]
copied // [1, 2, 3, 4]

Reverse

反转数组

const reverse = ([x, ...xs]) => def(x) ? [...reverse(xs), x] : []

这里我们通过递归依次将整个数组给调换顺序了,使用

const arr = [1, 2, 3]
reverse(arr) // [3, 2, 1]

First

返回一个新的数组,包含给定数组前 n 项的元素

const first = ([x, ...xs], n = 1) => def(x) && n ? [x, ...first(xs, n - 1)] : []

同样运用了递归的思想,使用

const arr = [1, 2, 3]
first(arr, 2) // [1, 2]

计算过程如下

  • 首先 first(arr, 2) = first([1, 2, 3], 2)
  • def(x) = def(1) && n = 2 => true
  • 返回 [x, ...first(xs, n - 1)] = [1, ...first([2, 3], 1)]
  • 计算 first([2, 3], 1)
  • def(x) = def(2) && n = 1 => true
  • 返回 [x, ...first(xs, n - 1)] = [2, ...first([3], 0)]
  • 计算 first([3], 0)
  • def(x) = def(3) && n = 0 => false
  • 于是 first([3], 0) 返回 []
  • 所以结果为 [1, ...[2, ...[]]] => [1, 2]

Last

返回一个新的数组,包含给定数组后 n 项的元素

const last = (xs, n = 1) => reverse(first(reverse(xs), n))

结合上面的 first 方法和反转,使用

const arr = [1, 2, 3]
last(arr, 2) // [2, 3]

本期的主要目的不是为了让大家在实际开发中就这样去上面的方法,实际上数组也提供了相应的便捷方法,之所以讲这些,是为了让大家更加深刻理解函数式编程的纯函数特性。

最新评论

  1. One more issue is that video gaming has become one of the all-time main forms of excitement for people of various age groups. Kids participate in video games, and adults do, too. The particular XBox 360 is among the favorite games systems for folks who love to have a lot of video games available to them, along with who like to experiment with live with people all over the world. Many thanks for sharing your opinions. Nicky Saines

  2. Hi Diane, PAWS stands for Post Acute Withdrawal Syndrome, or sometimes called Protracted Withdrawal Syndrome. Put simply it is a range of symptoms including but not limited to, insomnia, anxiety, fatigue depression etc. These symptoms can start anywhere from 1-2 weeks after the initial acute withdrawal, and last months to years after that. It is a huge roadblock to lasting recovery. I pulled the paragraph below from an article in Psychology Today to help put some light on the subject. Elden Jeppesen

  3. Very insightful. Allows an individual to take a step back and realize that we all have flaws that we are quick to judge others on. We are all human beings with many flaws that just come to us naturally. We are an imperfect race, that is quick to point out the flaws in others in order to makes ourselves feel better our own flaws. People should remember to take a third-party point of view on situations, and not only see what they want to see, but the entire situation as a whole. Brian Androsky

  4. I have observed that online diploma is getting well-known because attaining your college degree online has changed into a popular selection for many people. Numerous people have never had a possibility to attend an established college or university but seek the increased earning potential and a better job that a Bachelors Degree grants. Still other folks might have a qualification in one training but want to pursue anything they already have an interest in. Andres Hallan

  5. Have you ever thought about writing an e-book or guest authoring on other sites? I have a blog centered on the same topics you discuss and would really like to have you share some stories/information. I know my viewers would value your work. If you are even remotely interested, feel free to send me an e mail. Dean Chenaille

发表评论

邮箱地址不会被公开。 必填项已用*标注