Skip to main content

Creating callable variants of functions by currying in Ruby - code snippet showing how to avoid scope problems

While coding a project in Ruby, I was creating some variants of a function by currying. Initially, I simply created the curried variants as variables, but quickly ran into scope problems where I couldn't then call any of the variants from within other functions. This was because the scope of the variant was the same as the scope of a local variable of that name. 

So I created a code snippet as a demo for myself of what I should have done instead, which is to define the curried variant as another function. This new function then has the same scope as any function I would create and not the (more limited) scope of a local variable.

Of course, in certain situations defining it as a local variable is more desirable - for example if I was instead planning to use the variant as a variable that could be passed around. This is as opposed to using it solely as a callable function, which is what I ultimately desired.

def myfun(stuff, num1, num2)
  if stuff == true then
    num1 + num2
  else
    num1 - num2
  end
end

def myfun1
  self.method(:myfun).curry.(true, 1)
end

myfun_false = self.method(:myfun).curry.(false)
# but the problem is that myfun_false has a scope that's only valid here
# so it can't be used within another function. This is a huge problem for
# the use case where it's designed to be called elsewhere, which is the
# majority of my use cases.
# It may be less of an issue for the situation where the intent is to
# pass the curried function itself around as a variable, instead
# of actually calling it.


puts myfun_false.(20, 18)
# 2

# By contrast, myfun1 can be called anywhere - here, or within another function.
# Remember that any function resulting from a curried function should be
# called via .call or just the syntactic sugar of '.' Both are illustrated
# below
puts myfun1.(12)
# 13
puts myfun1.call(18)
# 19

Above is the code snippet to remind myself of what *I* generally want to do, which is the approach taken in the myfun1 function. The other approach, myfun_false which results in a local variable, is also shown for comparison's sake.

Popular posts from this blog

Why Solar Geoengineering is a Bad Idea: 3 reasons understandable to both the general and scientific communities

Solar geoengineering is the idea of limiting the amount of sunlight that reaches the earth, with the aim of limiting global warming. Here I will show 3 reasons why it's a really bad idea: 1. Photosynthesis requires sunlight, don't limit it - it's one of the few things pulling carbon dioxide out of the atmosphere Photosynthesis is one of the very few ways we have to remove carbon dioxide from the atmosphere. As an added bonus it's done automatically by plants, requiring no effort on our part. It requires light in order to work. To do it, plants take in sunlight, carbon dioxide and water, and they produce oxygen. These plants are literally removing carbon dioxide from our atmosphere, but they require light for it to work.  We should not do anything that could possibly limit the amount of photosynthesis occurring; limiting the amount of light hitting the planet is likely to decrease photosynthesis.  2. We should not be limiting the generation of solar power Solar electri...

How to center images horizontally using Grav

I've been playing around a bit with Grav. I was posing the question to myself: for the relatively simple use-cases I'm dealing with, could it possibly work for my purposes as an alternative to ProcessWire?  The problem I was initially dismayed to find that Grav uses Markdown as its editor, which does not offer native support for horizontal centering of anything (text or images). However, Grav offers some tweaks that help make it easier to do specific things you might commonly want to do. I tried writing a sample article, and I found that one of the hardest things to do was to center an image horizontally. And horizontal centering of images is something I would typically do in most of the articles I would write. So the lack of easy horizontal centering is a highly significant drawback IMHO (most people do want to center images in an article!) However, this issue is made up for by other things in Grav: the relative speed, ease and flexibility of custom theming and built-in suppor...

Good and bad uses of AI as it currently stands

The good Recently, the Photos app on my Android phone automatically curated some of my photos into a "highlights" album for me. I thought this was a fantastic idea. I love this use of AI - it does something quickly and easily that would otherwise take a lot of human time.  The only downside was that the AI included a close-up pic I'd taken of a spot on the skin behind my husband's ear so he could see it. Now, if this AI had instead been for a self-driving car, then too bad, we likely would've had a terrible wreck at this point - endangering myself, any passengers, and other drivers on the road. But since this application of AI for photo selection did not have any life-threatening consequences, I was all for it. The bad I've said before, and I'll say it again, that AI as it currently stands should not be used for self-driving purposes and I have explained clearly why. Indeed, I believe that AI should not be used for any purpose that may have life-threaten...

About Me

My photo
Vera
I'm a wife and mother. I don't have any formal computer science qualifications, or any religious qualifications. I have a PhD in biochemistry. This photo is of me, but is confusing for AI.