SlideShare a Scribd company logo
1 of 24
Pemrograman Fungsional
Pertemuan 2 – Fitur Pemrograman Fungsional
19 Oktober 2020
Capaian Mata Kuliah
• (M1) Mahasiswa Mampu Memahami Fitur dalam Pemrograman
Fungsional
• (M2) Mahasiswa mampu Menerapkan Fitur dalam Pemrograman
Fungsional
Python
• Mendukung functional programming walupun bukanlah yang utama
• Dalam python semua adalah object sehingga fungi dapat diassign
dalam variable
def coba(a, b):
return a + b
tambah = coba
tambah(1, 2) >> return 3
Contoh Functional
def x(a):
return a+10
print(x(2))
Def x(a):
return a+10
y=x
Print(y(2))
Contoh Functional
def x(a,b):
return a+b
print(x(2,2))
print (x(2,2), y(5,4))
Lambda
• Anonymous Function
• Membuat fungsi secara deklaratif
• lambda arguments : expression
Contoh Lambda
• x = lambda a, b : a * b
print(x(5, 6))
• x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
x = lambda a, b :
c = a * b
return c
print(x(5, 6))
Map
map(function, iterable, ...)
return menggunakan list
Map
def kuadrat(n):
return n*n
x = [1, 2, 3, 4, 5]
hasil = map(kuadrat, x)
print(list(hasil)
map(lambda x : x*x, x)
Filter
def coba(n):
return n % 2
x = [1, 2, 3, 4, 5]
hasil = filter(coba, x)
print(list(hasil))
filter(lambda x : x % 2, x)
print(list(filter(lambda x : x % 2, x)))
First Class Object
Def add(a, b):
return a + b
Add2 = add
Add2(1,2) // 3
Def giveMeadd():
deff add(a, b):
return a + b
return add
First Class Object
• Def coba (a):
• Return a
• Print(coba(1))
• Lagi = coba
• Print(lagi(2))
Function Pass an Argumen
• >>> def coba(a,b):
• ... return a+b
• >>> def lagi(a,b):
• ... return a-b
• >>> def hasil(gabung):
• ... hasil = gabung
• ... print (hasil)
• >>> hasil1 = coba(2,2)
• >>> print(hasil1)
• >>> hasil2 = lagi(5,1)
• >>> print(hasil2)
• hasil(hasil1)
• hasil(hasil2)
Function as Argument
def summation(nums): # normal function
return sum(nums)
def main(f, *args): # function as an argument
result = f(*args)
print(result)
if __name__ == "__main__":
main(summation, [1,2,3]) # output 6
High Order Function
• deff food():
• return “food got eaten”
• drink : lambda : “drink got drunk”
• def me (*arg):
• def eat ():
• return map(lambda x : x(), args)
• Return eat
• act = me (food, drink)
• print(act)
function as a return value
def add_tw0_nums(x, y): # normal function which returns data
return x + y
def add_three_nums(x, y, z): # normal function which returns data
return x + y + z
def get_appropriate_function(num_len): # function which returns functions
depending on the logic
if num_len == 3:
return add_three_nums
else:
return add_tw0_nums
function as a return value
if __name__ == "__main__":
args = [1, 2, 3]
num_len = len(args)
res_function = get_appropriate_function(num_len)
print(res_function) # <function add_three_nums at 0x7f8f34173668>
print(res_function(*args)) # unpack the args, output 6
args = [1, 2]
num_len = len(args)
res_function = get_appropriate_function(num_len)
print(res_function) # <function add_tw0_nums at 0x7f1630955e18>
print(res_function(*args)) # unpack the args, output 3
closure
def coba():
... a = 1
... def tambah(x):
... return x + a
... return tambah
>>> closure1 = coba()
>>> print(closure1(2))
3
>>> print(closure1(5))
6
Decorator
def smart_divide(func):
def inner(a,b):
print("Saya akan membagi",a,"dan",b)
if b == 0:
print("Whoops! tidak bisa membagi dengan 0")
return
return func(a,b)
return inner
@smart_divide
def divide(a,b):
return a/b
Local Global
def f():
print(s)
s = "Python"
f()
def f():
s = "Perl"
print(s)
s = "Python"
f()
print(s)
Error
def f():
print(s)
s = "Perl"
print(s)
s = "Python"
f()
print(s)
Global
def f():
global s
print(s)
s = "dog"
print(s)
s = "cat"
f()
print(s)
Tugas
• Buat program yang mengimplementasikan
• Lambda
• Filter
• Map
• Fist Class Object
• High order function
• Function as return value
• Closure
• decorator

More Related Content

Similar to Slide PF Pertemuan 2.pptx

Functions in python
Functions in pythonFunctions in python
Functions in python
Ilian Iliev
 
Part 3-functions
Part 3-functionsPart 3-functions
Part 3-functions
ankita44
 

Similar to Slide PF Pertemuan 2.pptx (20)

Functions_21_22.pdf
Functions_21_22.pdfFunctions_21_22.pdf
Functions_21_22.pdf
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdf
 
Functions12
Functions12Functions12
Functions12
 
Functions123
Functions123 Functions123
Functions123
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Part 3-functions
Part 3-functionsPart 3-functions
Part 3-functions
 
10. haskell Modules
10. haskell Modules10. haskell Modules
10. haskell Modules
 
Functional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with PythonFunctional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with Python
 
Functional python
Functional pythonFunctional python
Functional python
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
Python functions
Python functionsPython functions
Python functions
 
Function
FunctionFunction
Function
 
Programming For Engineers Functions - Part #1.pptx
Programming For Engineers Functions - Part #1.pptxProgramming For Engineers Functions - Part #1.pptx
Programming For Engineers Functions - Part #1.pptx
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
20170317 functional programming in julia
20170317 functional programming in julia20170317 functional programming in julia
20170317 functional programming in julia
 
Functors, applicatives, monads
Functors, applicatives, monadsFunctors, applicatives, monads
Functors, applicatives, monads
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
functions
functionsfunctions
functions
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
FIDO Alliance
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 

Slide PF Pertemuan 2.pptx

  • 1. Pemrograman Fungsional Pertemuan 2 – Fitur Pemrograman Fungsional 19 Oktober 2020
  • 2. Capaian Mata Kuliah • (M1) Mahasiswa Mampu Memahami Fitur dalam Pemrograman Fungsional • (M2) Mahasiswa mampu Menerapkan Fitur dalam Pemrograman Fungsional
  • 3. Python • Mendukung functional programming walupun bukanlah yang utama • Dalam python semua adalah object sehingga fungi dapat diassign dalam variable def coba(a, b): return a + b tambah = coba tambah(1, 2) >> return 3
  • 4. Contoh Functional def x(a): return a+10 print(x(2)) Def x(a): return a+10 y=x Print(y(2))
  • 5. Contoh Functional def x(a,b): return a+b print(x(2,2)) print (x(2,2), y(5,4))
  • 6. Lambda • Anonymous Function • Membuat fungsi secara deklaratif • lambda arguments : expression
  • 7. Contoh Lambda • x = lambda a, b : a * b print(x(5, 6)) • x = lambda a, b, c : a + b + c print(x(5, 6, 2))
  • 8. x = lambda a, b : c = a * b return c print(x(5, 6))
  • 10. Map def kuadrat(n): return n*n x = [1, 2, 3, 4, 5] hasil = map(kuadrat, x) print(list(hasil) map(lambda x : x*x, x)
  • 11. Filter def coba(n): return n % 2 x = [1, 2, 3, 4, 5] hasil = filter(coba, x) print(list(hasil)) filter(lambda x : x % 2, x) print(list(filter(lambda x : x % 2, x)))
  • 12. First Class Object Def add(a, b): return a + b Add2 = add Add2(1,2) // 3 Def giveMeadd(): deff add(a, b): return a + b return add
  • 13. First Class Object • Def coba (a): • Return a • Print(coba(1)) • Lagi = coba • Print(lagi(2))
  • 14. Function Pass an Argumen • >>> def coba(a,b): • ... return a+b • >>> def lagi(a,b): • ... return a-b • >>> def hasil(gabung): • ... hasil = gabung • ... print (hasil) • >>> hasil1 = coba(2,2) • >>> print(hasil1) • >>> hasil2 = lagi(5,1) • >>> print(hasil2) • hasil(hasil1) • hasil(hasil2)
  • 15. Function as Argument def summation(nums): # normal function return sum(nums) def main(f, *args): # function as an argument result = f(*args) print(result) if __name__ == "__main__": main(summation, [1,2,3]) # output 6
  • 16. High Order Function • deff food(): • return “food got eaten” • drink : lambda : “drink got drunk” • def me (*arg): • def eat (): • return map(lambda x : x(), args) • Return eat • act = me (food, drink) • print(act)
  • 17. function as a return value def add_tw0_nums(x, y): # normal function which returns data return x + y def add_three_nums(x, y, z): # normal function which returns data return x + y + z def get_appropriate_function(num_len): # function which returns functions depending on the logic if num_len == 3: return add_three_nums else: return add_tw0_nums
  • 18. function as a return value if __name__ == "__main__": args = [1, 2, 3] num_len = len(args) res_function = get_appropriate_function(num_len) print(res_function) # <function add_three_nums at 0x7f8f34173668> print(res_function(*args)) # unpack the args, output 6 args = [1, 2] num_len = len(args) res_function = get_appropriate_function(num_len) print(res_function) # <function add_tw0_nums at 0x7f1630955e18> print(res_function(*args)) # unpack the args, output 3
  • 19. closure def coba(): ... a = 1 ... def tambah(x): ... return x + a ... return tambah >>> closure1 = coba() >>> print(closure1(2)) 3 >>> print(closure1(5)) 6
  • 20. Decorator def smart_divide(func): def inner(a,b): print("Saya akan membagi",a,"dan",b) if b == 0: print("Whoops! tidak bisa membagi dengan 0") return return func(a,b) return inner @smart_divide def divide(a,b): return a/b
  • 21. Local Global def f(): print(s) s = "Python" f() def f(): s = "Perl" print(s) s = "Python" f() print(s)
  • 22. Error def f(): print(s) s = "Perl" print(s) s = "Python" f() print(s)
  • 23. Global def f(): global s print(s) s = "dog" print(s) s = "cat" f() print(s)
  • 24. Tugas • Buat program yang mengimplementasikan • Lambda • Filter • Map • Fist Class Object • High order function • Function as return value • Closure • decorator