使用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


相关文章
|
1月前
|
开发者 供应链 BI
SAP ABAP CALL SUBSCREEN 代码解析
SAP ABAP CALL SUBSCREEN 代码解析
58 0
|
1月前
|
SQL Android开发
创建 SAP ABAP CDS View 保存失败 - Dependencies DDL source - View Entity not written
创建 SAP ABAP CDS View 保存失败 - Dependencies DDL source - View Entity not written
9 0
创建 SAP ABAP CDS View 保存失败 - Dependencies DDL source - View Entity not written
|
1月前
|
SQL 数据库 索引
关于 SAP ABAP REPOSRC 数据库表在 HANA 中的 DDL Definition
关于 SAP ABAP REPOSRC 数据库表在 HANA 中的 DDL Definition
20 1
关于 SAP ABAP REPOSRC 数据库表在 HANA 中的 DDL Definition
|
2月前
|
安全 开发者
什么是 SAP ABAP 调试器里的 TRFC Block Sending 设置
什么是 SAP ABAP 调试器里的 TRFC Block Sending 设置
23 0
|
1月前
|
存储
使用 ABAP 代码打印出 SAP CRM 系统里所有维护了 Sales Area 的 business partner id
使用 ABAP 代码打印出 SAP CRM 系统里所有维护了 Sales Area 的 business partner id
21 0
|
1月前
关于 SAP ABAP OData 服务如何实现 Deep Insert 场景 - SAP 应用的标准行为试读版
关于 SAP ABAP OData 服务如何实现 Deep Insert 场景 - SAP 应用的标准行为试读版
16 1
|
1月前
|
人工智能
Suno AI 生成 SAP ABAP 顾问之歌
Suno AI 生成 SAP ABAP 顾问之歌
22 1
|
1月前
|
存储 安全 数据库
SAP ABAP 中数据类型 xstring 的使用介绍
SAP ABAP 中数据类型 xstring 的使用介绍
31 0
|
1月前
什么是 SAP ABAP 里的 Subscreen
什么是 SAP ABAP 里的 Subscreen
16 1
什么是 SAP ABAP 里的 Subscreen
|
1月前
SAP ABAP 里 CALL SUBSCREEN 语句的使用介绍
SAP ABAP 里 CALL SUBSCREEN 语句的使用介绍
13 1
SAP ABAP 里 CALL SUBSCREEN 语句的使用介绍

热门文章

最新文章