Skip to content

Releases: 0xLeif/Fork

0.6.0

17 Aug 23:46
443010d

Choose a tag to compare

What's Changed

Full Changelog: 0.5.0...0.6.0

0.5.0

17 Aug 23:37
f444a5c

Choose a tag to compare

What's Changed

Full Changelog: 0.4.0...0.5.0

0.4.0

17 Aug 23:09
dd2be76

Choose a tag to compare

What's Changed

Full Changelog: 0.3.0...0.4.0

0.3.0

11 Aug 00:13
0436cd4

Choose a tag to compare

What's Changed

Full Changelog: 0.2.0...0.3.0

ForkedActor Example

actor TestActor {
    var value: Int = 0
    
    func increment() {
        value += 1
    }
}

let forkedActor = ForkedActor(
    actor: TestActor(),
    leftOutput: { actor in
        await actor.increment()
    },
    rightOutput: { actor in
        try await actor.fork(
            leftOutput: { await $0.increment() },
            rightOutput: { await $0.increment() }
        )
        .act()
    }
)

try await forkedActor.act()

let actorValue = await forkedActor.actor.value

XCTAssertEqual(actorValue, 3)

0.2.0

04 Aug 23:55
5de8b76

Choose a tag to compare

What's Changed

  • Update merge closure to be async throwing by @0xLeif in #1

Full Changelog: 0.1.0...0.2.0


Fork

Using a single input create two separate async functions

What is Fork?

Fork allows for a single input to create two separate async functions that return potentially different outputs. Forks can also merge their two functions into one which returns a single output.

The word "fork" has been used to mean "to divide in branches, go separate ways" as early as the 14th century. In the software environment, the word evokes the fork system call, which causes a running process to split itself into two (almost) identical copies that (typically) diverge to perform different tasks.
Source

Basic usage

import Fork

Basic Example

let fork = Fork(
    value: 10,
    leftOutput: { $0.isMultiple(of: 2) },
    rightOutput: { "\($0)" }
)
        
let leftOutput = try await fork.left()
let rightOutput = try await fork.right()

XCTAssertEqual(leftOutput, true)
XCTAssertEqual(rightOutput, "10")
        
let mergedFork: () async throws -> String = fork.merge(
    using: { bool, string in
        if bool {
            return string + string
        }
            
        return string
    }
)
        
let output = await mergedFork()

XCTAssertEqual(output, "1010")

Service Example

let service = Fork(
    value: AppConfiguration(),
    leftOutput: { configuration in
        Fork(
            value: AuthService(configuration),
            leftOutput: { authService in ... },
            rightOutput: { authService in ... }
        )
    },
    rightOutput: { configuration in
        ...
    }
)

let mergedServiceFork: async throws () -> AppServices = service.merge(
    using: { authFork, configurationOutput in
        guard let services = authFork.merged(...) else { return }
            
        services.logger.log(configurationOutput)
            
        return services
    }
)

0.1.0

03 Aug 00:45

Choose a tag to compare

Fork

Using a single input create two separate async functions

What is Fork?

Fork allows for a single input to create two separate async functions that return potentially different outputs. Forks can also merge their two functions into one which returns a single output.

The word "fork" has been used to mean "to divide in branches, go separate ways" as early as the 14th century. In the software environment, the word evokes the fork system call, which causes a running process to split itself into two (almost) identical copies that (typically) diverge to perform different tasks.
Source

Basic usage

import Fork

Basic Example

let fork = Fork(
    value: 10,
    leftOutput: { $0.isMultiple(of: 2) },
    rightOutput: { "\($0)" }
)
        
let leftOutput = try await fork.left()
let rightOutput = try await fork.right()

XCTAssertEqual(leftOutput, true)
XCTAssertEqual(rightOutput, "10")
        
let mergedFork: () async throws -> String = fork.merge(
    using: { bool, string in
        if bool {
            return string + string
        }
            
        return string
    }
)
        
let output = await mergedFork()

XCTAssertEqual(output, "1010")

Service Example

let service = Fork(
    value: AppConfiguration(),
    leftOutput: { configuration in
        Fork(
            value: AuthService(configuration),
            leftOutput: { authService in ... },
            rightOutput: { authService in ... }
        )
    },
    rightOutput: { configuration in
        ...
    }
)

let mergedServiceFork: async throws () -> AppServices = service.merge(
    using: { authFork, configurationOutput in
        guard let services = authFork.merged(...) else { return }
            
        services.logger.log(configurationOutput)
            
        return services
    }
)