<commit_before>// Playground - noun: evlovation for fibonacci based on wwdc2014 session 404, advanced swift

import Cocoa

/*
* 1, recursive with non-memoize, middle across can't be adapted
*
func fibonacci_nonmemoize(n: Int) -> Double {
return n < 2 ? Double(n) : fibonacci_nonmemoize(n-1) + fibonacci_nonmemoize(n-2)
}
fibonacci_nonmemoize(20)
*/

/*
* 2, recursive with manual memooize
*/
var fibonacci_manualmem = Dictionary<Int, Double>()
func fibonacci_mm(n: Int) -> Double{
    if let result = fibonacci_manualmem[n] {
        return result
    }
    let result = n < 2 ? Double(n) : fibonacci_mm(n-1) + fibonacci_mm(n-2)
    fibonacci_manualmem[n] = result
    return result
}
fibonacci_mm(20)
<commit_msg>llv22 - for swift of fibonacci recursive of cloure<commit_after>// Playground - noun: evlovation for fibonacci based on wwdc2014 session 404, advanced swift

import Cocoa

/*
* 1, recursive with non-memoize, middle across can't be adapted
*
func fibonacci_nonmemoize(n: Int) -> Double {
return n < 2 ? Double(n) : fibonacci_nonmemoize(n-1) + fibonacci_nonmemoize(n-2)
}
fibonacci_nonmemoize(20)
*/

/*
* 2, recursive with manual memoize
*/
var fibonacci_manualmem = Dictionary<Int, Double>()
func fibonacci_mm(n: Int) -> Double{
    if let result = fibonacci_manualmem[n] {
        return result
    }
    let result = n < 2 ? Double(n) : fibonacci_mm(n-1) + fibonacci_mm(n-2)
    fibonacci_manualmem[n] = result
    return result
}
fibonacci_mm(20)

/*
* 3, recursive with auto memoize for result to avoid type inference
*/
func memoizeAuto<T: Hashable, U>( body: (T)-> U ) -> (T)->U {
    var memo = Dictionary<T, U>();
    return {
        x in
        if let q = memo[x] {return q}
        let r = body(x)
        memo[x] = r
        return r
    }
}
/* error: variable used within its own initial value
let fibonacciAuto = memoizeAuto{
    fibonacciAuto, n in
    n < 2 ? Double(n) : fibonacciAuto(n-1) + fibonacciAuto(n-2)
}
*/
var fibonacciAuto: (Int) -> Double = { Double($0) }
fibonacciAuto = memoizeAuto {
    (n : Int) in n < 2 ? Double(n) : fibonacciAuto(n-1) + fibonacciAuto(n-2)
}
fibonacciAuto(20)


/*
* 4, recursive with auto memoize for result to avoid type inference
*/
func memoizeBodyResult<T: Hashable, U>( body: ((T)->U, T)->U ) -> (T)->U {
    var memo = Dictionary<T, U>()
    var result: ((T)->U)!
    result = {
        x in
        if let q = memo[x] {return q}
        let r = body(result, x)
        memo[x] = r
        return r
    }
    return result
}
let fibonacci = memoizeBodyResult{
    (fib, x: Int) in x < 2 ? Double(x) : fib(x-1) + fib(x-2)
}
fibonacci(20)
{'fraction_non_alphanumeric': 0.13026819923371646, 'fraction_numerical': 0.021455938697318006, 'mean_word_length': 3.197749196141479, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2361002', 'n_tokens_mistral': 1028, 'n_tokens_neox': 966, 'n_words': 271}