SlideShare a Scribd company logo
CHAPTER
THIRTYFIVE
FUNCTIONAL PROGRAMMING
In previous chapters we learned about Functions and Recursion.
In this chapter we are going to learn about more Functional Programming (FP) concepts like
• Pure Functions
• First-class functions
• Higher-order functions
• Anonymous and nested functions.
• Equality of functions
35.1 Pure Functions
We can create pure functions (functions that doesn’t change the state) by the help of the assignment operator to copy
variables (Lists & Objects) by value to create new variables instead of working on the original data that are passed to
the function by reference.
Example:
Func Main
aList = [1,2,3,4,5]
aList2 = square(aList)
see "aList" + nl
see aList
see "aList2" + nl
see aList2
Func Square aPara
a1 = aPara # copy the list
for x in a1
x *= x
next
return a1 # return new list
Output:
aList
1
2
3
4
5
180
Ring Documentation, Release 1.2
aList2
1
4
9
16
25
35.2 First-class Functions
Functions inside the Ring programming language are first-class citizens, you can pass functions as parameters, return
them as value or store them in variables.
We can pass/return the function by typing the function name as literal like “FunctionName” or :FunctionName for
example.
We can pass/return functions using the variable that contains the function name.
We can call function from variables contains the function name using the Call command
Syntax:
Call Variable([Parameters])
Example:
Func Main
see "before test2()" + nl
f = Test2(:Test)
see "after test2()" + nl
call f()
Func Test
see "Message from test!" + nl
Func Test2 f1
call f1()
See "Message from test2!" + nl
return f1
Output:
before test2()
Message from test!
Message from test2!
after test2()
Message from test!
35.3 Higher-order Functions
Higher-order functions are the functions that takes other functions as parameters.
Example:
Func Main
times(5,:test)
35.2. First-class Functions 181
Ring Documentation, Release 1.2
Func Test
see "Message from the test function!" + nl
Func Times nCount,F
for x = 1 to nCount
Call F()
next
Output:
Message from the test function!
Message from the test function!
Message from the test function!
Message from the test function!
Message from the test function!
35.4 Anonymous and Nested Functions
Anonymous Functions are functions without names that can be passed as parameters to other functions or stored in
variables.
Syntax:
Func [Parameters] { [statements] }
Example:
test( func x,y {
see "hello" + nl
see "Sum : " + (x+y) + nl
} )
new great { f1() }
times(3, func { see "hello world" + nl } )
func test x
call x(3,3)
see "wow!" + nl
func times n,x
for t=1 to n
call x()
next
Class great
func f1
f2( func { see "Message from f1" + nl } )
func f2 x
call x()
Output:
hello
Sum : 6
35.4. Anonymous and Nested Functions 182
Ring Documentation, Release 1.2
wow!
Message from f1
hello world
hello world
hello world
Example:
Func Main
aList = [1,2,3,4]
Map (aList , func x {
return x*x
} )
see aList
aList = [4,9,14,25]
Map(aList, :myfilter )
see aList
aList = [11,12,13,14]
Map (aList , func x {
if x%2=0
return "even"
else
return "odd"
ok
})
see aList
Func myfilter x
if x = 9
return "True"
else
return "False"
ok
Func Map aList,cFunc
for x in aList
x = call cFunc(x)
next
Output:
1
4
9
16
False
True
False
False
odd
even
odd
even
35.5 Equality of functions
We can test if function = function or not using the ‘=’ or ‘!=’ operators
35.5. Equality of functions 183
Ring Documentation, Release 1.2
Example:
f1 = func { see "hello" + nl }
f2 = func { see "how are you?" + nl }
f3 = f1
call f1()
call f2()
call f3()
see (f1 = f2) + nl
see (f2 = f3) + nl
see (f1 = f3) + nl
Output:
hello
how are you?
hello
0
0
1
35.5. Equality of functions 184
CHAPTER
THIRTYSIX
REFLECTION AND META-PROGRAMMING
Since the Ring programming language is a dynamic language, we can get answers about the program code and we can
modify our code during the runtime.
In this chapter we will learn about this and the available functions to use.
36.1 locals() Function
We can get a list of variables names in the current scope using the locals() function.
Syntax:
locals() --> a list contains the variables names in the current scope
Example:
test("hello")
func test cMsg
see cMsg + nl
x = 10
y = 20
z = 30
see locals()
Output:
hello
cmsg
x
y
z
36.2 globals() Function
We can get a list of variables names in the global scope using the globals() function.
Syntax:
185
Ring Documentation, Release 1.2
globals() --> a list contains variables names in the global scope
Example:
x=10 y=20 z=30
test()
func test
see "message from test()" + nl +
"Global Variables:" + nl
see globals()
Output:
message from test()
Global Variables:
x
y
z
36.3 functions() Function
We can get a list of functions names written in the Ring language using the functions() function.
Syntax:
functions() --> a list contains functions names
Example:
see functions()
func f1
see "f1" + nl
func f2
see "f2" + nl
func f3
see "f3" + nl
Output:
f1
f2
f3
36.4 cfunctions() Function
We can get a list of functions names written in the C language using the cfunctions() function.
Syntax:
cfunctions() --> a list contains functions names
Example:
36.3. functions() Function 186
Ring Documentation, Release 1.2
aList = cfunctions()
See "Count : " + len(aList) + nl
for x in aList
see x + "()" + nl
next
Output:
Count : 217
len()
add()
del()
get()
clock()
...
Note: The complete list is removed from the previous output.
36.5 islocal() Function
We can check if a variable is defined in the local scope or not using the islocal() function.
Syntax:
islocal(cVariableName) --> returns 1 if the variable is defined in the local scope
returns 0 if the variable is not defined in the local scope
Example:
test()
func test
x=10 y=20
see islocal("x") + nl +
islocal("y") + nl +
islocal("z") + nl
Output:
1
1
0
36.6 isglobal() Function
We can check if a variable is defined in the global scope or not using the isglobal() function.
Syntax:
isglobal(cVariableName) --> returns 1 if the variable is defined in the global scope
returns 0 if the variable is not defined in the global scope
Example:
36.5. islocal() Function 187
Ring Documentation, Release 1.2
x=10 y=20
test()
func test
see isglobal("x") + nl +
isglobal("y") + nl +
isglobal("z") + nl
Output:
1
1
0
36.7 isfunction() Function
We can check if a Ring function is defined or not using the isfunction() function.
Syntax:
isfunction(cFunctionName) --> returns 1 if the Ring function is defined
returns 0 if the Ring function is not defined
Example:
see isfunction("f1") + nl +
isfunction("f2") + nl +
isfunction("f3") + nl
func f1
see "message from f1()" + nl
func f2
see "message from f2()" + nl
Output:
1
1
0
36.8 iscfunction() Function
We can check if a C function is defined or not using the iscfunction() function.
Syntax:
iscfunction(cFunctionName) --> returns 1 if the C function is defined
returns 0 if the C function is not defined
Example:
see iscfunction("len") + nl +
iscfunction("add") + nl +
iscfunction("test") + nl
36.7. isfunction() Function 188
Ring Documentation, Release 1.2
Output:
1
1
0
36.9 packages() Function
We can get a list of packages names using the packages() function.
Syntax:
packages() --> a list contains packages names
Example:
See packages()
Package Package1
Class class1
Func f1
Package Package2
Class class1
Func f1
Package Package3
Class class1
Func f1
Package Package4
Class class1
Func f1
Output:
package1
package2
package3
package4
36.10 ispackage() Function
We can check if a package is defined or not using the ispackage() function.
Syntax:
ispackage(cPackageName) --> returns 1 if the Package is defined
returns 0 if the Package is not defined
Example:
See ispackage("package1") + nl +
ispackage("package4") + nl +
ispackage("package5") + nl +
ispackage("package3") + nl
36.9. packages() Function 189

More Related Content

What's hot (20)

PPTX
20170317 functional programming in julia
岳華 杜
 
PPTX
20170714 concurrency in julia
岳華 杜
 
PDF
The Ring programming language version 1.5.1 book - Part 31 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 41 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 11 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 10 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.1 book - Part 20 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 12 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 9 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.3 book - Part 6 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 27 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.1 book - Part 74 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.1 book - Part 19 of 180
Mahmoud Samir Fayed
 
PPTX
Is java8 a true functional programming language
SQLI
 
PPTX
Is java8a truefunctionallanguage
Samir Chekkal
 
PDF
The Ring programming language version 1.8 book - Part 25 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 86 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 42 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.7 book - Part 35 of 196
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 38 of 202
Mahmoud Samir Fayed
 
20170317 functional programming in julia
岳華 杜
 
20170714 concurrency in julia
岳華 杜
 
The Ring programming language version 1.5.1 book - Part 31 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 41 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 11 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 10 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 20 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 12 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 9 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 6 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 27 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 74 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 19 of 180
Mahmoud Samir Fayed
 
Is java8 a true functional programming language
SQLI
 
Is java8a truefunctionallanguage
Samir Chekkal
 
The Ring programming language version 1.8 book - Part 25 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 86 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 42 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 35 of 196
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 38 of 202
Mahmoud Samir Fayed
 

Viewers also liked (20)

PDF
The Ring programming language version 1.2 book - Part 23 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 22 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 24 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 25 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 26 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 17 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 34 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 33 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 37 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 18 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 1 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 15 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 20 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 16 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 19 of 84
Mahmoud Samir Fayed
 
PDF
Variables Python
Armando Rodriguez L
 
PDF
How to make font transparent in PowerPoint
Presentitude
 
PDF
The Ring programming language version 1.2 book - Part 27 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 14 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 38 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 23 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 22 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 24 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 25 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 26 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 17 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 34 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 33 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 37 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 18 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 1 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 15 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 20 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 16 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 19 of 84
Mahmoud Samir Fayed
 
Variables Python
Armando Rodriguez L
 
How to make font transparent in PowerPoint
Presentitude
 
The Ring programming language version 1.2 book - Part 27 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 14 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 38 of 84
Mahmoud Samir Fayed
 
Ad

Similar to The Ring programming language version 1.2 book - Part 21 of 84 (20)

PDF
The Ring programming language version 1.5.1 book - Part 30 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.3 book - Part 32 of 184
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.3 book - Part 25 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.3 book - Part 13 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 26 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.4 book - Part 22 of 185
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 28 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.2 book - Part 33 of 181
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 39 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.4 book - Part 6 of 30
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.6 book - Part 24 of 189
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.3 book - Part 18 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.4 book - Part 21 of 185
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.7 book - Part 24 of 196
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.6 book - Part 36 of 189
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.1 book - Part 32 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 32 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 13 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5 book - Part 5 of 31
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 31 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 30 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 32 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 25 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 13 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 26 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 22 of 185
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 28 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 33 of 181
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 39 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.4 book - Part 6 of 30
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 24 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 18 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 21 of 185
Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 24 of 196
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 36 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 32 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 32 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 13 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.5 book - Part 5 of 31
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 31 of 210
Mahmoud Samir Fayed
 
Ad

More from Mahmoud Samir Fayed (20)

PDF
The Ring programming language version 1.10 book - Part 212 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 211 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 210 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 208 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 207 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 205 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 206 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 204 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 203 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 202 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 201 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 200 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 199 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 198 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 197 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 196 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 195 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 194 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 193 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 192 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 212 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 211 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 210 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 208 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 207 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 205 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 206 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 204 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 203 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 202 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 201 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 200 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 199 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 198 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 197 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 196 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 195 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 194 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 193 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 192 of 212
Mahmoud Samir Fayed
 

Recently uploaded (20)

PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 

The Ring programming language version 1.2 book - Part 21 of 84

  • 1. CHAPTER THIRTYFIVE FUNCTIONAL PROGRAMMING In previous chapters we learned about Functions and Recursion. In this chapter we are going to learn about more Functional Programming (FP) concepts like • Pure Functions • First-class functions • Higher-order functions • Anonymous and nested functions. • Equality of functions 35.1 Pure Functions We can create pure functions (functions that doesn’t change the state) by the help of the assignment operator to copy variables (Lists & Objects) by value to create new variables instead of working on the original data that are passed to the function by reference. Example: Func Main aList = [1,2,3,4,5] aList2 = square(aList) see "aList" + nl see aList see "aList2" + nl see aList2 Func Square aPara a1 = aPara # copy the list for x in a1 x *= x next return a1 # return new list Output: aList 1 2 3 4 5 180
  • 2. Ring Documentation, Release 1.2 aList2 1 4 9 16 25 35.2 First-class Functions Functions inside the Ring programming language are first-class citizens, you can pass functions as parameters, return them as value or store them in variables. We can pass/return the function by typing the function name as literal like “FunctionName” or :FunctionName for example. We can pass/return functions using the variable that contains the function name. We can call function from variables contains the function name using the Call command Syntax: Call Variable([Parameters]) Example: Func Main see "before test2()" + nl f = Test2(:Test) see "after test2()" + nl call f() Func Test see "Message from test!" + nl Func Test2 f1 call f1() See "Message from test2!" + nl return f1 Output: before test2() Message from test! Message from test2! after test2() Message from test! 35.3 Higher-order Functions Higher-order functions are the functions that takes other functions as parameters. Example: Func Main times(5,:test) 35.2. First-class Functions 181
  • 3. Ring Documentation, Release 1.2 Func Test see "Message from the test function!" + nl Func Times nCount,F for x = 1 to nCount Call F() next Output: Message from the test function! Message from the test function! Message from the test function! Message from the test function! Message from the test function! 35.4 Anonymous and Nested Functions Anonymous Functions are functions without names that can be passed as parameters to other functions or stored in variables. Syntax: Func [Parameters] { [statements] } Example: test( func x,y { see "hello" + nl see "Sum : " + (x+y) + nl } ) new great { f1() } times(3, func { see "hello world" + nl } ) func test x call x(3,3) see "wow!" + nl func times n,x for t=1 to n call x() next Class great func f1 f2( func { see "Message from f1" + nl } ) func f2 x call x() Output: hello Sum : 6 35.4. Anonymous and Nested Functions 182
  • 4. Ring Documentation, Release 1.2 wow! Message from f1 hello world hello world hello world Example: Func Main aList = [1,2,3,4] Map (aList , func x { return x*x } ) see aList aList = [4,9,14,25] Map(aList, :myfilter ) see aList aList = [11,12,13,14] Map (aList , func x { if x%2=0 return "even" else return "odd" ok }) see aList Func myfilter x if x = 9 return "True" else return "False" ok Func Map aList,cFunc for x in aList x = call cFunc(x) next Output: 1 4 9 16 False True False False odd even odd even 35.5 Equality of functions We can test if function = function or not using the ‘=’ or ‘!=’ operators 35.5. Equality of functions 183
  • 5. Ring Documentation, Release 1.2 Example: f1 = func { see "hello" + nl } f2 = func { see "how are you?" + nl } f3 = f1 call f1() call f2() call f3() see (f1 = f2) + nl see (f2 = f3) + nl see (f1 = f3) + nl Output: hello how are you? hello 0 0 1 35.5. Equality of functions 184
  • 6. CHAPTER THIRTYSIX REFLECTION AND META-PROGRAMMING Since the Ring programming language is a dynamic language, we can get answers about the program code and we can modify our code during the runtime. In this chapter we will learn about this and the available functions to use. 36.1 locals() Function We can get a list of variables names in the current scope using the locals() function. Syntax: locals() --> a list contains the variables names in the current scope Example: test("hello") func test cMsg see cMsg + nl x = 10 y = 20 z = 30 see locals() Output: hello cmsg x y z 36.2 globals() Function We can get a list of variables names in the global scope using the globals() function. Syntax: 185
  • 7. Ring Documentation, Release 1.2 globals() --> a list contains variables names in the global scope Example: x=10 y=20 z=30 test() func test see "message from test()" + nl + "Global Variables:" + nl see globals() Output: message from test() Global Variables: x y z 36.3 functions() Function We can get a list of functions names written in the Ring language using the functions() function. Syntax: functions() --> a list contains functions names Example: see functions() func f1 see "f1" + nl func f2 see "f2" + nl func f3 see "f3" + nl Output: f1 f2 f3 36.4 cfunctions() Function We can get a list of functions names written in the C language using the cfunctions() function. Syntax: cfunctions() --> a list contains functions names Example: 36.3. functions() Function 186
  • 8. Ring Documentation, Release 1.2 aList = cfunctions() See "Count : " + len(aList) + nl for x in aList see x + "()" + nl next Output: Count : 217 len() add() del() get() clock() ... Note: The complete list is removed from the previous output. 36.5 islocal() Function We can check if a variable is defined in the local scope or not using the islocal() function. Syntax: islocal(cVariableName) --> returns 1 if the variable is defined in the local scope returns 0 if the variable is not defined in the local scope Example: test() func test x=10 y=20 see islocal("x") + nl + islocal("y") + nl + islocal("z") + nl Output: 1 1 0 36.6 isglobal() Function We can check if a variable is defined in the global scope or not using the isglobal() function. Syntax: isglobal(cVariableName) --> returns 1 if the variable is defined in the global scope returns 0 if the variable is not defined in the global scope Example: 36.5. islocal() Function 187
  • 9. Ring Documentation, Release 1.2 x=10 y=20 test() func test see isglobal("x") + nl + isglobal("y") + nl + isglobal("z") + nl Output: 1 1 0 36.7 isfunction() Function We can check if a Ring function is defined or not using the isfunction() function. Syntax: isfunction(cFunctionName) --> returns 1 if the Ring function is defined returns 0 if the Ring function is not defined Example: see isfunction("f1") + nl + isfunction("f2") + nl + isfunction("f3") + nl func f1 see "message from f1()" + nl func f2 see "message from f2()" + nl Output: 1 1 0 36.8 iscfunction() Function We can check if a C function is defined or not using the iscfunction() function. Syntax: iscfunction(cFunctionName) --> returns 1 if the C function is defined returns 0 if the C function is not defined Example: see iscfunction("len") + nl + iscfunction("add") + nl + iscfunction("test") + nl 36.7. isfunction() Function 188
  • 10. Ring Documentation, Release 1.2 Output: 1 1 0 36.9 packages() Function We can get a list of packages names using the packages() function. Syntax: packages() --> a list contains packages names Example: See packages() Package Package1 Class class1 Func f1 Package Package2 Class class1 Func f1 Package Package3 Class class1 Func f1 Package Package4 Class class1 Func f1 Output: package1 package2 package3 package4 36.10 ispackage() Function We can check if a package is defined or not using the ispackage() function. Syntax: ispackage(cPackageName) --> returns 1 if the Package is defined returns 0 if the Package is not defined Example: See ispackage("package1") + nl + ispackage("package4") + nl + ispackage("package5") + nl + ispackage("package3") + nl 36.9. packages() Function 189