+ 1

Hello friends, Please help me know#Why Pure functions are hard to write with an example if possible??? Thanks for your responses

Regarding Pure functions from Python

29th May 2018, 10:43 AM
Harshal Simpi
Harshal Simpi - avatar
2 Respuestas
+ 3
Hi Harshal In all programmes we normally deal with state and behaviour. We normally associate state with variables, and behaviour with functions. Good programming practice will separate behaviour from state management. This allows us to write code that follows the DRY (Do not Repeat Yourself) principle, but also means the code is very easy to test debug and compose. Pure functions have the following characteristics:- -Given the same input will always produce the same output -Have no side effects (does not affect state) Writing pure functions is not really that difficult if you take the above into account, but challenges the following:- 1. Lazy programming such as use of Global variables, functions that do not return anything which means you cant compose them, and deep nesting of code which should be broken down into smaller functions. 2. Taking time to understand more advanced concepts such as currying, partial application, recursion and using functions as arguments to other functions. 3. Looking at problems in declarative way rather than procedural, eg using Lambdas and Map, Reduce and Filter instead of loops. To support pure functions you don't necessarily need a functional programming language like Haskell or LISP, any language that has first class functions such as JavaScript or Python will do. Rather than give you examples, perhaps you can look at this thread where you cans see a lot of one liners to reverse a string. Many show how you can compose smaller functions (which are pure) to solve the String reverse problem. https://www.sololearn.com/Discuss/1196847/assignment-reverse-string
29th May 2018, 12:36 PM
Mike Choy
Mike Choy - avatar
+ 1
Thanks Mike for helping me out
29th May 2018, 12:55 PM
Harshal Simpi
Harshal Simpi - avatar