Swiftpack.co - Package - 0xLeif/E.num

E

Swift... but only enums!

Variables

Basic String Example

let text: Variable = .string("Hello, World!")

Basic Dictionary Example

let dictionary: Variable = .dictionary(
	[
		.bool(false): .double(3.14)
	]
)

Array Example

let list: Variable = .array(
	[
		.bool(false),
		.string("False"),
		.dictionary(
			[
				.bool(false): .double(3.14)
			]
		),
		.int(27)
	]
)

Getting Values Example

if case .string(let value) = text {
	print("String: \(value)")
}

if case .array(let value) = list,
   let lastValue = value.last,
   case .int(let number) = lastValue {
	print(number * 99)
}

Functions

Void Function Example

let voidExample = Function.void {
	print("Print Lorem ipsum")
}
// ...
voidExample()

In Function Example

let printString = Function.in { stringValue in
	guard case .string(let value) = stringValue else {
		return
	}

	print(value)
}
// ...
printString(.string("Hello, World..."))

In & Out Function Example

let double = Function.inout { value in
	if case .int(let value) = value {
		return .int(value * 2)
	} else if case .float(let value) = value {
		return .float(value * 2)
	} else if case .double(let value) = value {
		return .double(value * 2)
	} else if case .string(let value) = value {
		return .string("\(value)\(value)")
	}

	return .array([value, value])
}
// ...
print("Double of \(Variable.float(3.14)) is \(double(.float(3.14)))")

Github

link
Stars: 5

Dependencies

Used By

Total: 0

Releases

import E - 2020-09-29 19:58:04

Switched E.num to E for a simple import.

import E

Control - 2020-09-29 19:31:50

Controls

case `if`(Bool, Function)
case `else`(Bool, Function)
case ifElse(Bool, Function, Function)
case loop(ClosedRange<Int>, Function)
case forEach([Variable], Function)
case forever(Function)

Variables

/// Update the Variable's Value
/// - Returns: A new Variable with the type of T
func update<T>(_ closure: (T) -> Variable) -> Self

/// Modify the Variable to be any type of Variable
/// - Returns: A new Variable of any type
func modify<T>(_ closure: (T?) -> Variable) -> Self

func value<T>(as type: T.Type? = nil) -> T?

Example

var count: Variable = .int(0)

let stillIntValue = count
    .update { .string($0 ?? "Hello, World!") } // returns .int(0)
    .update { .int($0 + 27) }

let defaultedStringValue = count.modify { value in
    .string(value ?? "Hello, World!")
}

XCTAssertEqual(count, 0)
XCTAssertEqual(stillIntValue, 27)
XCTAssertEqual(defaultedStringValue, "Hello, World!")

Control.loop(0 ... 5,
             .in { index in
                count = count.update { value in
                    .int(value + (index.value() ?? 0))
                }
             })
    .run()

XCTAssertEqual(count, 15)

ExpressibleByLiteral - 2020-09-29 15:01:43

Literals

extension Variable: ExpressibleByBooleanLiteral { ... }
extension Variable: ExpressibleByIntegerLiteral { ... }
extension Variable: ExpressibleByFloatLiteral { ... }
extension Variable: ExpressibleByStringLiteral { ... }
extension Variable: ExpressibleByArrayLiteral { ... }
extension Variable: ExpressibleByDictionaryLiteral { ... }

Example

let text: Variable = "Hello, World!"

XCTAssertEqual(text, "Hello, World!")

Variables and Functions - 2020-09-29 14:24:11

Variables

case void
case bool(Bool)
case int(Int)
case float(Float)
case double(Double)
case string(String)
case set(Set<Variable>)
case array([Variable])
case dictionary([Variable: Variable])

Variable Example

let text: Variable = .string("Hello, World!")
let list: Variable = .array(
	[
		.bool(false),
		.string("False"),
		.int(27)
	]
)

// ...

if case .string(let value) = text {
	print("String: \(value)")
}

if case .array(let value) = list,
   let lastValue = value.last,
   case .int(let number) = lastValue {
	print(number * 99)
}

Functions

case void(() -> ())
case `in`((Variable) -> ())
case out(() -> Variable)
case `inout`((Variable) -> Variable)

Function Example

let printString = Function.in { stringValue in
	guard case .string(let value) = stringValue else {
		return
	}

	print(value)
}
// ...
printString(.string("Hello, World..."))