Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 105
Good morning! Here's your coding interview problem for today.

This problem was asked by Facebook.

Given a function f, and N return a debounced f of N milliseconds.

That is, as long as the debounced f continues to be invoked, f itself will not be called for N milliseconds.

Pull request:
Day105
Resolves #221. 

Patch:
diff --git a/README.md b/README.md
index 4c264ef..e1ff522 100644
--- a/README.md
+++ b/README.md
@@ -114,3 +114,4 @@ problems from
 * [Day 102](https://github.com/vaskoz/dailycodingproblem-go/issues/214)
 * [Day 103](https://github.com/vaskoz/dailycodingproblem-go/issues/217)
 * [Day 104](https://github.com/vaskoz/dailycodingproblem-go/issues/219)
+* [Day 105](https://github.com/vaskoz/dailycodingproblem-go/issues/221)
diff --git a/day105/problem.go b/day105/problem.go
new file mode 100644
index 0000000..5135450
--- /dev/null
+++ b/day105/problem.go
@@ -0,0 +1,15 @@
+package day105
+
+import "time"
+
+// Debounced returns a debounced function that will not call its
+// provided function until after N milliseconds despite calling it.
+func Debounced(f func(), N int) func() {
+	created := time.Now()
+	return func() {
+		now := time.Now()
+		if elapsed := int(now.Sub(created) / time.Millisecond); elapsed > N {
+			f()
+		}
+	}
+}


Test patch:
diff --git a/day105/problem_test.go b/day105/problem_test.go
new file mode 100644
index 0000000..2fa10f3
--- /dev/null
+++ b/day105/problem_test.go
@@ -0,0 +1,28 @@
+package day105
+
+import (
+	"testing"
+	"time"
+)
+
+func TestDebounced(t *testing.T) {
+	t.Parallel()
+	x := 0
+	f := func() {
+		x++
+	}
+	dbf := Debounced(f, 500)
+	for i := 0; i < 10; i++ {
+		dbf()
+	}
+	if x != 0 {
+		t.Errorf("Expected x not to change since it's debounced for 500ms")
+	}
+	time.Sleep(800 * time.Millisecond)
+	for i := 0; i < 10; i++ {
+		dbf()
+	}
+	if x != 10 {
+		t.Errorf("Expected x to be incremented 10 times")
+	}
+}