使用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.pngIn 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.pngimage.pngSuppose 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.pngThen 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.pngHow 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.pngnstead, 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.pngPay attention to the callstack change:image.pngOnce yield is executed, the current iteration result is returned from function generator to consumer:image.pngChange the consumer code a little bit to make result returned by yield more clearly understood:image.pngNow 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.pngFinal result:image.png

In ABAP

In this simple report ( written by my colleague Lin Xun ), two different calculation approaches are demonstrated:image.pngimage.pngimage.pngExecute 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.pngimage.pngConsumer code:image.pngFurther 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


相关文章
|
6月前
|
存储
使用 ABAP 代码打印出 SAP CRM 系统里所有维护了 Sales Area 的 business partner id
使用 ABAP 代码打印出 SAP CRM 系统里所有维护了 Sales Area 的 business partner id
|
6月前
|
存储 安全 数据库
什么是 SAP ABAP 数据库表的 Display Maintenance Allowed with Restrictions
什么是 SAP ABAP 数据库表的 Display Maintenance Allowed with Restrictions
|
6月前
|
安全 API 数据库
SAP ABAP OData 中 Function import 的概念介绍
SAP ABAP OData 中 Function import 的概念介绍
|
6月前
|
SQL 负载均衡 监控
SAP ABAP DBSQL_SQL_ERROR 错误
SAP ABAP DBSQL_SQL_ERROR 错误
|
6月前
|
前端开发 数据库 开发者
如何在 SEGW 事务码里为 SAP ABAP OData 服务实现 Function Import 试读版
如何在 SEGW 事务码里为 SAP ABAP OData 服务实现 Function Import 试读版
SAP ABAP OData 服务里需要指定 guid 类型的请求参数时,正确语法是什么?
SAP ABAP OData 服务里需要指定 guid 类型的请求参数时,正确语法是什么?
|
6月前
|
JSON 应用服务中间件 API
使用 ABAP 代码消费 SAP 系统的 OData 服务
使用 ABAP 代码消费 SAP 系统的 OData 服务
|
6月前
|
SQL 监控 Oracle
SAP ABAP 系统错误 Return value of the database layer SQL dbsl rc 99
SAP ABAP 系统错误 Return value of the database layer SQL dbsl rc 99
|
6月前
|
存储 前端开发 Linux
在 SAP ABAP 系统里访问 FTP 服务器
在 SAP ABAP 系统里访问 FTP 服务器
|
6月前
|
存储 前端开发 应用服务中间件
使用 SAP ABAP 执行 FTP 操作
使用 SAP ABAP 执行 FTP 操作

热门文章

最新文章