Recursive/trailing recursion (+destructuring assignment) Implementation of reversal sequences (JavaScript + ES6)

Source: Internet
Author: User

Here is the recursive/trailing recursive implementation of the reverse sequence (array/string) made with JavaScript. In addition, the early adopters used a ES6 destructuring assignment + spread operator to make a more functional version (only supported arrays).

Correctness can pass the test (see the demo on my Github and write a small test frame), but efficiency is mark-especially with the ES6 feature version. This is mainly written to play the function of JS features.

1. Recursive implementation of reversal sequences

First use Haskell to do the draft (because it is more natural), the array version length

[a] [a] [] [] [ x]       

In JavaScript, arrays and strings all have the slice sum concat , so you can write a version of two "sequences" (often referred to as sequence in fields such as discrete mathematics).

Note:

    • Array.prototype.slice()
    • Array.prototype.concat()
    • String.prototype.slice()
    • String.prototype.concat()

All appear in ECMAScript 3, implemented in JavaScript 1.2

The corresponding JS implementation is as follows, set the base case to a sequence length of less than 1, this reduces the one-step recursion. In comparison, the Haskell version is a bit more at a glance ...

function recursivereverse (seq) {    return seq.length > 1?         Recursivereverse (Seq.slice (1)). Concat (Seq.slice (0, 1)): seq;}

After testing, both arrays and strings can pass.

2. Tail recursive implementation

Haskell version Long this way, use one to ret pass the completed part:

[a] [a] [][] ret = ret = Rev xs (x:ret)                 

The corresponding JS version, which is used slice(0, 0) to avoid type checking to create an empty sequence.

function tailreverse (seq) {    return (function  rev (seq, ret) {        return Seq.length > 0?             Rev (Seq.slice (1), Seq.slice (0, 1). Concat (ret)): ret;    }) (Seq, Seq.slice (0, 0));}

After testing, both arrays and strings can pass.

3. Using a recursive version of the new ES6 feature

ES6 introduces destructuring assignment and spread operator, which allows partial use of pattern matching in functional programming (pattern matching). But I haven't found anything in ES6 's new feature that allows JS's function to be fused into the overloaded (overloading) combination (destructuring Assignment) at the syntactic level , like Haskell. itself is a subset of pattern matching), to overload or to use the current JS in the most common solution--if-else or three mesh operator with type check. Here, because of the simple usage, the direct trinocular operator is OK.

The simple recursive version of the new features implemented with ES6 is as follows, because the JS array and string do not have a common function similar append (add elements back and return the new array) function, lazy toss type check, so here only implemented the array version (due to destructuring assignment + The combination of the spread operator [x, ...xs] splits the string 第一个字符,[其他字符组成的数组] , so passing in the string directly to the following function and returning the value back to the join string can also achieve the same effect.

function Pmreverse ([x, ... xs]) {    returntypeof x = = = "undefined"?  []: Pmreverse (XS). Concat ([x]);}

After testing, the array can be reversed normally (only if the browser opens the ES6 feature and the page runs out of this version of the test, otherwise there is only one alert).

4. Tail-recursive version with ES6 new features
function Pmtailreverse (list) {    return (function  rev ([x, ... xs], ret) {        return  typeof x = = = "undefined"? Ret:rev (XS, [X].concat (ret));    }) (list, []);}

After testing, the array can be reversed normally (only if the browser opens the ES6 feature and the page runs out of this version of the test, otherwise there is only one alert).

About the test page

To facilitate the writing of a small test framework, see source code.

Because the browser does not support ES6 in parsing the ES6 features of the code will be a syntax error, with Try-catch can not grasp, here used window.onerror to catch and hint. I do not know why my Chrome opened the experimental JS still can't open ES6 support ... FireFox is available by default.

Recursive/trailing recursion (+destructuring assignment) Implementation of reversal sequences (JavaScript + ES6)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.