Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

JavaScript and Functional Programming: A Dive into Pure Functions, Currying, and Functional Composition

Posted on Oct 4 • Originally published at Medium Have you ever felt the thrill of discovering a new way to approach problems in your coding journey? Or the satisfaction of writing code that’s not just functional, but also elegant and efficient? If you’re nodding along, or even if you’re just curious, you’re in for a treat. Today, we’re embarking on a delightful exploration into the realm of functional programming in JavaScript.Whether you’re a seasoned developer or a curious newbie, this guide on pure functions, currying, and functional composition promises to sprinkle a touch of magic on your JavaScript endeavors. So, grab your favorite beverage, get comfy, and let’s dive deep into the mesmerizing world of functional programming!Functional Programming (FP) is like giving someone a set of instructions to follow, step by step, without any distractions or deviations.Imagine you have a toy robot. You give it a set of commands like “move forward”, “turn left”, or “pick up”. Every time you give it the same command, it does the exact same thing. It doesn’t suddenly decide to dance or sing; it just follows your instructions precisely.In FP, our code is like that robot. We write functions (sets of instructions) that:Always do the same thing with the same input. If you tell it to add 2 and 3, it will always give you 5. It won’t suddenly give you 6 or 4.They don’t have memories of their own. They don’t remember past actions or results. Every time you run them, it’s like they’re doing their task for the first time.Don’t mess with other stuff. They mind their own business. If a function’s job is to calculate something, it won’t secretly change something else in your program.This way of coding makes it easier to predict what your program will do, find mistakes, and understand how different parts of your program work together. It’s like having a well-trained robot that always follows your commands without any surprises!A cornerstone of functional programming, pure functions have two main characteristics:Deterministic: For a given input, a pure Function will always produce the same output.No Side Effects: A pure function does not cause any observable changes outside its scope. This means it doesn’t modify any external variables, log to the console, or perform any other side effects.Benefits:Predictability: Since the output is solely determined by its input, testing and debugging become much easier.Reusability: Pure functions can be safely reused in different parts of your application without unintended consequences.Example:The add function is pure because for the same values of a and b, it will always return the same result, and it doesn't modify any external state.For example, if you give it the numbers 2 and 3 to add, it will always return 5.Currying is about breaking a function that takes multiple inputs into a series of functions that take one input at a time.Currying is like breaking down a multi-step task into smaller tasks that you do one at a time.Imagine you have a machine that makes colored stickers. Normally, you’d tell it both the shape and color at once, like “Make a blue star.” But with currying, you’d first tell it “Prepare to make a star,” and then, “Now, color it blue.”In JavaScript, instead of a function that takes two inputs at once:With currying, you break it down:Now, you can first set the shape:const starSticker = makeSticker('star');And later, set the color:Benefits:Partial Application: Currying allows you to fix a certain number of arguments for a function, creating a new function. This can be useful when you have functions where some arguments remain constant throughout the application.Enhanced Function Composition: Currying can make it easier to compose functions. When functions are curried, you can easily chain them together, leading to more readable and maintainable code.Increased Modularity: By breaking down functions into unary functions (functions that take one argument), you can reuse and combine them in various ways, leading to more modular and DRY (Don’t Repeat Yourself) code.Laziness: Curried functions support lazy evaluation. This means you can delay the evaluation of the function until all its arguments are available.Improved Code Clarity: When functions are curried, it can make the intention of the code clearer. Each step or function call becomes a descriptive action, making the code more self-documenting.Flexibility: Currying provides flexibility in the order of supplying arguments. You can provide arguments to a function in any sequence, and it will return a new function waiting for the remaining arguments.Functional composition is the process of combining two or more functions to produce a new function. It’s like a pipeline where the output of one function becomes the input of the next.Example:Benefits:Readability: Instead of nested function calls, you have a clear sequence of data transformations.Modularity: You can easily rearrange, add, or remove steps in the pipeline.Imagine you’re getting ready for a party.Currying is like choosing your outfit piece by piece. First, you pick a shirt, then pants, then shoes, and so on. You’re breaking down the process of getting dressed into smaller steps, and you handle one step at a time.Functional Composition is like combining different activities to get ready. For example, first, you take a shower, then you get dressed, and finally, you put on some perfume. Here, you’re combining different activities (or functions) to complete the whole process of getting ready.In terms of coding:Currying:Functional Composition:In essence:Currying is about how a single function receives its arguments.Functional Composition is about how multiple functions are combined together.Both are powerful concepts in functional programming, but they address different aspects of how functions are structured and used.Functional programming offers a fresh perspective on solving problems and organizing code. By embracing concepts like pure functions, currying, and functional composition, JavaScript developers can write more robust and maintainable applications. While it might seem daunting at first, the benefits in terms of code quality and predictability are well worth the effort. So, the next time you sit down to code, consider giving functional programming in JavaScript a try!A Big Thank You! 🌟Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse An Architect - Oct 1 Leetcode - Sep 9 Imamuzzaki Abu Salam - Oct 2 PGzlan - Sep 10 Once suspended, jaimaldullat will not be able to comment or publish posts until their suspension is removed. Once unsuspended, jaimaldullat will be able to comment and publish posts again. Once unpublished, all posts by jaimaldullat will become hidden and only accessible to themselves. If jaimaldullat is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Jaimal Dullat. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag jaimaldullat: jaimaldullat consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging jaimaldullat will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

JavaScript and Functional Programming: A Dive into Pure Functions, Currying, and Functional Composition

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×