Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Functional programming
(section)
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!
=== Imperative vs. functional programming === The following two examples (written in [[JavaScript]]) achieve the same effect: they multiply all even numbers in an array by 10 and add them all, storing the final sum in the variable <code>result</code>. Traditional imperative loop: <syntaxhighlight lang="javascript"> const numList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let result = 0; for (let i = 0; i < numList.length; i++) { if (numList[i] % 2 === 0) { result += numList[i] * 10; } } </syntaxhighlight> Functional programming with higher-order functions: <syntaxhighlight lang="javascript"> const result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] .filter(n => n % 2 === 0) .map(a => a * 10) .reduce((a, b) => a + b, 0); </syntaxhighlight>Sometimes the abstractions offered by functional programming might lead to development of more robust code that avoids certain issues that might arise when building upon large amount of complex, imperative code, such as [[off-by-one error]]s (see [[Greenspun's tenth rule]]).
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)