使用ES5,ES6和SAP ABAP实现非波拉契数列Fibonacci

简介: 使用ES5,ES6和SAP ABAP实现非波拉契数列Fibonacci

The concept of Fibonacci Sequence or Fibonacci Number is widely used in many programming books.

It could be defined via the formula: image.png

In ES5

In ES6 there is a feature so called generatorFunction which can achieve the calculation of Fibonacci Sequence in a very convenient way. But before we really enjoy the built-in language feature, let’s first see how to simulate it in ES5.


Here I use the closure in JavaScript to store the current round of calculation and return a new function for next round trigger.

image.png

image.png

Suppose I would like to get a series of result and I am too lazy to call next again and again, then I write a tool function to ease my life:

image.png

Then I can get 10 rounds of calculation result via a single shot:

console.log(take(10, fib(1,1)));

Result in console:

image.png

In ES6

Now we have generatorFunction which makes life pretty easier.

The complete source code:

image.png

How does this native function generator work

In line 40~47 we get a function generator with ES6 grammar function *().


Within the body we declare an endless loop to calculate Fibonacci sequence.


In line 49 we call this generator via () and store its result via variable fib. Here the code in line 41~45 is never executed so far.


image.png


Instead, the variable fib just holds a ITERATOR reference to function generator fib_generator.


This ITERATOR has a built-in method next supported by ES6. When this next method is called ONCE, the body in fib_generator is then executed ONCE as well.


Now let’s step into next call for the first time:

image.png

Pay attention to the callstack change:

image.png

Once yield is executed, the current iteration result is returned from function generator to consumer:

image.png

Change the consumer code a little bit to make result returned by yield more clearly understood:

image.png

Now it is clear that yield keyword returns an object with one attribute which stores current calculated result, and a boolean done flag to indicate whether the function generator has ended. In my case it is always false which makes sense since I declare a while(true) inside it.

image.png

Final result:

image.png

In ABAP

In this simple report ( written by my colleague Lin Xun ), two different calculation approaches are demonstrated

image.png

image.png

image.png

Execute report you can find out that the second approach to calculate using ABAP internal table is greatly faster than the first solution.


Christian Drumm has also provided another approach in his blog Functional ABAP – Functional Programming in ABAP ?!


image.png

The approach to simulate next call is also very simple:

image.png

Consumer code:

image.png

Output:

image.png

Further reading

I have written a series of blogs which compare the language feature among ABAP, JavaScript and Java. You can find a list of them below:


Lazy Loading, Singleton and Bridge design pattern in JavaScript and in ABAP

Functional programming – Simulate Curry in ABAP

Functional Programming – Try Reduce in JavaScript and in ABAP

Simulate Mockito in ABAP

A simulation of Java Spring dependency injection annotation @Inject in ABAP

Singleton bypass – ABAP and Java

Weak reference in ABAP and Java

Java byte code and ABAP Load


相关文章
|
3月前
|
Java C++ Python
Rust每日一练(Leetday0013) 解数独、外观数列、组合总和
Rust每日一练(Leetday0013) 解数独、外观数列、组合总和
32 0
Rust每日一练(Leetday0013) 解数独、外观数列、组合总和
|
7月前
P4057 [Code+#1]晨跑(数学分析,辗转相除法模板(欧几里得算法))
P4057 [Code+#1]晨跑(数学分析,辗转相除法模板(欧几里得算法))
23 0
LeetCode contest 182 5368. 找出数组中的幸运数
LeetCode contest 182 5368. 找出数组中的幸运数
|
Java Shell
Codeforces Round #746 (Div. 2) D - Hemose in ICPC ?(交互 二分 欧拉序)
Codeforces Round #746 (Div. 2) D - Hemose in ICPC ?(交互 二分 欧拉序)
135 0
|
JavaScript 前端开发 算法
Golang语言[6] 递增的三元子序列/笨阶乘/矩阵查找/直方图的水量 |Go主题月(上)
给你一个整数数组 nums ,判断这个数组中是否存在长度为 3 的递增子序列。 如果存在这样的三元组下标 (i, j, k) 且满足 i < j < k ,使得 nums[i] < nums[j] < nums[k] ,返回 true ;否则,返回 false 。
89 0
|
JavaScript 前端开发 Go
Golang语言[6] 递增的三元子序列/笨阶乘/矩阵查找/直方图的水量 |Go主题月(中)
给你一个整数数组 nums ,判断这个数组中是否存在长度为 3 的递增子序列。 如果存在这样的三元组下标 (i, j, k) 且满足 i < j < k ,使得 nums[i] < nums[j] < nums[k] ,返回 true ;否则,返回 false 。
130 0
|
JavaScript 前端开发 Java
使用ES5,ES6和SAP ABAP实现非波拉契数列Fibonacci
使用ES5,ES6和SAP ABAP实现非波拉契数列Fibonacci
101 0
使用ES5,ES6和SAP ABAP实现非波拉契数列Fibonacci
|
存储 JSON JavaScript
使用JavaScript ES6的新特性计算Fibonacci(非波拉契数列)
使用JavaScript ES6的新特性计算Fibonacci(非波拉契数列)
116 0
使用JavaScript ES6的新特性计算Fibonacci(非波拉契数列)
最简单的实现,用ABAP创建非波拉契数列
最简单的实现,用ABAP创建非波拉契数列
141 0

热门文章

最新文章