RSSAmplifier

The Ian Atha Museum of Internet Curiosities · Jun 13, 2021

Idempotent

0
Sign in to vote or save

Ian Atha · The Ian Atha Museum of Internet Curiosities

The Ian Atha Museum of Internet Curiosities

Published on

Authors
  • Ian Atha is an Athens-based technologist, ex-OpenAI, building at the intersection of craft, code, and civic life.

Idempotent

tl;dr: A function is idempotent if running it once, or n times, has the same effect.

An operation (function) is idempotent if and only if, multiple applications (executions) of it do not change anything beyond the first application (execution).

Consider, console.log('test'), for example.

It isn't idempotent, because multiple applications of it change the state of the screen beyond the first execution.

However, given the following function, idempotent_log('id_0', 'test') is idempotent:

const logged_keys = [];
function idempotent_log(idempotency_key: string, args: any...) {
  if (logged_keys.indexOf(idempotency_key) == -1) {
	console.log(...args);
	logged_keys.push(idempotency_key);
  }
}

I always like to think of idempotency in terms of a "lifetime"1. A lifetime is a property of every datum. Specifically, a datum's lifetime begins when it is created and ends when it is destroyed.

In the aforementioned example, idempotent_log('id_0', 'test') is idempotent within the lifetime of logged_keys.

Lastly, please note that the closure of idempotent_log('id_0', 'test') is ['id_0', 'test', logged_keys, console].

Footnotes

  1. https://doc.rust-lang.org/rust-by-example/scope/lifetime.html

Ian Atha is an Athens-based technologist, ex-OpenAI, building at the intersection of craft, code, and civic life.

Read the original on atha.io

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.