photo by Bessi Story Time Today, I had a quirky function that needed writing. It involved far too many inputs and a lot more math (for a single function) than I would have liked. Even though the math itself is actually just basic trigonometry. After writing a few lines of code, it became quite obvious that this function would not be maintainable. I was reminded of something I recently heard and fell in love with: A Programming language is not for the computer, it is for people. Another along the same lines: If the computer doesn’t run it, it’s broken. If people can’t read it, it will be broken. Soon. ❤ Anyway, back to the function. I had set my mind towards refactoring this thing. It was a typical refactor, break out some responsibilities, sprinkle in a little partial application, add some currying and a wrap it all up with function composition. What I was left with was some very elegant and easily maintainable code. I reflected upon how rare it is for code to be written this way, which is why I am sharing today. A single article is nowhere near enough to cover all the functional programming techniques within. And we won’t be deep diving into any of them. My hope is that this will pique your curiosity with FP and interest you into starting your own journey. Disclaimer: high level overview Requirements So what am I doing anyway? I am trying to calculate the probability of an event occurring. I know that as the of this event increases so does the of the event occurring. velocity probability If the is below 1,000, then the of occurrence is always 50%. If the is above 9,000, the jumps to a maximum of 85%. It is easier to visualize with a chart, so here’s a chart. velocity probability velocity probability And I want this all encapsulated into a single function. This could be solved with a single line trigonometry, but it would look like a regex or machine language and today, . we are writing code for people The Beginning First I created a calibration object to transform the ’s to the ’s. X Y const calibration = {x: { min: 1000, max: 9000 },y: { min: 50, max: 85 },} Then I wrote this code to return the min if the value is below the threshold and the max if it is above. almost Y Y But I really don’t like statements. If you find yourself curious as to why, then check out the article about it… if _Thinking functionally has opened my mind about programming. It has given me a greater depth of understanding of code…_hackernoon.com Rethinking JavaScript: The if statement And using and . Since I need to use both I plan on just use Ramda’s method, which does exactly this. there’s a better way of doing this Math.min Math.max clamp Feature Scaling Feature scaling, also known as , is a machine learning technique that I think will apply well here. normalizing The gist of feature scaling is to scale the input (the ) to a value between . Then I will do the reverse and de-scale the number into the scale. X [0, 1] Y , just take note of , specifically how it calls two functions, and . does the same, just in reverse. You should ignore the majority of the code below normalize scale R.clamp denormalize Function Composition Function composition is one of my favorite FP tools. It’s a technique to create new functions using existing functions. and are great candidates for function composition and we can use Ramda’s method to transform them into this: normalize denormalize pipe Not vastly different, but we’re only half way there. We’ll reduce this more in the Currying & Partial Application section. To continue learning more about Function Composition, check out my previous article on Function Composition. _Function composition has got to be my favorite part of functional programming. I hope to provide you with a good real…_hackernoon.com Functional JavaScript: Function Composition For Every Day Use. Currying & Partial Application Currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. — WikiPedia Currying allows us to easily partially apply arguments to and . scale R.clamp , min and max should have also been broken out and separated. Though, it didn’t add any utility since both min and max will be supplied at the same time and it would have complicated the examples. Sometimes it’s better to go with the spirit of the law and not the letter of the law :) Note: While I didn’t follow the 1 argument rule here The Function Building the function is now super easy. This is because we have written all of the logic already. All that is left is to compose and like this: normalize denormalize That’s it! Super simple. Finally All together it will look like this: Observations: Every function is an and not a . The functions return their value. expression block immediately Every function is , meaning given the same inputs it will produce the same output. idempotent always Every function is a . The output it calculated from the inputs. pure function only The majority our functions , they are simply of existing functions. do not contain any new code composed Extra Credit The simplicity is more than just cosmetic. The transformation I am working with is linear, but actually it’s probably closer to easeOutSine or easeOutCubic pictured below. Because we have broken the code out into small pieces, it would be unbelievably simple to enhance this function. We simply have to add our transition into the pipeline. Here’s a few ways this could be done: R.pipe Disclaimer I do understand that “easier” is subjective and a lot of people who are unfamiliar with a functional programming style are probably having their minds melted right now. You do have to know how to program functionally before . Though once you do, this kind of style becomes the obvious and correct choice. The key here being . you will also feel this way once you know This is also not meant to be a tutorial. It is just a small lens into the world of functional javascript. Summary Hey, you made it all the way to the end! Thanks for listening to my rantings. I truly mean that. Today I have demonstrated how to take functions that have been broken down into their single responsibilities can be easily composed into new functions. I know I didn’t cover nowhere near enough, but I do have a lot of other articles that go into more details. functional programming _Read the latest stories written by Joel Thoms on Medium. Computer Scientist and Technology Evangelist with 21 years of…_medium.com Latest stories written by Joel Thoms - Medium I know it’s a small thing, but it makes my day when I get those follow notifications on Medium and Twitter ( ). Or if you think I’m full of shit, tell me in the comments below. @joelnet Cheers!