SlideShare a Scribd company logo
CHAPTER
FORTYTHREE
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
43.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
331
Ring Documentation, Release 1.8
aList2
1
4
9
16
25
43.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!
43.3 Higher-order Functions
Higher-order functions are the functions that takes other functions as parameters.
Example:
Func Main
times(5,:test)
43.2. First-class Functions 332
Ring Documentation, Release 1.8
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!
43.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
43.4. Anonymous and Nested Functions 333
Ring Documentation, Release 1.8
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
43.5 Equality of functions
We can test if function = function or not using the ‘=’ or ‘!=’ operators
43.5. Equality of functions 334
Ring Documentation, Release 1.8
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
43.5. Equality of functions 335
CHAPTER
FORTYFOUR
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.
• locals()
• globals()
• functions()
• cfunctions()
• islocal()
• isglobal()
• isfunction()
• iscfunction()
• packages()
• ispackage()
• classes()
• isclass()
• packageclasses()
• ispackageclass()
• classname()
• objectid()
• isobject()
• attributes()
• methods()
• isattribute()
• isprivateattribute()
• ismethod()
• isprivatemethod()
• addattribute()
• addmethod()
336
Ring Documentation, Release 1.8
• getattribute()
• setattribute()
• mergemethods()
• packagename()
44.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
44.2 globals() Function
We can get a list of variables names in the global scope using the globals() function.
Syntax:
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:
44.1. locals() Function 337
Ring Documentation, Release 1.8
message from test()
Global Variables:
x
y
z
44.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
44.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:
aList = cfunctions()
See "Count : " + len(aList) + nl
for x in aList
see x + "()" + nl
next
Output:
Count : 205
len()
add()
del()
get()
44.3. functions() Function 338
Ring Documentation, Release 1.8
clock()
...
Note: The complete list is removed from the previous output.
44.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
44.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:
x=10 y=20
test()
func test
see isglobal("x") + nl +
isglobal("y") + nl +
isglobal("z") + nl
Output:
1
1
0
44.5. islocal() Function 339
Ring Documentation, Release 1.8
44.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
44.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
Output:
1
1
0
44.9 packages() Function
We can get a list of packages names using the packages() function.
Syntax:
packages() --> a list contains packages names
44.7. isfunction() Function 340

More Related Content

What's hot (20)

PDF
Scala is java8.next()
daewon jeong
 
PDF
Java 8 Lambda Expressions
Scott Leberknight
 
ODP
Clojure basics
Knoldus Inc.
 
PDF
Refactoring to Java 8 (Devoxx BE)
Trisha Gee
 
PDF
Scala categorytheory
Knoldus Inc.
 
PPT
Scala functions
Knoldus Inc.
 
PPTX
Java 7, 8 & 9 - Moving the language forward
Mario Fusco
 
PPTX
Java Generics
Zülfikar Karakaya
 
PDF
FP in Java - Project Lambda and beyond
Mario Fusco
 
PDF
Practical Functional Programming Presentation by Bogdan Hodorog
3Pillar Global
 
PDF
Java Class Design
Ganesh Samarthyam
 
PDF
The Ring programming language version 1.6 book - Part 34 of 189
Mahmoud Samir Fayed
 
PPT
Introduction to Functional Programming in JavaScript
tmont
 
PDF
Pragmatic functional refactoring with java 8
RichardWarburton
 
PDF
Thinking in Functions: Functional Programming in Python
Anoop Thomas Mathew
 
PDF
The Ring programming language version 1.7 book - Part 36 of 196
Mahmoud Samir Fayed
 
ODP
Functional Programming With Scala
Knoldus Inc.
 
PDF
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
Ganesh Samarthyam
 
PPTX
Java generics
Hosein Zare
 
PDF
Demystifying functional programming with Scala
Denis
 
Scala is java8.next()
daewon jeong
 
Java 8 Lambda Expressions
Scott Leberknight
 
Clojure basics
Knoldus Inc.
 
Refactoring to Java 8 (Devoxx BE)
Trisha Gee
 
Scala categorytheory
Knoldus Inc.
 
Scala functions
Knoldus Inc.
 
Java 7, 8 & 9 - Moving the language forward
Mario Fusco
 
Java Generics
Zülfikar Karakaya
 
FP in Java - Project Lambda and beyond
Mario Fusco
 
Practical Functional Programming Presentation by Bogdan Hodorog
3Pillar Global
 
Java Class Design
Ganesh Samarthyam
 
The Ring programming language version 1.6 book - Part 34 of 189
Mahmoud Samir Fayed
 
Introduction to Functional Programming in JavaScript
tmont
 
Pragmatic functional refactoring with java 8
RichardWarburton
 
Thinking in Functions: Functional Programming in Python
Anoop Thomas Mathew
 
The Ring programming language version 1.7 book - Part 36 of 196
Mahmoud Samir Fayed
 
Functional Programming With Scala
Knoldus Inc.
 
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
Ganesh Samarthyam
 
Java generics
Hosein Zare
 
Demystifying functional programming with Scala
Denis
 

Similar to The Ring programming language version 1.8 book - Part 37 of 202 (20)

PDF
The Ring programming language version 1.9 book - Part 40 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.3 book - Part 32 of 184
Mahmoud Samir Fayed
 
PPTX
Python functions
ToniyaP1
 
PDF
The Ring programming language version 1.6 book - Part 33 of 189
Mahmoud Samir Fayed
 
PPTX
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
tony8553004135
 
PDF
The Ring programming language version 1.5.3 book - Part 31 of 184
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.2 book - Part 21 of 181
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 42 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.1 book - Part 20 of 180
Mahmoud Samir Fayed
 
PDF
TI1220 Lecture 6: First-class Functions
Eelco Visser
 
PPTX
General Mathematics 11 -review on functions
JudyDacawan
 
PDF
Clojure intro
Basav Nagur
 
PDF
The Ring programming language version 1.8 book - Part 25 of 202
Mahmoud Samir Fayed
 
PDF
Functions2.pdf
prasnt1
 
PDF
The Ring programming language version 1.5.1 book - Part 19 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 38 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5 book - Part 6 of 31
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 98 of 210
Mahmoud Samir Fayed
 
PDF
TDC2016SP - Trilha Programação Funcional
tdc-globalcode
 
PDF
Haskell 101
Roberto Pepato
 
The Ring programming language version 1.9 book - Part 40 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 32 of 184
Mahmoud Samir Fayed
 
Python functions
ToniyaP1
 
The Ring programming language version 1.6 book - Part 33 of 189
Mahmoud Samir Fayed
 
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
tony8553004135
 
The Ring programming language version 1.5.3 book - Part 31 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 21 of 181
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 42 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 20 of 180
Mahmoud Samir Fayed
 
TI1220 Lecture 6: First-class Functions
Eelco Visser
 
General Mathematics 11 -review on functions
JudyDacawan
 
Clojure intro
Basav Nagur
 
The Ring programming language version 1.8 book - Part 25 of 202
Mahmoud Samir Fayed
 
Functions2.pdf
prasnt1
 
The Ring programming language version 1.5.1 book - Part 19 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 38 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.5 book - Part 6 of 31
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 98 of 210
Mahmoud Samir Fayed
 
TDC2016SP - Trilha Programação Funcional
tdc-globalcode
 
Haskell 101
Roberto Pepato
 
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
 
Ad

Recently uploaded (20)

PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PDF
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
Human Resources Information System (HRIS)
Amity University, Patna
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 

The Ring programming language version 1.8 book - Part 37 of 202

  • 1. CHAPTER FORTYTHREE 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 43.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 331
  • 2. Ring Documentation, Release 1.8 aList2 1 4 9 16 25 43.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! 43.3 Higher-order Functions Higher-order functions are the functions that takes other functions as parameters. Example: Func Main times(5,:test) 43.2. First-class Functions 332
  • 3. Ring Documentation, Release 1.8 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! 43.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 43.4. Anonymous and Nested Functions 333
  • 4. Ring Documentation, Release 1.8 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 43.5 Equality of functions We can test if function = function or not using the ‘=’ or ‘!=’ operators 43.5. Equality of functions 334
  • 5. Ring Documentation, Release 1.8 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 43.5. Equality of functions 335
  • 6. CHAPTER FORTYFOUR 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. • locals() • globals() • functions() • cfunctions() • islocal() • isglobal() • isfunction() • iscfunction() • packages() • ispackage() • classes() • isclass() • packageclasses() • ispackageclass() • classname() • objectid() • isobject() • attributes() • methods() • isattribute() • isprivateattribute() • ismethod() • isprivatemethod() • addattribute() • addmethod() 336
  • 7. Ring Documentation, Release 1.8 • getattribute() • setattribute() • mergemethods() • packagename() 44.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 44.2 globals() Function We can get a list of variables names in the global scope using the globals() function. Syntax: 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: 44.1. locals() Function 337
  • 8. Ring Documentation, Release 1.8 message from test() Global Variables: x y z 44.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 44.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: aList = cfunctions() See "Count : " + len(aList) + nl for x in aList see x + "()" + nl next Output: Count : 205 len() add() del() get() 44.3. functions() Function 338
  • 9. Ring Documentation, Release 1.8 clock() ... Note: The complete list is removed from the previous output. 44.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 44.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: x=10 y=20 test() func test see isglobal("x") + nl + isglobal("y") + nl + isglobal("z") + nl Output: 1 1 0 44.5. islocal() Function 339
  • 10. Ring Documentation, Release 1.8 44.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 44.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 Output: 1 1 0 44.9 packages() Function We can get a list of packages names using the packages() function. Syntax: packages() --> a list contains packages names 44.7. isfunction() Function 340