Anonymous
Not logged in
Talk
Contributions
Create account
Log in
WikiKnowledgeBase
Search
Editing
Technology/Software Development/General knowledge/Programming Fundamentals/Functional Programming
(section)
From WikiKnowledgeBase
Namespaces
Page
Discussion
More
More
Page actions
Read
Edit
History
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Functional Programming == '''Definition''' Functional Programming (FP) is a programming paradigm that treats computation as the evaluation of mathematical functions. It focuses on writing pure functions that produce output based solely on their input, without modifying external state or producing side effects. '''Key Concepts''' Let's delve into some fundamental concepts of functional programming: 1. '''Pure Functions:''' - Pure functions are functions that, given the same input, always produce the same output and do not modify external state. - They have no side effects, such as modifying global variables or performing I/O operations. - Pure functions are deterministic and easier to reason about and test. 2. '''Immutable Data:''' - Functional programming discourages mutable data, where values can be changed after they are created. - Instead, immutable data is preferred, where values remain constant throughout their lifetime. - Immutable data promotes safer concurrency, easier debugging, and simpler program flow. 3. '''Higher-Order Functions:''' - Higher-order functions are functions that can take other functions as arguments or return functions as results. - They enable composition and abstraction, allowing developers to create powerful and flexible code. 4. '''Recursion:''' - Recursion is a technique where a function calls itself to solve a problem by breaking it down into smaller sub-problems. - Functional programming encourages the use of recursion instead of iterative loops for iteration. '''Code Example''' Here's an example in JavaScript that demonstrates the usage of functional programming concepts: <pre> // Pure function example function square(number) { return number * number; } // Higher-order function example function doTwice(func, value) { return func(func(value)); } // Immutable data example const numbers = [1, 2, 3, 4, 5]; const doubledNumbers = numbers.map((number) => number * 2); // Recursion example function factorial(n) { if (n === 0) { return 1; } else { return n * factorial(n - 1); } } console.log(square(5)); // Output: 25 console.log(doTwice(square, 3)); // Output: 81 console.log(doubledNumbers); // Output: [2, 4, 6, 8, 10] console.log(factorial(5)); // Output: 120 </pre> In this example, we define a pure function `square` that calculates the square of a number. We also define a higher-order function `doTwice` that takes a function and a value, and applies the function twice to the value. The `map` function is used to create a new array `doubledNumbers` by doubling each number in the original `numbers` array. Lastly, we demonstrate recursion by implementing a factorial function that calculates the factorial of a given number.
Summary:
Please note that all contributions to WikiKnowledgeBase may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
My wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation
Navigation
Main page
Random page
Categories
Help about MediaWiki
Wiki tools
Wiki tools
Special pages
Page tools
Page tools
User page tools
More
What links here
Related changes
Page information
Page logs