97. Simulate functional programming in Lotus script, 97 Lotus script

Source: Internet
Author: User
Tags javascript array

97. Simulate functional programming in Lotus script, 97 Lotus script

This article describes 96. Techniques for simulating functional programming in Lotus script used by common field modifiers.

Functional Programming

Functional programming is a beautiful and powerful programming paradigm. It is derived from the Lambda algorithm proposed by Alonzo Church. A problem can be expressed as a Lambda algorithm, it is equivalent to this problem that can be effectively calculated in mathematics. Roughly speaking, a functional programming language corresponding to Lambda calculus can be used to write computing programs with any theoretical computing problems. Due to its close relationship with the theory of computing and powerful expressiveness, functional programming has always been very important in academia. However, in the software industry of daily application, its sound name and popularity are far less than imperative and object-oriented programming paradigm. However, in recent years, with the emergence of functional programming languages such as Erlang and Clojure, and the application of functional programming in mixed paradigm programming languages such as JavaScript, Scala, and Python, function programming is becoming more and more popular in the industry. Microsoft launched F # On the. Net platform, and Java also introduced Lambda expressions.
Compared with imperative programming familiar to General programmers, functional programming has the following main features:
-A function is a first-class object. This means that functions, like other common data types, can be assigned variables, passed as parameters to other functions, and return values of functions.
-Avoid function side effects as much as possible. That is, all external data used by a function operation is passed in as parameters, and the results are transmitted as return values without reading or modifying external public variables.
-More use of recursion and less use of loops.
-A large number of list calculations are often involved.
The following is a simple example of JavaScript that supports both imperative and functional programming to demonstrate the difference between the two. Converts each element in an array containing numbers to its square.
Imperative:

var list=[1, 2, 3, 4, 5];function square(source){    for (i=0; i<source.length; i++)    {        source[i]*=source[i]    }}square(list);

Function Type:

var list=[1, 2, 3, 4, 5];list=list.map(    function(elem){        return elem*elem;    });

People who are used to imperative programming may initially feel odd and uncomfortable with functional programming ideas and expressions, but once they are familiar with it, they will like it for convenience, simplicity, and strength. For example, in the preceding example, the map method of the JavaScript array is a common tool for listing objects in functional programming languages. The function is to return a new list, each element is the return value obtained by passing the corresponding element of the original list to the function in the parameter. This saves the imperative programming every time you need to process the cycle routines required by the elements in the list, you only need to give the core "business logic"-the square algorithm. In fact, even in traditional imperative programming languages, it is often necessary to pass a function. The core of event-based programming is to pass the event handler between the event publisher and the subscriber, or change the term callback function. C has function pointers, and the language on the. Net platform has proxies. The smallest unit of a Java program is a class. Therefore, to pass a function, it can only be packaged in one class.

Simulate functional programming in Lotus script

Functions in Lotus script cannot be passed independently. to simulate the function, we can only use it as a scripting language to explain and execute a string in a program. We have applied this technique when simulating events for custom objects. Here we use it to simulate functional programming.

'Operate on an element in an array. Used by ArrayMap.Private Function Operate(obj As Variant, op As String)    'Operates on an object    Execute_Access=obj    Dim pre As String, post As String, pos As Integer     If Not StrContains(op,"(") Then        op=op & "()"    End If    Dim pairs    pairs=Split("():(,:,):,,",":")    Dim pair As String     ForAll p In pairs        pair=CStr(p)        If StrContains(op,pair) Then            pos=InStr(op,pair)            pre=Left(op,pos)            post=Mid(op,pos+1)            Exit ForAll        End If          End ForAll    If pre><"" Then        Dim script As String        script={Execute_Access=} & pre & {Execute_Access} & post        'script="Execute_Access=StrRight(Execute_Access,"""")"        'Stop        Execute(script)        Operate=Execute_Access    End IfEnd Function

In the above function, we interpret the op passed as a parameter as a function and apply it to the obj parameter. The public variable Execute_Access of the script library where the Operate function is located is used to read and write values in the Execute function. To enable the op to include the parameters of the corresponding function, for example, the input {StrRight (, | "|)} string corresponds to the StrRight function and |" | parameter, operate may contain parentheses and commas. With the Operate function, we can simulate the above map method:

Function ArrayMap(arr As Variant, op As String) As Variant    'operates on each element in an array    If IsArray(arr) Then        Dim result() As Variant        Dim lb As Integer, ub As Integer        lb=LBound(arr)        ub=UBound(arr)        ReDim result(lb To ub)        Dim i As Integer        For i=lb To ub            result(i)=Operate(arr(i), op)        Next        ArrayMap=result         End IfEnd Function

In 96. at the end of the code given by the common field modifier, we can see that the ArrayMap function is frequently used. This style makes it clear the logic to process the array elements multiple times. If you use a traditional loop to write the code, it is not only long and difficult to understand and maintain the code.

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.