Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
martindrlik committed Apr 9, 2024
1 parent 83494e2 commit d40171f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 36 deletions.
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
func main() {
bind("union", "", func(a, b *table.Table) *table.Table { return a.Union(b) })
bind("difference", "", func(a, b *table.Table) *table.Table { return a.Difference(b) })
bind("intersection", "", func(a, b *table.Table) *table.Table { return a.Intersection(b) })
bind("natural-join", "", func(a, b *table.Table) *table.Table { return a.NaturalJoin(b) })
exec(parse(os.Args[1:]))
}
Expand Down Expand Up @@ -157,7 +158,13 @@ func usage(err error) {
names := maps.Keys(ops)
slices.Sort(names)
for _, name := range names {
fmt.Printf(" %s: %s\n", name, ops[name].desc)
fmt.Printf(" %s", name)
desc := ops[name].desc
if desc == "" {
fmt.Println()
} else {
fmt.Printf("%s\n", desc)
}
}
fmt.Println("Input:")
fmt.Println(" -fa <file> [-ta <file> ...]: name of file that contains array of tuples")
Expand Down
16 changes: 8 additions & 8 deletions table/intersection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (

func ExampleTable_Intersection() {
u := table.New().Add(
T{"title": "Dune"},
T{"title": "Dune: Part Two", "year": 2024})
T{"title": "Dune: Part One"},
movie("Dune: Part Two", 2024))
v := table.New().Add(
T{"title": "Dune"})
T{"title": "Dune: Part One"})

fmt.Println(box.Table([]string{"title"}, u.Intersection(v).Tuples()...))

// Output:
// ┏━━━━━━━┓
// ┃ title ┃
// ┠───────┨
// ┃ Dune
// ┗━━━━━━━┛
// ┏━━━━━━━━━━━━━━━━
// ┃ title
// ┠────────────────
// ┃ Dune: Part One
// ┗━━━━━━━━━━━━━━━━
}
9 changes: 0 additions & 9 deletions table/naturaljoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,3 @@ func (t *Table) NaturalJoin(u *Table) *Table {
}
return x
}

func (t *Table) hasCommon(u *Table) bool {
for k := range t.Schema() {
if _, ok := u.Schema()[k]; ok {
return true
}
}
return false
}
13 changes: 7 additions & 6 deletions table/restrict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (

func ExampleTable_Restrict() {
movies := table.New().Add(
map[string]any{"title": "Die Hard", "year": 1988},
map[string]any{"title": "The Matrix", "year": 1999},
map[string]any{"title": "Guardians of the Galaxy", "year": 2014},
map[string]any{"title": "Blade Runner: 2049", "year": 2017},
map[string]any{"title": "Dune", "year": 2021})
movie("Die Hard", 1988),
movie("The Matrix", 1999),
movie("Guardians of the Galaxy", 2014),
movie("Blade Runner: 2049", 2017),
movie("Dune: Part One", 2021))

year := func(f func(int) bool) func(tuple map[string]any) bool {
return func(tuple map[string]any) bool {
return f(tuple["year"].(int))
Expand All @@ -36,6 +37,6 @@ func ExampleTable_Restrict() {
// ┠─────────────────────────┼──────┨
// ┃ Guardians of the Galaxy │ 2014 ┃
// ┃ Blade Runner: 2049 │ 2017 ┃
// ┃ Dune │ 2021 ┃
// ┃ Dune: Part One │ 2021 ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━┛
}
38 changes: 26 additions & 12 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ import (
type T = map[string]any

func ExampleTable() {
movies := table.New().Add(
T{"title": "The Matrix", "year": 1999},
T{"title": "Blade Runner: 2049", "year": 2017, "length": 164},
T{"title": "Dune", "year": 2021, "length": 155})
movies := table.New("title", "year", "length").Add(
movie("The Matrix", 1999),
withLength(movie("Blade Runner: 2049", 2017), 164),
withLength(movie("Dune: Part One", 2021), 155))

fmt.Println(box.Table([]string{"title", "year", "length"}, movies.Tuples()...))
fmt.Println(box.Table(movies.SchemaOrder(), movies.Tuples()...))

// Output:
// ┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━┯━━━━━━━━┓
// ┃ title │ year │ length ┃
// ┠────────────────────┼──────┼────────┨
// ┃ The Matrix │ 1999 │ ? ┃
// ┃ Blade Runner: 2049 │ 2017 │ 164 ┃
// ┃ Dune │ 2021 │ 155 ┃
// ┃ Dune: Part One │ 2021 │ 155 ┃
// ┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━┷━━━━━━━━┛
}

func ExampleTable_Add() {
movies := table.New().Add(
T{"title": "The Matrix", "year": 1999},
T{"title": "The Matrix", "year": 1999}) // no duplicate
movie("The Matrix", 1999),
movie("The Matrix", 1999)) // duplicate

fmt.Println(box.Table([]string{"title", "year"}, movies.Tuples()...))

Expand All @@ -44,20 +44,34 @@ func ExampleTable_Add() {
}

func TestContains(t *testing.T) {
movies := table.New().Add(T{"title": "The Matrix", "year": 1999})
matrixMovie := movie("The Matrix", 1999)
movies := table.New().Add(matrixMovie)
moviesBox := box.Table([]string{"title", "year"}, movies.Tuples()...)
if !movies.Tuples().Contains(T{"title": "The Matrix", "year": 1999}) {
if !movies.Tuples().Contains(matrixMovie) {
t.Errorf(
"\nexpected\n%v\nto contain\n%v",
moviesBox,
T{"title": "The Matrix", "year": 1999})
matrixMovie)
}

matrixWithLength := T{"title": "The Matrix", "year": 1999, "length": 136}
matrixWithLength := withLength(matrixMovie, 136)
if movies.Tuples().Contains(matrixWithLength) {
t.Errorf(
"\nexpected\n%v\nnot to contain\n%v",
moviesBox,
matrixWithLength)
}
}

func movie(title string, year int) map[string]any {
return map[string]any{"title": title, "year": year}
}

func withLength(tuple map[string]any, length int) map[string]any {
x := map[string]any{}
for k, v := range tuple {
x[k] = v
}
x["length"] = length
return x
}

0 comments on commit d40171f

Please sign in to comment.