4

I am writing module that other people can use in their code. It's a calendar. I want to allow users to set locale parameter so that they have month names and other data in language of their choice.

I could create Calendar class that accepts locale, startOfWeekDay as arguments. Methods of that class will those properties so there is no need to pass them to each method.

export class Calendar {
    static WEEKDAYS = {sunday: 0, ...};

    constructor(year, month, options = {locale: 'en-US', startOfWeekDay: Calendar.WEEKDAYS.sunday}) {
        this.locale = options.locale;
        this.startOfWeekDay = options.startOfWeekDay;
        this.calendar = this.getCalendar(year, month);
    }

    /**
     * returns True or False whether given day is first day of week
     */
    isFirstDayOfWeek(day) {
        // depends on this.startOfWeekDay
    }

    /**
     * returns True or False whether given day is last day of week
     */
    isLastDayOfWeek(day) {
        // depends on this.startOfWeekDay
    }

    /**
     * returns list of objects representing a calendar
     * [ {day: 1, month: 1, year: 2019, weekDayName: 'Sunday' }, ... ]
     */
    getCalendar(year, month) {
        // depends on this.locale to return correct weekDayName
    }

    /**
     * returns list of week day names: ['Sunday', 'Monday', ... ];
     */
    getWeekDays() {
        // depends on this.locale & this.startOfWeekDay
    }
}

If I would want to rewrite it using more functional approach, what first comes to mind is to ask user to pass locale to each function that depends on it:

const WEEKDAYS = {sunday: 0, ...};
export function isFirstDayOfWeek(startOfWeekDay, day) {}
export function isLastDayOfWeek(startOfWeekDay, day) {}
export function getCalendar(year, month, options = {locale: 'en-US'}) {}
export function getWeekDays(options = {locale: 'en-US', startOfWeekDay: WEEKDAYS.sunday}) {}

What functional programming concepts would you use?

4
  • 1
    If your question is "How do you write functions in a referentially-transparent way without passing locale and other required data," the answer is "you can't." Commented Aug 25, 2019 at 18:18
  • 5
    Too busy to write a full-fledged answer at the moment. The standard solution to ambient configuration data is the Reader Monad. Commented Aug 25, 2019 at 18:50
  • 1
    "I want to use a functional style but don't want to pass configuration as a parameter, how else do I pass configuration?" - the question is, why would you wish to? The whole point of functional programming is to cause all variation in execution to occur only as the consequence of explicit parameters provided. Commented Aug 30, 2019 at 16:49
  • I would like to reduce repetition (e.g passing locale as a parameter to multiple functions) both for the consumer of the module and in the code of the module itself. Is it considered a normal practice to pass config parameters explicitly to keep functions pure? Commented Sep 1, 2019 at 5:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.