Minor code refactors in some tasks
This commit is contained in:
parent
e0ed0a4cde
commit
6fe051eea5
5 changed files with 30 additions and 38 deletions
|
@ -27,36 +27,30 @@ func (result HTTPGetResult) String() string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetHTTPBody(url string, ch chan HTTPGetResult) {
|
func GetHTTPBody(url string, ch chan string) {
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ch <- HTTPGetResult{Success: false, Error: err}
|
ch <- fmt.Sprint(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
data, err := io.ReadAll(resp.Body)
|
n, err := io.Copy(io.Discard, resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ch <- HTTPGetResult{Success: false, Error: err}
|
ch <- fmt.Sprint(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ch <- HTTPGetResult{
|
ch <- fmt.Sprintf("%.2fs\t%v\t%v", time.Since(startTime).Seconds(), n, url)
|
||||||
Success: true,
|
|
||||||
Body: string(data),
|
|
||||||
Elapsed: time.Since(startTime),
|
|
||||||
Size: len(data),
|
|
||||||
URL: url,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
urls, ch := os.Args[1:], make(chan HTTPGetResult)
|
urls, ch := os.Args[1:], make(chan string)
|
||||||
for _, url := range urls {
|
for _, url := range urls {
|
||||||
go GetHTTPBody(url, ch)
|
go GetHTTPBody(url, ch)
|
||||||
}
|
}
|
||||||
for i := 0; i < len(urls); i++ {
|
for range urls {
|
||||||
fmt.Println(<-ch)
|
fmt.Println(<-ch)
|
||||||
}
|
}
|
||||||
fmt.Println(time.Since(startTime), "elapsed")
|
fmt.Printf("%.2fs elapsed\n", time.Since(startTime).Seconds())
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ func (e *Evaluator) arithmeticOp(op func(int, int) int) func() error {
|
||||||
return func() error {
|
return func() error {
|
||||||
a, b, err := e.popTwo()
|
a, b, err := e.popTwo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("not enough elements on stack")
|
return err
|
||||||
}
|
}
|
||||||
e.push(op(a, b))
|
e.push(op(a, b))
|
||||||
return nil
|
return nil
|
||||||
|
@ -55,7 +55,7 @@ func (e *Evaluator) initBasicWords() {
|
||||||
e.words["/"] = func() error {
|
e.words["/"] = func() error {
|
||||||
a, b, err := e.popTwo()
|
a, b, err := e.popTwo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("not enough elements on stack")
|
return err
|
||||||
}
|
}
|
||||||
if b == 0 {
|
if b == 0 {
|
||||||
return errors.New("division by zero")
|
return errors.New("division by zero")
|
||||||
|
@ -110,8 +110,7 @@ func (e *Evaluator) getFunc(word string) (func() error, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Evaluator) defineWord(word string, def string) error {
|
func (e *Evaluator) defineWord(word string, def string) error {
|
||||||
_, err := strconv.Atoi(word)
|
if _, err := strconv.Atoi(word); err == nil {
|
||||||
if err == nil {
|
|
||||||
return errors.New("word definition must not be numeric")
|
return errors.New("word definition must not be numeric")
|
||||||
}
|
}
|
||||||
definitionWords := strings.Fields(def)
|
definitionWords := strings.Fields(def)
|
||||||
|
|
|
@ -11,7 +11,7 @@ func singleCourseList(
|
||||||
course string, prereqs map[string][]string, list []string, traversed []string,
|
course string, prereqs map[string][]string, list []string, traversed []string,
|
||||||
) []string {
|
) []string {
|
||||||
if slices.Contains(traversed, course) {
|
if slices.Contains(traversed, course) {
|
||||||
log.Panic("Recursive prereqs, aborting...")
|
log.Panic("recursive prereqs, aborting...")
|
||||||
}
|
}
|
||||||
traversed = append(traversed, course)
|
traversed = append(traversed, course)
|
||||||
for _, prereqCourse := range prereqs[course] {
|
for _, prereqCourse := range prereqs[course] {
|
||||||
|
|
|
@ -6,24 +6,23 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var digits = []string{
|
var (
|
||||||
|
digits = []string{
|
||||||
"", "one", "two", "three", "four", "five",
|
"", "one", "two", "three", "four", "five",
|
||||||
"six", "seven", "eight", "nine",
|
"six", "seven", "eight", "nine",
|
||||||
}
|
}
|
||||||
|
teens = []string{
|
||||||
var teens = []string{
|
|
||||||
"ten", "eleven", "twelve", "thirteen", "fourteen",
|
"ten", "eleven", "twelve", "thirteen", "fourteen",
|
||||||
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
|
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
|
||||||
}
|
}
|
||||||
|
decs = []string{
|
||||||
var decs = []string{
|
|
||||||
"", "", "twenty", "thirty", "forty", "fifty",
|
"", "", "twenty", "thirty", "forty", "fifty",
|
||||||
"sixty", "seventy", "eighty", "ninety",
|
"sixty", "seventy", "eighty", "ninety",
|
||||||
}
|
}
|
||||||
|
large = []string{
|
||||||
var large = []string{
|
|
||||||
"billion", "million", "thousand",
|
"billion", "million", "thousand",
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
|
||||||
func digitsReversedSlice(n int64) []uint8 {
|
func digitsReversedSlice(n int64) []uint8 {
|
||||||
digits := make([]uint8, 12)
|
digits := make([]uint8, 12)
|
||||||
|
|
|
@ -22,6 +22,6 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic("fetch:", err)
|
log.Panic("fetch:", err)
|
||||||
}
|
}
|
||||||
fmt.Println(string(body))
|
fmt.Printf("%s", body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue