Skip to content

Instantly share code, notes, and snippets.

@joshuakfarrar
Last active November 30, 2022 04:43
Show Gist options
  • Save joshuakfarrar/cdc43da25ee65ace176bb769e02e5385 to your computer and use it in GitHub Desktop.
Save joshuakfarrar/cdc43da25ee65ace176bb769e02e5385 to your computer and use it in GitHub Desktop.
// source: https://stackoverflow.com/questions/71608818/purely-functional-immutable-doubly-linked-list-in-javascript
var head = (function CircularList(previous, item, ...items) {
if (!items.length) return { item, next: () => head, previous };
var rest = CircularList(() => current, ...items); // Recursion
var current = { ...rest, previous };
return { ...rest, item, next: () => current };
})(() => head, 1, 2, 3, 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment