exercism / clojure

Exercism exercises in Clojure.
https://exercism.org/tracks/clojure
MIT License
160 stars 155 forks source link

New concept: deriving new functions from existing functions (partial, comp, memoize) #588

Open JaumeAmoresDS opened 10 months ago

JaumeAmoresDS commented 10 months ago

Would there be interest in creating a concept dedicated to function tools such as comp, partial, memoize, that facilitate deriving new functions from existing ones?

github-actions[bot] commented 10 months ago

Hello. Thanks for opening an issue on Exercism. We are currently in a phase of our journey where we have paused community contributions to allow us to take a breather and redesign our community model. You can learn more in this blog post. As such, all issues and PRs in this repository are being automatically closed.

That doesn't mean we're not interested in your ideas, or that if you're stuck on something we don't want to help. The best place to discuss things is with our community on the Exercism Community Forum. You can use this link to copy this into a new topic there.


Note: If this issue has been pre-approved, please link back to this issue on the forum thread and a maintainer or staff member will reopen it.

bobbicodes commented 10 months ago

Reopening this as per: https://forum.exercism.org/t/new-concept-deriving-new-functions-from-existing-functions-partial-comp-memoize/7746

JaumeAmoresDS commented 10 months ago

@bobbicodes, I just created a PR from my fork, but I don't see it in the list of PRs in the main branch. This is the first time I do a PR from a fork and I'm not sure if I did it correctly? I also tried to select the reviewer but couldn't unfortunately...

The PR includes only the new concept, not the exercises, at this point in time... should I wait until I include exercises and create a new PR?

bobbicodes commented 10 months ago

We could merge the concept now to get that out of the way... I reopened the PR so I'll comment over there.

JaumeAmoresDS commented 10 months ago

Hi @bobbicodes, I was just thinking about possible exercises, and I have some that might be appropriate. However, I realize that basically anything that one can do with partial, can be done with an anonymous function, at least for solving those exercises. A similar thing happens with comp, where one can use a direct composition of functions rather than using this construct, and also with juxtp. They all seem to add syntactic sugar and be a convenience that one can opt to not use (there might be situations where they are strictly required but I can't think of them).

My question is: to what extent is this important? Is there a way to enforce the use of those specific functions when testing an exercise, e.g, through the analyzers / representers that are discussed by the Exercism team?

JaumeAmoresDS commented 10 months ago

@bobbicodes In order to be specific, I include here a very rough description of the exercises.

We have Mary and John, who do frequent trips by bike:

'(800, 500, 300)
  1. Use partial to make this calculation any time they want to plan a new trip.
  2. Use comp to update their average speed and then apply the updated speed to estimate the total time it will take to do a new trip they have in mind.
  3. Use juxtp to evaluate different trips that they are considering, in terms of how long it would take each of one. The input would be a list of distances for each trip, and the output the total time required.

The idea is that comp is typically used (AFAIK) with a single input from the second function onwards, so that we need to use partial (or an anonymous function) as part of comp. I think a similar thing happens with juxtp, but I'm not sure about that.

While estimating the total times for each potential trip, they may want to play a bit with the trips, and add, modify, or remove destinations several times. Sometimes they might end up with the original trip, and they don't want to have to annotate the previously computed distances. Let's imagine that calculating the time it takes to go from one destination to another one is computationally expensive. We can use memoize to compute those times, so we save computational cost when requiring those results again. Then we can have a function that, given a potentially long list of cities, calculates the total estimated time for that trip. They would be evaluating many potential lists (one for each trip, where they have many possible trips in mind, and for each trip they would be playing with many possibilities, i.e., changing the list of cities many times and calculating their total time). We can also use memoize, not only for the pairwise times, but for the total time, in case the list is repeated while exploring the possibilities.

A final task might be a group of people that bet on which couple will be making the same trip faster, based on the average speed of each couple. Here we could use map and partial, where map takes a list of partial functions, one for each couple, and the total distance of the trip. Again, anonymous functions could be used here instead of partial ones...

Please let me know your thoughts! Thank you very much for your help.

bobbicodes commented 10 months ago

...I realize that basically anything that one can do with partial, can be done with an anonymous function, at least for solving those exercises. A similar thing happens with comp...

It's funny that you say this because I actually never use comp or partial in my own code, and whenever I see them I compulsively have to change them to anonymous functions because they make so much more sense to me! I've heard others say similarly, so I've never been motivated to use them. The one exception I've seen is when using comp with transducers, but that's obviously out of scope for this exercise.

juxt is different, though, there are some very useful patterns with it... years ago I even did a recurring blog series called just-juxt where every day I posted a new solution using juxt, but theh Tl;DR is it comes in handy any time you want to generate a sequence of pairs.

So far we haven't enforced the usage of the concepts so it's been on the student to get the intended lesson out of the exercise. For example, in the exercise teaching the Java Character class, I've seen students solve it using regex. This could be addressed with an exercise-specific analyzer, but for now I wouldn't worry about it.

JaumeAmoresDS commented 10 months ago

Thanks @bobbicodes. In that case, I will go ahead and start working on these exercises, if you think the description of them that I sent to you makes sense! Thanks again for your feedback.