Merge pull request #4 from 0xLeif/feature/flatten

Feature/flatten
This commit is contained in:
Zach Eriksen 2021-03-01 20:21:58 -06:00 committed by GitHub
commit f91b21ccfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 11 deletions

View File

@ -21,48 +21,78 @@ public extension Variable {
init() {
self = .void
}
init(bool: Bool) {
self = .bool(bool)
}
init(int: Int) {
self = .int(int)
}
init(float: Float) {
self = .float(float)
}
init(double: Double) {
self = .double(double)
}
init(string: String) {
self = .string(string)
}
init(set: Set<Variable>) {
self = .set(set)
}
init(array: [AnyHashable]) {
self = .array(array.map({ $0.variable }))
}
init(dictionary: [AnyHashable: AnyHashable]) {
let variable = Variable.dictionary([:])
if case .dictionary(var variable) = variable {
dictionary.forEach { (key, value) in
variable[value.variable] = value.variable
}
}
self = variable
}
}
public extension Variable {
var flatten: Variable {
guard case .array(let value) = self else {
return self
}
guard !value.isEmpty else {
return .array([])
}
guard value.count > 1 else {
return value[0]
}
let flattenedArray = value.map(\.flatten)
var flatArray = [Variable]()
for variable in flattenedArray {
if case .array(let values) = variable {
flatArray.append(contentsOf: values.map(\.flatten))
} else {
flatArray.append(variable)
}
}
return .array(flatArray)
}
}
public extension Variable {
/// Update the Variable's Value
/// - Returns: A new Variable with the type of T
@ -153,7 +183,7 @@ extension Variable: ExpressibleByDictionaryLiteral {
}
}
extension AnyHashable {
public extension AnyHashable {
var variable: Variable {
if let variable = self as? Variable {
return variable