使用ES5,ES6和SAP ABAP实现非波拉契数列Fibonacci-阿里云开发者社区

开发者社区> 开发者小助手-bz4> 正文

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


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
阿里云服务器怎么设置密码?怎么停机?怎么重启服务器?
如果在创建实例时没有设置密码,或者密码丢失,您可以在控制台上重新设置实例的登录密码。本文仅描述如何在 ECS 管理控制台上修改实例登录密码。
8661 0
阿里云服务器ECS远程登录用户名密码查询方法
阿里云服务器ECS远程连接登录输入用户名和密码,阿里云没有默认密码,如果购买时没设置需要先重置实例密码,Windows用户名是administrator,Linux账号是root,阿小云来详细说下阿里云服务器远程登录连接用户名和密码查询方法
11024 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
10493 0
使用SSH远程登录阿里云ECS服务器
远程连接服务器以及配置环境
2298 0
使用OpenApi弹性释放和设置云服务器ECS释放
云服务器ECS的一个重要特性就是按需创建资源。您可以在业务高峰期按需弹性的自定义规则进行资源创建,在完成业务计算的时候释放资源。本篇将提供几个Tips帮助您更加容易和自动化的完成云服务器的释放和弹性设置。
11950 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
12305 0
腾讯云服务器 设置ngxin + fastdfs +tomcat 开机自启动
在tomcat中新建一个可以启动的 .sh 脚本文件 /usr/local/tomcat7/bin/ export JAVA_HOME=/usr/local/java/jdk7 export PATH=$JAVA_HOME/bin/:$PATH export CLASSPATH=.
4573 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,云吞铺子总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系统盘、创建快照、配置安全组等操作如何登录ECS云服务器控制台? 1、先登录到阿里云ECS服务器控制台 2、点击顶部的“控制台” 3、通过左侧栏,切换到“云服务器ECS”即可,如下图所示 通过ECS控制台的远程连接来登录到云服务器 阿里云ECS云服务器自带远程连接功能,使用该功能可以登录到云服务器,简单且方便,如下图:点击“远程连接”,第一次连接会自动生成6位数字密码,输入密码即可登录到云服务器上。
21743 0
2315
文章
0
问答
来源圈子
更多
+ 订阅
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载