From b048f4ad54f885d2f9c168c48e7e5344d2019416 Mon Sep 17 00:00:00 2001 From: Jari Haavisto Date: Fri, 24 Nov 2023 11:17:26 +0200 Subject: [PATCH] initial commit --- README.md | 103 ++++++++++++++++++++++++++++++++++++++++++++++ src/time_since.ts | 72 ++++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 README.md create mode 100644 src/time_since.ts diff --git a/README.md b/README.md new file mode 100644 index 0000000..5f80038 --- /dev/null +++ b/README.md @@ -0,0 +1,103 @@ +# Time Since + +A demo of a function creating another function + +## Preliminary Code +```ts +function sleep(milliseconds: number) { + const timeWhenStarted = Date.now() + let timeNow = Date.now() + while (timeNow < timeWhenStarted + milliseconds) { + timeNow = Date.now() + } +} +``` + +## First Version + +```typescript +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: +```ts +const timeSince1 = timeSinceInitializer1() +sleep(1000) +timeSince1() // prints "It has been 1000 ms since initialization" +``` + +## Second Version + +```typescript +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: +```ts +const timeSince = timeSinceInitializer2(true) +sleep(1000) +timeSince() // prints "It has been 1000 ms since initialization (2023-11-24T09:09:34.295Z)" +``` + +## Third Version + +```ts +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: +```ts +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)" +``` \ No newline at end of file diff --git a/src/time_since.ts b/src/time_since.ts new file mode 100644 index 0000000..c375219 --- /dev/null +++ b/src/time_since.ts @@ -0,0 +1,72 @@ +/** + * Stops program progress for given amount of milliseconds. + * + * NOTICE: This is NOT a good way to do this! + * It is simple for a demonstration, but should not be used + * in actual programs. + */ +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 +} + +const timeSince1 = timeSinceInitializer1() +sleep(1000) +timeSince1() + + +// 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 +} + +const timeSince2 = timeSinceInitializer2(true) +sleep(1000) +timeSince2() + +// Third version +const timeSinceInitializer3 = (includeInitialTime: boolean) => { + 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 +} + + +const timeSince3 = timeSinceInitializer3(true) +sleep(1000) +timeSince3('Holy Cow! It has been ', ' ms since the initialization!') \ No newline at end of file