A demo of a function that creates another function
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
Jari Haavisto b048f4ad54 initial commit 2 years ago
src initial commit 2 years ago
README.md initial commit 2 years ago

README.md

Time Since

A demo of a function creating another function

Preliminary Code

function sleep(milliseconds: number) {
    const timeWhenStarted = Date.now()
    let timeNow = Date.now()
    while (timeNow < timeWhenStarted + milliseconds) {
        timeNow = Date.now()
    }
}

First Version

const timeSinceInitializer1 = () => {
    const initialTime = Date.now()
    const timeSince = () => {
        const now = Date.now()
        const diff = now - initialTime
        const message = 'It has been ' + diff + ' ms since initialization'
        console.log(message)
    }
    return timeSince
}

The function timeSinceInitializer1 sets the initialTime variable to the time when the initializer function was executed. Then it creates a timeSince function and returns that function.

Notice that the timeSince function is not executed, only created, so the code inside that function does not get run yet. When the timeSince gets executed, it sets the now variable to the time when the timeSince function is executed. Then it calculates the difference between these two times, and logs the information.

Usage:

const timeSince1 = timeSinceInitializer1()
sleep(1000)
timeSince1() // prints "It has been 1000 ms since initialization" 

Second Version

const timeSinceInitializer2 = (includeInitialTime: boolean) => {
    const initialTime = Date.now()
    const timeSince = () => {
        const now = Date.now()
        const diff = now - initialTime
        let message = 'It has been ' + diff + ' ms since initialization'
        if (includeInitialTime) {
            const initialTimeString = new Date(initialTime).toISOString()
            message = message + ` (${initialTimeString})`
        }
        console.log(message)
    }
    return timeSince
}

In the second version we include a parameter to the initializer function. It is a boolean parameter. If it is set to true, the message in the timeSince function will include the initial time. If not, the message will be as it was in the first version.

The noteworthy thing here is that by using a parameter in the initializer function, we can create different kinds of result functions. When they have been created, the behaviour can not be changed.

Usage:

const timeSince = timeSinceInitializer2(true)
sleep(1000)
timeSince() // prints "It has been 1000 ms since initialization (2023-11-24T09:09:34.295Z)" 

Third Version

const timeSinceInitializer3 = (includeInitialTime) => {
    const initialTime = Date.now()
    const timeSince = (messagePart1 = 'It has been ', messagePart2 = ' ms since initialization') => {
        const now = Date.now()
        const diff = now - initialTime
        let message = messagePart1 + diff + messagePart2
        if (includeInitialTime) {
            const initialTimeString = new Date(initialTime).toISOString()
            message = message + ` (${initialTimeString})`
        }
        console.log(message)
    }
    return timeSince
}

In the third version we add even more complexity by having parameters in both the initializer function and the resulting function.

The parameter in the initializer function dictates if the initial time is shown. This behaviour can not be changed when the function has been created.

The parameters in the timeSince function tell what the printed message will be. This allows us to control the behaviour of the resulting function after creation.

Usage:

const timeSince3 = timeSinceInitializer3(true)
sleep(1000)
timeSince3('Holy Cow! It has been ', ' ms since the initialization!')
// prints "Holy Cow! It has been 1001 ms since the initialization! (2023-11-24T09:13:54.369Z)"