-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples for package manager and spreadsheet (#13)
The examples help me to understand how to define the API. Later, they can be used for tests and for documentation. See pull request #13
- Loading branch information
Showing
6 changed files
with
77 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
struct Package: Identifiable, Hashable { | ||
let id: String | ||
let name: String | ||
let author: String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import DependencyGraphs | ||
|
||
// A -> B means that A depends on B | ||
typealias PackageGraph = DependencyGraph<Package> | ||
|
||
extension PackageGraph { | ||
func findAllDependencies(of package: Package) -> [Package] { | ||
// Traverse the graph starting from package | ||
// and collect vertices visited | ||
[] | ||
} | ||
|
||
// This should be in `DependencyGraph`. | ||
func insert(package: Package, withDependencies dependencies: [Package]) { | ||
// Call `insert` method in `DependencyGraph`. | ||
// `insert` also inserts the dependencies and checks | ||
// if circular dependencies would be introduced. | ||
} | ||
|
||
func findDependents(of package: Package) -> [Package] { | ||
// Traverse graph in reverse direction | ||
// and collect packages. | ||
[] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
struct Cell: Identifiable, Hashable { | ||
let id: Coordinate | ||
let computedValue: Int | ||
let expression: String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
struct Coordinate: Hashable { | ||
let row: Int | ||
let column: Int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import DependencyGraphs | ||
|
||
// A -> B means that A depends on B | ||
typealias SpreadSheet = DependencyGraph<Cell> | ||
|
||
extension SpreadSheet { | ||
// This should be in `DependencyGraph`. | ||
func insert(cell: Cell, withDependencies dependencies: [Cell]) { | ||
// Call `insert` method in `DependencyGraph`. | ||
// `inserts` also inserts the dependencies of `cell` | ||
// and checks if circular dependencies would be introduced. | ||
} | ||
|
||
func update(cell: Cell, with newExpression: String) { | ||
compute(expression: newExpression) | ||
// Call `update` method in `DependencyGraph` | ||
// to update `expression` and `computedValue` for `cell`. | ||
// `update` also updates the dependencies of `cell` | ||
// and checks if circular dependencies would be introduced. | ||
// We need to recompute values of cells that depend on `cell`. | ||
// Traverse graph with dfs starting from `cell` | ||
// and update their values. | ||
} | ||
|
||
func compute(expression: String) -> Double { | ||
// Parse expression and get cell names C it depends on. | ||
// Find C in `vertices` dictionary. | ||
// We don't need to recompute value of C. | ||
// Compute expression. | ||
0.0 | ||
} | ||
|
||
// This should be in `DependencyGraph`. | ||
// There is no domain specific logic here. | ||
func remove(cell: Cell) {} | ||
} |