Skip to content

Commit fe1eb0d

Browse files
committed
Initial commit
0 parents  commit fe1eb0d

File tree

20 files changed

+600270
-0
lines changed

20 files changed

+600270
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Atomic Arithmetic
2+
3+
Inspired by [Tailwind CSS](https://tailwindcss.com/).
4+
5+
A utility-first arithmetic framework packed with functions like `Add23`, `Subtract8921`, `MultiplyBy5522`
6+
and `DivideBy7` that can be composed to compute any number, directly in your Go code.
7+
Atomic Arithmetic is unapologetically modern, and takes advantage of all the latest and greatest Go features
8+
to make the developer experience as enjoyable as possible.
9+
10+
Build whatever you want, without touching pesky [arithmetic operators](https://go.dev/ref/spec#Arithmetic_operators).
11+
Currently supports numbers up to 10,000, which should be enough for most applications.
12+
13+
## Usage
14+
15+
Import the operations package to use the atomic arithmetic functions.
16+
Fast, built-in autocomplete in most IDEs!
17+
18+
```go
19+
package main
20+
21+
import "github.com/initialcapacity/atomic-arithmetic/pkg/operations"
22+
23+
func main() {
24+
operations.Add365(17)
25+
operations.Subtract714(893)
26+
operations.MultiplyBy199(87)
27+
operations.DivideBy342(744)
28+
operations.Mod17(654)
29+
}
30+
```
31+
32+
Run the [demo](./cmd/demo/main.go) to see these examples in action.
33+
34+
```shell
35+
go run ./cmd/demo
36+
```
37+
38+
## Development
39+
40+
1. Re-generate the operations package.
41+
```shell
42+
go ./cmd/generate
43+
```
44+
45+
1. Run the extensive test suite.
46+
```shell
47+
go test ./...
48+
```

cmd/demo/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/initialcapacity/atomic-arithmetic/pkg/operations"
6+
)
7+
8+
func main() {
9+
fmt.Println("\nAdd 365 to 17")
10+
fmt.Println(operations.Add365(17))
11+
12+
fmt.Println("\nSubtract 714 from 893")
13+
fmt.Println(operations.Subtract714(893))
14+
15+
fmt.Println("\nMultiple 87 by 199")
16+
fmt.Println(operations.MultiplyBy199(87))
17+
18+
fmt.Println("\nDivide 744 by 342")
19+
fmt.Println(operations.DivideBy342(744))
20+
21+
fmt.Println("\nCalculate 654 modulo 17")
22+
fmt.Println(operations.Mod17(654))
23+
}

cmd/generate/main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"github.com/initialcapacity/atomic-arithmetic/internal/go_support"
5+
"log"
6+
)
7+
8+
func main() {
9+
operationsDir, err := go_support.CreatePackageDirectory("operations")
10+
if err != nil {
11+
log.Fatalf("Unable to create pkg/operations directory: %v\n", err)
12+
}
13+
14+
maximumNumber := 10_000
15+
16+
go_support.CreateAtomicFile(operationsDir, "operations", "add.go", "Add", "+", maximumNumber)
17+
go_support.CreateAtomicTest(operationsDir, "operations", "add_test.go", "Add", func(i, j int) int { return i + j }, maximumNumber)
18+
19+
go_support.CreateAtomicFile(operationsDir, "operations", "subtract.go", "Subtract", "-", maximumNumber)
20+
go_support.CreateAtomicTest(operationsDir, "operations", "subtract_test.go", "Subtract", func(i, j int) int { return i - j }, maximumNumber)
21+
22+
go_support.CreateAtomicFile(operationsDir, "operations", "multiply.go", "MultiplyBy", "*", maximumNumber)
23+
go_support.CreateAtomicTest(operationsDir, "operations", "multiply_test.go", "MultiplyBy", func(i, j int) int { return i * j }, maximumNumber)
24+
25+
go_support.CreateAtomicFile(operationsDir, "operations", "divide.go", "DivideBy", "/", maximumNumber)
26+
go_support.CreateAtomicTest(operationsDir, "operations", "divide_test.go", "DivideBy", func(i, j int) int { return i / j }, maximumNumber)
27+
28+
go_support.CreateAtomicFile(operationsDir, "operations", "modulo.go", "Mod", "%", maximumNumber)
29+
go_support.CreateAtomicTest(operationsDir, "operations", "modulo_test.go", "Mod", func(i, j int) int { return i % j }, maximumNumber)
30+
31+
log.Println("Success!")
32+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/initialcapacity/atomic-arithmetic
2+
3+
go 1.23.5

internal/go_support/atomic_file.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package go_support
2+
3+
import (
4+
"log"
5+
"path/filepath"
6+
)
7+
8+
func CreateAtomicFile(directory, packageName, fileName, functionPrefix, operator string, count int) {
9+
builder := NewFileBuilder(packageName)
10+
11+
for i := 1; i <= count; i++ {
12+
builder.AppendFunction(functionPrefix, operator, i)
13+
}
14+
15+
err := builder.WriteFile(filepath.Join(directory, fileName))
16+
if err != nil {
17+
log.Fatalf("Unable to create %s/%s: %v\n", packageName, fileName, err)
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package go_support
2+
3+
import (
4+
"log"
5+
"path/filepath"
6+
)
7+
8+
func CreateAtomicTest(directory, packageName, fileName, functionPrefix string, resultCalculator func(int, int) int, count int) {
9+
addBuilder := NewTestBuilder(packageName)
10+
11+
for i := 1; i <= count; i++ {
12+
addBuilder.AppendTestFunction(functionPrefix, i, 17_000, resultCalculator(17_000, i))
13+
}
14+
15+
err := addBuilder.WriteFile(filepath.Join(directory, fileName))
16+
if err != nil {
17+
log.Fatalf("Unable to create %s/%s: %v\n", packageName, fileName, err)
18+
}
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package go_support
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
)
8+
9+
func CreatePackageDirectory(packageName string) (string, error) {
10+
pkgDir := filepath.Join(".", "pkg")
11+
err := os.RemoveAll(pkgDir)
12+
if err != nil {
13+
return "", fmt.Errorf("failed to remove pkg directory: %v", err)
14+
}
15+
operationsDir := filepath.Join(pkgDir, packageName)
16+
err = os.MkdirAll(operationsDir, 0755)
17+
if err != nil {
18+
return "", fmt.Errorf("failed to create %s directory: %v\n", packageName, err)
19+
}
20+
return operationsDir, nil
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package go_support
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
)
8+
9+
type FileBuilder struct {
10+
stringBuilder strings.Builder
11+
}
12+
13+
func NewFileBuilder(packageName string) *FileBuilder {
14+
b := &FileBuilder{}
15+
b.stringBuilder.WriteString(fmt.Sprintf(`package %s
16+
`, packageName))
17+
return b
18+
}
19+
20+
func (b *FileBuilder) AppendFunction(functionPrefix, operator string, n int) {
21+
b.stringBuilder.WriteString(fmt.Sprintf(`
22+
func %s%d(operand int) int {
23+
return operand %s %d
24+
}
25+
`, functionPrefix, n, operator, n))
26+
}
27+
28+
func (b *FileBuilder) WriteFile(path string) error {
29+
return os.WriteFile(path, []byte(b.stringBuilder.String()), 0644)
30+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package go_support
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
)
8+
9+
type TestBuilder struct {
10+
stringBuilder strings.Builder
11+
}
12+
13+
func NewTestBuilder(packageName string) *TestBuilder {
14+
b := &TestBuilder{}
15+
b.stringBuilder.WriteString(fmt.Sprintf(`package %s_test
16+
17+
import (
18+
"github.com/initialcapacity/atomic-arithmetic/pkg/%s"
19+
"testing"
20+
)
21+
`, packageName, packageName))
22+
return b
23+
}
24+
25+
func (b *TestBuilder) AppendTestFunction(functionPrefix string, n, input, result int) {
26+
b.stringBuilder.WriteString(fmt.Sprintf(`
27+
func Test%s%d(t *testing.T) {
28+
result := operations.%s%d(%d)
29+
30+
if result != %d {
31+
t.Errorf("Expected %d but got %%d", result)
32+
}
33+
}
34+
`, functionPrefix, n, functionPrefix, n, input, result, result))
35+
}
36+
37+
func (b *TestBuilder) WriteFile(path string) error {
38+
return os.WriteFile(path, []byte(b.stringBuilder.String()), 0644)
39+
}

0 commit comments

Comments
 (0)