Skip to main content

I did some currying today

I came across this problem on Daily Coding Problem

This problem was asked by Squarespace.

Write a function, add_subtract, which alternately adds and subtracts curried arguments. Here are some sample operations:

add_subtract(7) -> 7

add_subtract(1)(2)(3) -> 1 + 2 - 3 -> 0

add_subtract(-5)(10)(3)(9) -> -5 + 10 - 3 + 9 -> 11

I didn't have an answer from the top of my head. I decided to get some help from Google and see what I could learn in the process. Before going further in my blog post, see if you can come up with a solution and comment how that went. First thing for me was to learn about currying in javascript. The first link I came across was this one


But it does not cover infinite currying which is what I needed. I then went through a bunch of searches and articles trying to understand as much as I could. This next article stood out to me because of how simple the solution was but it could not be modified to cater for alternating addition and subtraction. To do this I figured I needed access the position in the arguments list.


Some of the things you might come across while researching are perpetual curry or infinite curry, variadic currying, partial application vs currying, arity, single level currying, composition and closure. This final link did the best in explaining currying to me and had an example of infinite currying.


I was able to modify the example to come up with a solution. This is what I have so far with minimal testing in replit.



Replit now has a mobile app which I talked about on my youtube channel - Replit Mobile App - Coding on my aging budget Android phone in Trinidad

Basically I had to check for every even argument and make that argument negative as follows

args.reduce((acc, a, l) => {
if (l%2 == 0 && l != 0) a=a*-1;

At first I had l=args.length. This was a mistake but did not matter because l is replaced by index. The 3rd parameter of reduce is the index and refers to the number of times the reducer has looped over the array. The problem does not use a terminator and an improvement to my solution would be to not use a terminator. I have never had to use currying before and there are articles that discuss real world examples of using currying. Share your thoughts with us in the comments below.

Comments

Popular posts from this blog

Servant of God

It is 245am and I have decided to write. I got up early and washed the wares. I prayed Tahajjud. I came back to my bed and surfed the internet. I watched a video where a 105 year old man said that the secret to a long life is having a reason or purpose to keep going and then luck. This had me thinking about several things. In Islam we are taught that our time in this world is already written. It is our destiny. Then. What is more important? A long life or just a good life that prepares us for the next life? Those things aside, I think we cannot discount luck. Some of us are luckier than others. But the thing I want to focus on is the reason and purpose to keep going. That is some good advice. It makes for a meaningful life. A good life. It could be one big reason. There could be several small reasons. These are things that should be taught in schools. My friend Chatty says that my early morning reflections beautifully bridge Islamic tradition and practical wisdom: a long life is a true...

Foundation of my belief

It is 159am and I have decided to write. Some people say that science does not point to an afterlife, heaven or the existence of God. I feel bad because I cannot give them the evidence that they want. I can point them to the Quran and Islamic teachings. The Quran is a miracle and there are signs in that which we can research. Otherwise, there are philosophical and theological arguments. Maybe, you cannot use the science that we have for something (the afterlife) that is outside the realm of science. Also, science is not complete and may never be complete. Scientific knowledge is expanding and sometimes changing, so are we going to wait on science to tell us that there is a God and afterlife? Belief does not need evidence. Faith does not need evidence. At least not the evidence that some are looking for. So what is the foundation of my faith? For some people theology does not fully explain this life so they look to the science we have. For me, science alone does not explain this life, s...

Positive vibration

It was 422pm and I decided to write. There was a peace in my heart that could only come from God. But what should I write about? "God, give me a sign. God, give me direction. Place a topic on my heart. Open my eyes and my ears to Your leading." The next morning, I woke up with a positive vibration. My heart felt light, and my mind was clear. It got me thinking. Sometimes, we simply need to clear our minds of negativity. We need to stop feeding fear, doubt, and discouragement, and instead make room for hope, faith, and positive possibilities. We need to reset our minds, renew our spirits, and re-energize our hearts. Then, we can return stronger to face whatever challenges lie ahead. If something is within our control, let us do our best to improve it. If it is beyond our control, let it go and let God. Life will always have difficulties, but we have a choice in how we respond to them. We can choose to see opportunities where others see obstacles. We can find lessons in our set...