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
package com.example.demo.controller; | |
import com.example.demo.calculator.Calculator; | |
import com.example.demo.calculator.CalculatorException; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestParam; | |
import org.springframework.web.bind.annotation.RestController; | |
@RestController | |
public class CalcController { |
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
package com.example.demo.calculator; | |
import java.util.ArrayDeque; | |
import java.util.Deque; | |
public class CalculatorImpl implements Calculator { | |
public CalculatorImpl() { | |
} | |
@Override |
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
package com.example.demo.calculator; | |
public interface Calculator { | |
double process(String expression); | |
} |
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
package main | |
import ( | |
"fmt" | |
"strconv" | |
) | |
func main() { | |
vals := [][]string{{"FizzBuzz", "Fizz"}, {"Buzz", ""}} | |
for i := 1; i <= 100; i++ { |
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
type WorkPool struct { | |
work chan Runnable | |
shutdown chan struct{} | |
o sync.Once | |
} | |
type Runnable func() | |
func NewWorkPool(tasks int) *WorkPool { | |
work := make(chan Runnable, tasks) |
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
func AddSlowly(a, b int) int { | |
time.Sleep(100 * time.Millisecond) | |
return a + b | |
} | |
func main() { | |
ch, err := Cacher(AddSlowly, 2*time.Second) | |
if err != nil { | |
panic(err) | |
} |
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
type outExp struct { | |
out []reflect.Value | |
expiry time.Time | |
} | |
func Cacher(f interface{}, expiration time.Duration) (interface{}, error) { | |
ft := reflect.TypeOf(f) | |
if ft.Kind() != reflect.Func { | |
return nil, errors.New("Only for functions") | |
} |
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
func buildInStruct(ft reflect.Type) (reflect.Type, error) { | |
if ft.NumIn() == 0 { | |
return nil, errors.New("Must have at least one param") | |
} | |
var sf []reflect.StructField | |
for i := 0; i < ft.NumIn(); i++ { | |
ct := ft.In(i) | |
if !ct.Comparable() { | |
return nil, fmt.Errorf("parameter %d of type %s and kind %v is not comparable", i+1, ct.Name(), ct.Kind()) | |
} |
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
func Cacher(f interface{}, expiration time.Duration) (interface{}, error) { | |
ft := reflect.TypeOf(f) | |
if ft.Kind() != reflect.Func { | |
return nil, errors.New("Only for functions") | |
} | |
return f, nil | |
} |
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
// Takes in a function and a time.Duration and returns a caching version of the function | |
// | |
// The limitations on memoization are: | |
// | |
// — there must be at least one in parameter | |
// | |
// — there must be at least one out parameter | |
// | |
// — the in parameters must be comparable. That means they cannot be of kind slice, map, or func, | |
// nor can input parameters be structs that contain (at any level) slices, maps, or funcs. |