Skip to content

Instantly share code, notes, and snippets.

View dkokic's full-sized avatar

Draško Kokić dkokic

View GitHub Profile
@dkokic
dkokic / fibonacci function
Created February 20, 2022 09:24
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)
@dkokic
dkokic / prime generator
Last active August 29, 2015 14:12
purely functional implementation of prime numbers generator
def from(n: Integer): Stream[Integer] = n #:: from(n+1)
def sieve(input: Stream[Integer]): Stream[Integer] = input.head #:: sieve(input.tail.filter(_ % input.head != 0))
def primes = sieve(from(2))
primes take 10 print