0% found this document useful (0 votes)
16 views11 pages

Functional Programming 1

The document provides an overview of functional programming, highlighting its key concepts such as immutability, lambda functions, higher-order functions, and decorators. It explains the advantages of functional programming, including concise and maintainable code, and contrasts it with imperative programming. Additionally, it includes quizzes to reinforce understanding of the material covered.

Uploaded by

Kadasi Abishek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
16 views11 pages

Functional Programming 1

The document provides an overview of functional programming, highlighting its key concepts such as immutability, lambda functions, higher-order functions, and decorators. It explains the advantages of functional programming, including concise and maintainable code, and contrasts it with imperative programming. Additionally, it includes quizzes to reinforce understanding of the material covered.

Uploaded by

Kadasi Abishek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 11
m5 242° Furtoal Pogrenning t= Boss natesipr9 -Cdeb © Functional Programming - Basics Y Content +. Functional Programing 2. Lamba Functions 3. Higher reer Funesons 4. Decorators © Introduction Functional programming i a paradlgm where computations wewed asthe evaluation a mathemati functions It stands ot fortwo major easons + mutable Data: avoids chanlng state and mutable date. + Decrative Sil: stead ofinsttctng howto achiev something (ke imperatve programing), you declare wha” you want the program tod, Lote state te nates Goa eared nantes. appna{ountin) 1 rnettonal pron bers = Os 2,35 4 ‘eared punters» lat{ea lama #2, mses) Why use functional programming? ip oa ese ool somal 1OBOTH ESE ameGBecAKIOVIL Tsp i m5, 242° Furtoal Parana t- Boss natesipr -Cdeb + Concise and Readable: With furetional programing youte often working wi fair mathematical fureions, This makes code shorter nd intltve. Frinstance the nas, Fter, an reeuce fenton in Python are dct implementations of functional More Maintsinable Sine there arose eects rom mule da, tacking exors becomes sel Imagine net having wory outa variables vale being unexpectedly changed eleenhere in your progam! fice: Without he constant ned to update deta states, some funcional programs cn outperform thr mpertve counterparts, ¥ Quiz ¥ Question Why ean functional pragrms be more efit compared to npeatve programs? Choices + They alaays use fener lines of code + They operate sirectyon the hardware without neecing an 0S, + They ao el equ state updates, which ean bea cost operaton. + Theystitzea special compiler hat speeds up the execution Explanation + Funevoral programming languages afen promote immutailty, meaning once a data srucluve 8 eeatd i eanot bemodied. Ary ‘operation that seems to modify the data actually creates anew copy with the desired changes Imsmutebity cen ead to move predictable and faster code. I makes pall an concurent programming more staigorwets leading to tiieney + gestive programming onthe car hand ypicly involves mutable sate, Vacables canbe modiedin plac, leading to potential ae efects Managing mutable state can introduce compleiy anid make hard o reason about the behavior of progam, eepecaly aa mult tended or cneucent environment. > Lambda Functions + Alco krown a8 Anonymous fuetons ip oa ese ool somal 1OBMTHESEameGBecAKIOVIOL Tsp an 35,2420 Furtoal Parana t- Boss natesipr -Cdeb Syntae ‘+ tanta isthe keyword tat sigs he creation ofa lambda function. re the put parameters ofthe function. 0 ihe single expression or operation hat the function performs. ip tases goog ome! OBKSTHBSE ameSAscAKIOVJAL:TsP=HUMAEDINMOHE=Ne ais sms, 242° Furtoal Parana t- Boss natesipr -Cdeb (ates 6 199(2) 4 cab of 2 amber (lates, ye mY) C4,S) ateing te marbore 40 condition else y > Ternary Operator (ates as fs alse 944) 2 157.800.2351 et yale = tata ark") stores = ‘Cana Coe Cee omer ip eos eae gol soma 1OBSTHESEmmeGBecAKIOVIOL Tsp i325, 242°M1 Furtoal Parana t- Boss natesipr -Cdeb (nae: 7, anes" 68), 1 suerte] > stucers(2] “Typetrror ‘Traceback (wort recert call last) Ulpythontrgutas-fuesbstsas 49 cell ne! 21) ST Steams > satensta) eet otetstudenstan) eet value snus) et yeluetetusonts[a}) > gt_vua(eterts[s)) sorectstutents) Sgpeeror "reabsck (ease raceme cal Ise) eee raaanty to tts 38 ‘The sorted() function takes an extra argrument known as key, which takes @ function to judge how to compare for sorting orsotetdent, Key = Labi x: x{"nme"), roveseetrs) Se (inane: ey ares ‘name's “0°, mares 78 ip tases goog ome! OBKSTHBSE ameSAscAKIOVJAL:TsP=HUMAEDINMOHE=Ne sms, 242°u Furtoal Parana t- Boss natesipr -Cdeb mes Cy mas pomess 58) tine: ie), (Gomes, tare S31 » Quiz2 Question How can you sort ais of dctionaries based on aspcifc key using lamba function? Choices 1+ Usingthe song method ané specifying the key with the lamba function, + Using the sored functon and specifying the key wt the lambda functon. + Ualng theater) method an pectin the Key wt the lambda function. + Lambée functions canner be used fr serig, Explanation | Lets say youhave alist of dctonaies Ike this: cael owes alse a's 3 roe Woy Sage 28), (Cras hare, age), "Now we want to srt this st of scionaries based on the'age key: ease alae) + This uses lambda function asthe key argument tothe sorted function. + Toe mba function takes an element bic it a tonay i thi ase) and returns he value ascciated with theage key. +The sorted furetion then sorts thelist of cetonaries based on these wakes, ouput ip oa ese ool somal 1OBOTH ESE ameGBecAKIOVIL Tsp sms, 242°u Furtoal Parana t- Boss natesipr -Cdeb c rae tos ae (erat alee, ap" eh (rma heat age, Higher Order Funetion + function that rtume another function ip oa ese ool somal 1OBOTH ESE ameGBecAKIOVIL Tsp mi — | Hraner Order Factions cet ger 09t): EP ote, 1 pte dt amen ' cos = soLe9t6) er sot sypeton.) e050) ¥ Quiz ¥ Question Conese te following scenano: You want to generate a furction thet can calculate the aque of any number. How can you achieve tie Choices ip oa ese ool somal 1OBOTH ESE ameGBecAKIOVIL Tsp wos m5, 242° Furtoral Pogranningt- Boss natesipr9 -Cdeb + © Bysetng the inner funtion tors he np tthe power of? and etuing the ne orton + Bymutipyng the input numberby Zin the outer fnction, + By viding the input rarber by 29 tre outer urtin. + By subtracting 2 fom the inp umber nthe inner faction, Explanation et art lealate sare = raat snore etn) et) Pincteent) ouput + geverate_square_functon Ie» higherotder function because tetas another funeton equate) The are ure f tum, ca be sed to cael he sauare of any number + When we cll generate square functin(), tune the square funtion. We then asign ths furetion oa variable (eateaate_squre inthis cae) and use ke any oer function + Tis approach allows to create specialized functions on hey based onthe logic defined inthe hgherorder function tea wayof ‘encapsulating behavior andeeating reusable function tha an be taloted to speci needs ¥ Decorators + Thete are higher nde functions that ake anther urtion as pul and add the exta behaviour along wth the fnetioality of he passed tuneton et toa DPeint"ele evryoel ow are you dlne?*) ip oa ese ool somal 1OBOTH ESE ameGBecAKIOVIL Tsp ane sms, 242° Furtoal Parana t- Boss natesipr -Cdeb 000) set 400) Frine(""s8) code at rans before Fine ("ella everyone! How are you din Prine(vwst) ¥ cade tat rans eer fet prety(fie) oF Sener) pet" 30) fact) eine -es0) reu_too = pretty(foo) es fo0() eereesy fe 520) Pein oThs és naring! 1") ip ea eae gol somal 1OBOTHESEameGBecAKIOVIOL Tsp sms, 242°u Furtoal Parana t- Boss natesipr -Cdeb ip oa ese ool somal 1OBOTH ESE ameGBecAKIOVIL Tsp sue

You might also like