<commit_before>//: Playground - noun: a place where people can play

import UIKit

<commit_msg>finish closure course<commit_after>//: Playground - noun: a place where people can play

import UIKit

// Global and nested functions are actually special kinds of closures.

/*
 {(parameters) -> return type in
    statements
 }
 */

// default values of parameter cannot be used.

/* The Sort Function */

func backwards(s1: String, s2: String) -> Bool {
    return s1 > s2
}

let names = ["Cc", "Aa", "Ee", "Bb", "DD"]
var reversed = names.sort(backwards)

// Write the sorting closure inline, using closure expression syntax:
reversed = names.sort({(s1: String, s2: String) -> Bool in
    return s1 > s2
})

// the keyword in is used to introduce the beginning of the closure's body

/* Inferring type from context */

reversed = names.sort({s1, s2 in return s1 > s2})
print(reversed)


/* Inferring type from context */
reversed = names.sort({s1, s2 in s1 > s2})
print(reversed)

/* Shorthand argument names */
// Swift automatically provides argument names like $0, $1, $2 and so on
reversed = names.sort({$0 > $1})
print(reversed)

/* Operator functions */
// The shortest way to write the closure expression above:
reversed = names.sort(>)

{'fraction_non_alphanumeric': 0.12077294685990338, 'fraction_numerical': 0.016908212560386472, 'mean_word_length': 3.638059701492537, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '2469671', 'n_tokens_mistral': 404, 'n_tokens_neox': 382, 'n_words': 156}