<commit_before>//
//  DictionaryExtensions.swift
//  Dove
//
//  Created by <NAME> on 10/08/2016.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

public extension Dictionary where Key: Hashable {
	public func transform<Key, Value>(_ transform: (Element) -> (Key, Value)) -> [Key: Value] {
		var result = [Key: Value]()
		
		self.map(transform).forEach({ result[$0] = $1 })
		
		return result
	}
}

public extension Dictionary {
	public func any(_ predicate: (Key, Value) -> Bool) -> Bool {
		return self.count(predicate) > 0
	}
	
	public func all(_ predicate: (Key, Value) -> Bool) -> Bool {
		return self.count(predicate) >= self.count
	}
	
	public func count(_ predicate: (Key, Value) -> Bool) -> Int {
		var count = 0
		
		for (key, value) in self {
			if predicate(key, value) {
				count += 1
			}
		}
		
		return count
	}
	
	public var urlQuery: String {
		let keys = self.keys.flatMap({ String(describing: $0).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) })
		let values = self.values.flatMap({ String(describing: $0).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) })
		
		let result = zip(keys, values)
			.map({ "\($0)=\($1)" })
			.joined(separator: "&")
		
		return "?\(result)"
	}
	
	public var random: (Key, Value)? {
		guard self.count > 0 else {
			return nil
		}
		
		let keys = Array(self.keys)
		let values = Array(self.values)
		let index = Int(arc4random_uniform(UInt32(self.count)))
		
		return (keys[index], values[index])
	}
}

public func unwrap<Key, Value>(_ dictionary: [Key: Value?]) -> [Key: Value] {
	var result = [Key: Value]()
	
	for (key, value) in dictionary {
		if let value = value {
			result[key] = value
		}
	}
	
	return result
}

public func +<Key, Value>(first: [Key: Value], second: [Key: Value]) -> [Key: Value] {<commit_msg>Added method for creating a dictionary of string enums.<commit_after>//
//  DictionaryExtensions.swift
//  Dove
//
//  Created by <NAME> on 10/08/2016.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

public extension Dictionary where Key: Hashable {
	public func transform<Key, Value>(_ transform: (Element) -> (Key, Value)) -> [Key: Value] {
		var result = [Key: Value]()
		
		self.map(transform).forEach({ result[$0] = $1 })
		
		return result
	}
}

public extension Dictionary {
	public func any(_ predicate: (Key, Value) -> Bool) -> Bool {
		return self.count(predicate) > 0
	}
	
	public func all(_ predicate: (Key, Value) -> Bool) -> Bool {
		return self.count(predicate) >= self.count
	}
	
	public func count(_ predicate: (Key, Value) -> Bool) -> Int {
		var count = 0
		
		for (key, value) in self {
			if predicate(key, value) {
				count += 1
			}
		}
		
		return count
	}
	
	public var urlQuery: String {
		let keys = self.keys.flatMap({ String(describing: $0).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) })
		let values = self.values.flatMap({ String(describing: $0).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) })
		
		let result = zip(keys, values)
			.map({ "\($0)=\($1)" })
			.joined(separator: "&")
		
		return "?\(result)"
	}
	
	public var random: (Key, Value)? {
		guard self.count > 0 else {
			return nil
		}
		
		let keys = Array(self.keys)
		let values = Array(self.values)
		let index = Int(arc4random_uniform(UInt32(self.count)))
		
		return (keys[index], values[index])
	}
}

public func instantiate<Target: RawRepresentable, Value>(_ dictionary: [String: Value], _ type: Target.Type) -> [Target: Value] where Target.RawValue == String {
	var result = [Target: Value]()
	
	for (key, value) in dictionary {
		if let target = type.init(rawValue: key) {
			result[target] = value
		}
	}
	
	return result
}

public func unwrap<Key, Value>(_ dictionary: [Key: Value?]) -> [Key: Value] {
	var result = [Key: Value]()
	
	for (key, value) in dictionary {
		if let value = value {
			result[key] = value
		}
	}
	
	return result
}

public func +<Key, Value>(first: [Key: Value], second: [Key: Value]) -> [Key: Value] {
{'fraction_non_alphanumeric': 0.15440079562406764, 'fraction_numerical': 0.012431626056688214, 'mean_word_length': 3.4257425742574257, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 41, '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': '2354833', 'n_tokens_mistral': 1516, 'n_tokens_neox': 1311, 'n_words': 391}