Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 363
Good morning! Here's your coding interview problem for today.

This problem was asked by Squarespace.

Write a function, `add_subtract`, which alternately adds and subtracts curried arguments. Here are some sample operations:

```
add_subtract(7) -> 7

add_subtract(1)(2)(3) -> 1 + 2 - 3 -> 0

add_subtract(-5)(10)(3)(9) -> -5 + 10 - 3 + 9 -> 11
```

Pull request description:
Day363
Resolves #716.

Patch:
diff --git a/README.md b/README.md
index 953af36..80c4b16 100644
--- a/README.md
+++ b/README.md
@@ -344,3 +344,4 @@ problems from
 * [Day 360](https://github.com/vaskoz/dailycodingproblem-go/issues/709)
 * [Day 361](https://github.com/vaskoz/dailycodingproblem-go/issues/712)
 * [Day 362](https://github.com/vaskoz/dailycodingproblem-go/issues/714)
+* [Day 363](https://github.com/vaskoz/dailycodingproblem-go/issues/716)
diff --git a/day363/problem.go b/day363/problem.go
new file mode 100644
index 0000000..b0fc94b
--- /dev/null
+++ b/day363/problem.go
@@ -0,0 +1,24 @@
+package day363
+
+type curry struct {
+	total int
+	add   bool
+}
+
+func (c *curry) __(n int) *curry {
+	if c.add {
+		c.total += n
+	} else {
+		c.total -= n
+	}
+	c.add = !c.add
+	return c
+}
+
+func (c *curry) Execute() int {
+	return c.total
+}
+
+func AddSubtract(n int) *curry {
+	return &curry{n, true}
+}


Test patch:
diff --git a/day363/problem_test.go b/day363/problem_test.go
new file mode 100644
index 0000000..4fb437d
--- /dev/null
+++ b/day363/problem_test.go
@@ -0,0 +1,48 @@
+package day363
+
+import "testing"
+
+// nolint
+var testcases = []struct {
+	input    []int
+	expected int
+}{
+	{[]int{7}, 7},
+	{[]int{1, 2, 3}, 0},
+	{[]int{-5, 10, 3, 9}, 11},
+}
+
+func TestAddSubtract(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		as := AddSubtract(tc.input[0])
+		for i := 1; i < len(tc.input); i++ {
+			as = as.__(tc.input[i])
+		}
+		if result := as.Execute(); result != tc.expected {
+			t.Errorf("Expected %v, got %v", tc.expected, result)
+		}
+	}
+}
+
+func TestAddSubtractChained(t *testing.T) {
+	t.Parallel()
+	result := AddSubtract(5).__(6).__(12).__(20).__(30)
+	result = result.__(10).__(-50)
+	expected := 49
+	if result.Execute() != expected {
+		t.Errorf("Expected %v, got %v", expected, result.Execute())
+	}
+}
+
+func BenchmarkAddSubtract(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			as := AddSubtract(tc.input[0])
+			for i := 1; i < len(tc.input); i++ {
+				as = as.__(tc.input[i])
+			}
+			as.Execute()
+		}
+	}
+}