Chain

0.3.0

⛓ Enum to chain functions together
0xLet/Chain

What's New

Chain Head

2021-03-03T02:38:20Z

Head Functions

runHead

func runHead(
        name: String? = nil,
        input: Variable? = nil,
        logging: Bool = false
    ) -> Variable

dropHead

func dropHead() -> Chain?

Chain ⛓

Chain of events

Use an enum to structure the call stack.

public indirect enum Chain {
    case end
    case complete(E.Function?)
    case link(E.Function, Chain)
    case background(E.Function, Chain)
    case multi([Chain])
}

Example Code

let output = Chain.link(
    .out { "First" },
    .link(
        .in {
            print("Value: \($0)")
        },
        .multi(
            [
                .multi(
                    [
                        .end,
                        .end,
                        .end
                    ]
                ),
                .link(
                    .out { "Link" },
                    .link(
                        .out { "Last" },
                        .complete(
                            .inout { value in
                                guard case .string(let value) = value else {
                                    XCTFail()
                                    return .void
                                }
                                
                                return  .string("\(value) !!!")
                            }
                        )
                    )
                )
            ]
        )
    )
)
.run(name: "ChainTests-testOutput")

Normal Chain Output

(lldb) po output
 Variable
   array : 2 elements
     0 : Variable
      - string : "First"
     1 : Variable
       array : 2 elements
        - 0 : E.Variable.void
         1 : Variable
           array : 2 elements
             0 : Variable
               array : 3 elements
                 0 : Variable
                   array : 1 element
                    - 0 : E.Variable.void
                 1 : Variable
                   array : 1 element
                    - 0 : E.Variable.void
                 2 : Variable
                   array : 1 element
                    - 0 : E.Variable.void
             1 : Variable
               array : 2 elements
                 0 : Variable
                  - string : "Link"
                 1 : Variable
                   array : 2 elements
                     0 : Variable
                      - string : "Last"
                     1 : Variable
                       array : 1 element
                         0 : Variable
                          - string : "Last !!!"

Flattened Chain Output

(lldb) po output.flatten
 Variable
   array : 8 elements
     0 : Variable
      - string : "First"
    - 1 : E.Variable.void
    - 2 : E.Variable.void
    - 3 : E.Variable.void
    - 4 : E.Variable.void
     5 : Variable
      - string : "Link"
     6 : Variable
      - string : "Last"
     7 : Variable
      - string : "Last !!!"

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

  • E0.3.0
Last updated: Thu Oct 17 2024 16:55:37 GMT-0900 (Hawaii-Aleutian Daylight Time)