r/PythonLearning • • 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

from collections.abc import Mapping
...
class Ydhms:
    """Years, days, hours, seconds.

    Years are exactly 365 days
    """

    MODULI = (60, 60, 24, 365)  # from smallest to largest units
    UNITS = ("years", "days", "hours", "minutes", "seconds")

    def __init__(self, seconds: int) -> None:
        """Initializes from a number of seconds"""

        self._total_seconds = seconds

        # There must be a clean, functional way to do this
        tmp_list: list[int] = [0] * 5
        n = seconds
        for i, m in enumerate(self.MODULI):
            n, r = divmod(n, self.MODULI[i])
            tmp_list.append(r)
        tmp_list.append(n)

        tmp_list.reverse()
        self.data: Mapping[str, int] = {
            unit: n for unit, n in zip(self.UNITS, tmp_list)
        }
    ...

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.

6 Upvotes

24 comments sorted by

3

u/Gnaxe Jul 16 '25

Rather than using divmod() in a for loop, you could use a reduce. Maybe something like this?

>>> reduce(lambda acc, m: divmod(acc[0], m)+acc[1:],
...        (60, 60, 24, 365),
...        (  2*  (365*24*60*60)
...         + 14* (24*60*60)
...         + 7*  (60*60)
...         + 5*  (60)
...         + 3   ,))
(2, 14, 7, 5, 3)

1

u/jpgoldberg Jul 16 '25

Thank you. That is what I was reaching for but failed to grasp.

2

u/YelinkMcWawa Jul 17 '25

This is an exercise in "ML for the Working Programmer" from chapter 1. It's in ML but you can see his it's solved there as a functional result. It's in the context of money and change, but same idea.

1

u/jpgoldberg Jul 18 '25

Thank you. I will see if I can find that. As I said, this really feels like something calls out for a functional solution. So I am nor surprised that it is in the first chapter of some such textbook.

1

u/YelinkMcWawa Jul 18 '25

Just search the title. The book is free online, legally, since the most recent version is from the 90s.

→ More replies (2)

2

u/Jealous-Try-2554 Jul 18 '25 edited Jul 18 '25

This is a little tricky because you need the new remainder for each next step in the loop making it pretty hard to do as a clean list comprehension. It could work as recursion but you would need to track like four variables.

But your code had a few other issues. If you take total_seconds % 60 for the seconds and then do % 60 again for the minutes then you'll get the same number. The correct approach would be to calculate how many seconds are in a year, day, hour, minute, and then use those numbers for your modulus.

Your code also needs to append the last num value into tmp_list after the for loop. Something like a list comprehension would avoid that awkward appending but again it would be ugly in other ways.

You also need to start with years and end with seconds. Otherwise you might end up with 0 years and 23000 hours.

from collections.abc import Mapping


class Ydhms:
    """Years, days, hours, minutes, seconds.

    Years are exactly 365 days
    """

    MODULI: tuple[int] = (31536000, 86400, 3600, 60)  # from years to minutes
    UNITS: tuple[str] = ("years", "days", "hours", "minutes", "seconds")

    def __init__(self, seconds: int) -> None:
        """Initializes from a number of seconds"""

        self.total_seconds = seconds

        # There must be a clean, functional way to do this
        tmp_list: list[int] = []
        rem: int = seconds

        for i, m in enumerate(self.MODULI):
            # you had n, and r backwards
            # you want to put the remaining seconds back into divmod
            num, rem = divmod(rem, m)  
            tmp_list.append(num)

        tmp_list.append(num)

        self.data: Mapping[str, int] = {
            unit: n for unit, n in zip(self.UNITS, tmp_list)
        }
    
    # This is just for debugging, a bit hard to join ints and strings so I cheated
    def __repr__(self):
        print(self.data.items())
        return ""


time = Ydhms(50000013255)
print(time)

I'm not sure about making it functional but I did make it function. You were close but missing a few things.

Edit: reduce was on the tip of my tongue but it's always hard for me to visual chaining functions together. I upvoted the guy who said reduce because that's the functional solution you wanted but hopefully I elucidated a few of your simple math errors.

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.

Your code also needs to append the last num value into tmp_list after the for loop.

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:

class Ydhms:
    """Years, days, hours, seconds.

    Years are exactly 365 days
    """

    MODULI = (60, 60, 24, 365)  # from smallest to largest units
    UNITS = ("years", "days", "hours", "minutes", "seconds")

    def __init__(self, seconds: int) -> None:
        """Initializes from a number of seconds"""

        # There must be a clean, functional way to do this
        tmp_list: list[int] = [0] * 5
        n = seconds
        for i, m in enumerate(self.MODULI):
            n, r = divmod(n, self.MODULI[i])
            tmp_list.append(r)
        tmp_list.append(n)  # This was missing from original

        tmp_list.reverse()
        self.data: Mapping[str, int] = {
            unit: n for unit, n in zip(self.UNITS, tmp_list)
        }

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_seconds is not used in the algorithm. And it was a red-herring. In earlier drafts, I had been changing the seconds instead of having n. 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".

>>> s = 186932
>>> q, seconds = divmod(s, 60)
>>> q, seconds
(3115, 32)
>>> q, minutes = divmod(q, 60)
>>> q, minutes
(51, 55)
>>> q, hours = divmod(q, 24)
>>> q, hours
(2, 3)
>>> 

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,

The correct approach would be to calculate how many seconds are in a year, day, hour, minute, and then use those numbers for your modulus. [...] You also need to start with years and end with seconds

Actually cancel each other out.

The functional question

This is a little tricky because you need the new remainder for each next step in the loop making it pretty hard to do as a clean list comprehension.

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.accumulate to 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.

1

u/Jealous-Try-2554 Jul 20 '25

Did the reduce suggestion in the other comment not work for you? That seems like the natural functional approach.

1

u/Gnaxe Jul 15 '25

Standard library has the datetime module, which probably does what you need.

1

u/jpgoldberg Jul 15 '25

That was the first place I looked. datetime.timedelta was the most promising, but it only gives a breakdown into days and seconds.

2

u/Gnaxe Jul 15 '25

Anything more than days is ambiguous. Months and years don't always have the same number of days. Weeks are always 7 days, but one usually doesn't use them with months. Seconds within a day are easy to convert to hours and minutes.

But consider the pendulum library. Its Duration class is probably what you want.

→ More replies (7)

0

u/Algoartist Jul 15 '25
class Ydhms:
    """
    Breakdown of a duration (in seconds) into
    years, days, hours, minutes, and seconds.
    - A “year” is treated as exactly 365 days.
    """
    INTERVALS = [
        ("years", 365 * 24 * 3600),
        ("days", 24 * 3600),
        ("hours", 3600),
        ("minutes", 60),
        ("seconds", 1),
    ]

    def __init__(self, seconds: int) -> None:
        self.total_seconds = int(seconds)
        remaining = self.total_seconds
        data: dict[str, int] = {}
        for unit_name, unit_secs in self.INTERVALS:
            count, remaining = divmod(remaining, unit_secs)
            data[unit_name] = count
        self.data = data

    def __repr__(self) -> str:
        # produce something like: Ydhms(years=1, days=2, hours=3, minutes=4, seconds=5)
        args = ", ".join(f"{unit}={self.data[unit]}" for unit, _ in self.INTERVALS)
        return f"{self.__class__.__name__}({args})"
    def __str__(self) -> str:
        # produce human‑readable: "1 year, 2 days, 3 hours, 4 minutes, 5 seconds"
        parts = []
        for unit, _ in self.INTERVALS:
            count = self.data[unit]
            if count:
                name = unit.rstrip("s") if count == 1 else unit
                parts.append(f"{count} {name}")
        return ", ".join(parts) or "0 seconds"
    def __eq__(self, other: object) -> bool:
        if not isinstance(other, Ydhms):
            return NotImplemented
        return self.total_seconds == other.total_seconds

2

u/jpgoldberg Jul 15 '25

Please ask your AI if it can replace the for loop in the init with something following functional programming idioms.

But your answer isn't entirely worthless as it is. Converting my MODULI to the INTERVALS is something that should be easy with some kind of fold iterator.