97. Simulating functional programming in LotusScript

Source: Internet
Author: User

This article describes the 96. Common field modifiers are used to simulate functional programming techniques in lotusscript.

Function-Type programming

Functional programming is a graceful and powerful programming paradigm. It originates from the λ calculus (lambda calculus) proposed by Alonzo Church, and a problem can be expressed as a lambda calculus, which, according to the church–turing argument, is equivalent to the problem mathematically calculated effectively. Roughly speaking, the computational program of any theoretically computable problem can be written in a functional programming language corresponding to the lambda calculus. Because of its close relationship with computable theory and strong expressive force, functional programming has been paid much attention in academic circles. But in the daily application of the software industry, its reputation and popularity is far less than the 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 JavaScript, Scala and Python, functional programming has become more and more popular and popularized in the industry. Microsoft in the. NET platform introduced F#,java also introduced lambda expression.
Functional programming has several key features compared to the command-style programming familiar to programmers:
-The function is a level (first-class) object. This means that the function, like the values of other ordinary data types, can be given a variable and passed as a parameter to other functions as the return value of the function.
-Avoid side effects of functions as much as possible. That is, all external data used by a function operation is passed in as a parameter, and the result is outgoing from the return value without reading and modifying the external public variables.
-use recursion more, less loops.
-often involves large lists (list) calculations.
Here is a simple example of JavaScript that can be both imperative and functional programming to illustrate the difference. Turns each element in an array containing numbers into its square.
Command type:

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

Function Type:

varlist=[12345];list=list.map(    function(elem){        return elem*elem;    });

People who are accustomed to imperative programming may initially feel somewhat eccentric and uncomfortable with the idea and expression of functional programming, but once familiar, they will enjoy the convenience and simplicity and power of it. For example, the map method of a JavaScript array is a common tool for a functional programming language list object, and the function is to return a new list where each element is the return value of the function that passed the corresponding element of the original list into the parameter. This eliminates the need to process the list of elements in the imperative programming of the loop routines, just give the core "business logic"-the algorithm of square. In fact, even in traditional command-type programming languages, there is often a need to pass a function. The core of event programming is to pass event handlers between event publishers and Subscribers, or to change the term callback function. C has a function pointer,. NET platform is a proxy, the smallest element of a Java program is a class, so in order to pass a function it can only be wrapped in a class.

Simulation of functional programming in LotusScript

The functions in LotusScript cannot be passed independently, in order to simulate, we can only use as a scripting language it can be in the program to interpret a string of the ability to execute. We have applied this technique when simulating events for custom objects. Here we use it to simulate functional programming.

' Operate on a element in an array. Used by Arraymap.Private FunctionOperate (obj as Variant, op asString)' operates on a objectExecute_access=objDimPre ASString, Post asString, POS as IntegerIf  notStrcontains (OP,"(") ThenOp=op &"()"    End If    DimPairs pairs=Split("():(,:,):,,",":")DimPair asStringForAll PinchPairs pair=CStr(p)IfStrcontains (Op,pair) Thenpos=InStr(Op,pair) pre= Left(Op,pos) post=Mid(op,pos+1)ExitForAllEnd If          EndForAllIfpre><""  Then        DimScript asStringscript={execute_access=} & Pre & {execute_access} & Post' script= ' Execute_access=strright (execute_access, "" "") "        ' Stop        Execute(script) Operate=execute_accessEnd IfEnd Function

In the above function, we interpret the OP passed as a parameter as a function that is applied to another parameter, obj. In order for the OP to contain parameters of its corresponding function itself, such as passing in {strright (, | "|)} string corresponding to Strright function and | "| parameter, operate some processing of parentheses and commas that the OP might contain. With the operate function as the basis, we can simulate the above map method:

FunctionArraymap (arr as Variant, op asString) as Variant' operates  onEach elementinchAnArray    IfIsArray (arr) ThenDim result () as Variant dim lb AsInteger, UB asIntegerLb=lbound (arr) ub=ubound (arr) ReDim result (LB toUB) Dim I asInteger         forI=lb toUB result (i) =operate (arr (i), op)NextArraymap=resultEnd IfEnd Function

In the 96. The general field modifier gives the end of the code, and we see the frequent use of the Arraymap function, which makes the multiple processing of the array elements appear logically clear, if the use of traditional loops to write, not only the code will be much more difficult to understand and maintain.

97. Simulating functional programming in LotusScript

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.