SlideShare a Scribd company logo
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_21_22.pdf
Functions_21_22.pdfFunctions_21_22.pdf
Functions_21_22.pdf
paijitk
 
Functions in python
Functions in pythonFunctions in python
Functions in python
Ilian Iliev
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdf
paijitk
 
Functions12
Functions12Functions12
Functions12
sandhubuta
 
Functions123
Functions123 Functions123
Functions123
sandhubuta
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Nikhil Pandit
 
Part 3-functions
Part 3-functionsPart 3-functions
Part 3-functions
ankita44
 
10. haskell Modules
10. haskell Modules10. haskell Modules
10. haskell Modules
Sebastian Rettig
 
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
Carlos V.
 
Functional python
Functional pythonFunctional python
Functional python
Jesué Junior
 
cbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxcbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptx
tcsonline1222
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
2017eee0459
 
Python functions
Python functionsPython functions
Python functions
ToniyaP1
 
Function
FunctionFunction
Function
yash patel
 
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
NoorAntakia
 
Functions in python
Functions in pythonFunctions in python
Functions in python
colorsof
 
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
rkaippully
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
Praveen M Jigajinni
 
functions
functionsfunctions
functions
Makwana Bhavesh
 

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
 
cbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxcbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptx
 
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
 

Recently uploaded

How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
fredae14
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
jpupo2018
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
David Brossard
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 

Recently uploaded (20)

How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 

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