Wednesday, November 22, 2023

My #1 productivity hack - Google Calendar default email reminders

I'd like to share a super simple trick, that might or might not work for you - which is essential in my life to organize my personal and work events. All hail Google calendar default email reminder.

If you're juggling a busy schedule like I am, Google Calendar's default email reminders are a game-changer. Seriously, this feature saves me so much hassle. You can customize reminders for all your events, ensuring nothing slips through the cracks.

What I love most is how easy it is to set up. Just head to settings, tweak your preferences, and voila! You can get an email nudge whenever you need it, whether it's a day before or just an hour prior to your event.

Trust me, relying on these default reminders has made my life a whole lot easier. No more frantic manual setting of reminders for each event—I just set it and forget it. It's like having a personal assistant keeping track of everything for me.

Honestly, it's not just a notification feature; it's a productivity hack. It frees up mental space, letting me focus on what I need to do without constantly worrying about missing important stuff. Give it a shot; you'll thank yourself later!

Setting default email reminders proves immensely beneficial in managing a busy schedule. It eradicates the need to manually set reminders for each event, saving time and ensuring no event goes unnoticed. The simplicity of configuring these reminders simplifies the organizational process for users, fostering a more efficient workflow.

This feature fosters productivity by reducing the mental load of remembering every event. Users can rely on the system to prompt them at designated times, allowing them to focus on the tasks at hand without worrying about missing appointments or deadlines.

The only downside of this approach is that your email client can get pretty chatty. But that also means, you're living a busy life! So all in all, this is my best approach to manage all of these events (e.g. birthdays are there too haha), but if you know a better way to do it, let me know!

Learn more about it here!

Wednesday, October 4, 2023

My VSCode TypeScript prototyping setup

 .vscode/launch.json

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug TS",
"program": "${workspaceFolder}/index.ts",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": ["${workspaceFolder}/out/**/*.js"]
}
]
}

tsconfig.json

{
"compilerOptions": {
"target": "ES2022",
"module": "commonjs",
"outDir": "out",
"sourceMap": true
}
}

Sunday, March 5, 2023

JavaScript infinite streams

This post was heavily inspired by this post: Infinite Data structures in Javascript  (author: Dimitris Papadimitriou)

After reading the article, I also wanted to have the bare minimum, and most simple implementation of a Stream - and surprisingly, it is very easy (in terms of code), but also, kinda complicated. Kind of the sweetspot I really like to learn about!

In this implementation, we add a filter method to the stream object, which takes a predicate function pred and returns a new stream object with only the elements that pass the predicate. If the current value passes the predicate, then the resulting stream will include the current value and the result of calling filter on the next property. Otherwise, the resulting stream will only include the result of calling filter on the next property. If there is no next property, then the emptyStream object is returned.

The resulting mappedStream is a stream of strings with the same structure as the original stream, but with each element converted to a string. The resulting filteredStream is a stream with only the elements that are greater than 1.


What's wrong with eager evaluation? (And why you should use fp-ts/Fluture for that)

Eager evaluation in JavaScript Promises can lead to several problems. Eager evaluation means that a Promise's executor function is executed immediately when the Promise is created, rather than when it is needed later on. Here are some potential issues with eager evaluation:

Increased resource usage: If the Promise's executor function performs a resource-intensive operation, eager evaluation can cause unnecessary resource usage. For example, if the Promise is used to fetch data from a server, eager evaluation would mean that the fetch operation is performed immediately, even if the data is not needed until later.

  • Unnecessary blocking: If the Promise's executor function performs a blocking operation (such as a long-running loop), eager evaluation can cause unnecessary blocking of the main thread. This can lead to unresponsive user interfaces and other performance issues.
  • Wasted work: If the Promise's executor function performs work that is not needed (for example, if it fetches data that is never used), eager evaluation can result in wasted work and unnecessary network traffic.
  • Race conditions: Eager evaluation can also lead to race conditions, where multiple Promises are created but only one of them is needed. This can result in unnecessary resource usage and can make code harder to reason about.

To avoid these problems, it's generally better to use lazy evaluation with Promises. In lazy evaluation, the Promise's executor function is only executed when the Promise is needed (for example, when it is passed to a then() method or when it is awaited in an async/await function). This approach allows for more efficient use of resources and can help prevent performance issues.

The fp-ts library provides several abstractions and functions that can help avoid the problems associated with eager evaluation in Promises. For example, the type Task is essentially a Promise wrapped in a function, allowing you to control when it's executed, therefore it's considered "lazy".

import { task } from 'fp-ts/lib/Task'

const fetchTask = task(() => fetch('https://example.com/data'))

If you call fetchTask(), only then will your HTTP call be executed.

Fluture

There are also other viable options, like Fluture, which is a Future implementation in Javascript. I'm not aiming to discuss Future-s here, but it might be mentioned, that it is a monadic interface that is lazy by it's nature - in a way it's similar to Promise, but more functional, with it's advantages.

OPENAPI discriminators

 OpenAPI discriminators are a feature of the OpenAPI Specification (formerly known as Swagger) that allows you to define alternative schemas for different values of a property.

In other words, you can use a discriminator to specify different schema definitions based on the value of a property. For example, you may have an "animal" schema with a discriminator property called "type". The "type" property could have possible values of "cat" or "dog". You could then define different schema definitions for the "cat" and "dog" types.

Discriminators are especially useful for modeling inheritance in your API schema. By using discriminators, you can define common properties and behaviors for a group of related schemas, while still allowing for differences in their specific implementations.

The discriminator property is used to select the appropriate schema definition for a given value. The value of the discriminator property is typically found in the JSON payload of a request or response.

Let's say we want to define a schema for different types of animals, with common properties like "name" and "age", but with different properties based on the type of animal. We can use a discriminator to define alternative schemas based on the value of the "type" property:

components:

  schemas:

    Animal:

      type: object

      properties:

        name:

          type: string

        age:

          type: integer

      discriminator:

        propertyName: type

      required:

        - type

    

    Dog:

      allOf:

        - $ref: '#/components/schemas/Animal'

        - type: object

          properties:

            breed:

              type: string

    

    Cat:

      allOf:

        - $ref: '#/components/schemas/Animal'

        - type: object

          properties:

            color:

              type: string

In this example, we define a base schema called "Animal" with properties for "name" and "age", and a discriminator called "type" that will be used to select the appropriate schema for each animal. We also define two alternative schemas called "Dog" and "Cat", which extend the "Animal" schema with additional properties specific to dogs and cats.
If a request or response payload includes an animal with a "type" property of "dog", the schema definition for "Dog" will be used. Similarly, if the "type" property is "cat", the "Cat" schema will be used.
Here's an example payload for a dog:
{
  "type": "dog",
  "name": "Fido",
  "age": 3,
  "breed": "Golden Retriever"
}
And here's an example payload for a cat:
{
  "type": "cat",
  "name": "Whiskers",
  "age": 2,
  "color": "tabby"
}

In this way, discriminators allow you to define flexible, extensible API schemas that can adapt to a variety of use cases.

AsyncGenerator

In TypeScript, an AsyncGenerator is a special type of generator function that can be used to asynchronously generate a sequence of values.

Like a regular generator function, an AsyncGenerator is defined using the function* syntax, but with the addition of the async keyword before the function keyword:

async function* myAsyncGenerator() {  // ...}

An AsyncGenerator function can use the yield keyword to return values one at a time, just like a regular generator. However, because it is an asynchronous function, it can also use the await keyword to pause execution until an asynchronous operation completes before continuing to the next yield statement.

Here is an example of an AsyncGenerator that asynchronously generates a sequence of random numbers:

async function* randomNumbers(count: number): AsyncGenerator<number> {  for (let i = 0; i < count; i++) {    // Wait for a random number to be generated asynchronously    await new Promise(resolve => setTimeout(resolve, 1000));  }}

To use an AsyncGenerator, you can call it like a regular generator and iterate over the values it generates using a for-await-of loop:

async function printRandomNumbers(count: number) {
  for await (const number of randomNumbers(count)) {
    console.log(number);
  }
}
This will asynchronously generate and print count random numbers, one per second.