Skip to content

Instantly share code, notes, and snippets.

@dkokic
Created February 20, 2022 09:24
Show Gist options
  • Save dkokic/04ebf5d08729e9bb0ab85cf8d69410dd to your computer and use it in GitHub Desktop.
Save dkokic/04ebf5d08729e9bb0ab85cf8d69410dd to your computer and use it in GitHub Desktop.
purely functional implementation of the fibonacci function (including optimisation via memoization)
const fibonacci = (n) => n < 3 ? 1 : fibonacci(n - 1) + fibonacci(n - 2)
const fibonacci = (n, memo = { 1: 1, 2: 1 }) => n in memo ? memo[n] : memo[n] = fibonacci(n - 1) + fibonacci(n - 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment