r/PythonLearning • u/jpgoldberg • Jul 15 '25
My disFunctional brain can't make this functional
Update since cross posting to r/functionalprogrammming
I had originally posted this "how to do this functionally" question in r/PythonLearning, but later sought the help of people from r/functionalprogramming. With that cross posting, I am asking for an illustration of how do to this functionally in general. I do not need a Python-specific solution.
Another note for the FP people. It's fine if you want to recommend alternatives to Python in addition to showing how to solve this in those alternatives or at least helping to arrive at such a solution.
Background
I wanted to write a quick little thing for something I thought would be in a standard library but couldn't find (represent a number of seconds in terms of years, days, hours, minutes, and remaining seconds. It turned out that I struggled with something that I feel should have been easy.
It works, but ...
There must be a more functional and better way to create the instance data from the data at hand.
Update, there was a bug that had it fail to collect years. Thank you u/Jealous-Try-2554
...
"""Years, days, hours, seconds.
Years are exactly 365 days
"""
= # from smallest to largest units
=
"""Initializes from a number of seconds"""
=
# There must be a clean, functional way to do this
: = * 5
=
, =
: =
...
Also, if there is a standard library or even conventional way to do this, that would be great. But I still want to turn this into an opportunity improve my ability to use functional styles.
Solutions so far
u/AustinVelonaut has provided a solution in Haskell, using MapAccum, and pointing out that that can be constructed using runState.
u/Gnaxe pointed out that the third-party excellent pendulum Python library does what I want. So I could just import its Interval class instead of rolling my own.
u/YelinkMcWawa pointed out that this problem (with respect to making change in coins) is used in ML for the Working Programmer by Lawrence Paulson. It is in section 3.7 of chapter 3 of the second edition. The solution presented in the chapter uses recursion, but the exercises might invite other approaches. This suggests to me that cleanest way to express this in Python would be with recursion, but I believe that Python does not optimize tail recursion.
View all comments
Show parent comment
1
u/jpgoldberg Jul 18 '25
Thank you! My code was indeed broken, but not nearly as broken as you say. It's just that I took another approach.
That was the only mistake. (And I had forgotten about
append). My tests didn't catch the error because I failed to create a test that would give me more than 0 years. With that fixed, it now passes a richer set of tests.So this does work:
But your comments indicate that I should spell out what I have done more clearly. (If you want to skip this, the FP comments and questions are further below.)
First of all,
total_secondsis not used in the algorithm. And it was a red-herring. In earlier drafts, I had been changing thesecondsinstead of havingn. Sorry that its presence threw you off.My "backwards" arrangement of moduli and its non-cumulative nature is where I very much do things differently than your approach, but mine does work.
I developed it because before hand I doing stuff like this by "hand".
which gave me 2 day, 3 hours, 55 minutes, and 32 seconds.
The script I wrote was aimed to replicate that process. So what you saw as two errors,
Actually cancel each other out.
The functional question
Yeah. That is where I got stuck. (In my approach, I need to keep the new quotient instead of the new remainder, but it is the same problem.).
I have a feeling that I could use
itertools.accumulateto create a sequence of tuples, and then process that sequence to generate the actual results, but I can't seem to turn that vague notion something specific enough to play with.Again, my frustration is that this feels like there should be a natural FP solution (whether in Python or some other language), but I am not able to grasp that solution.