Minor code refactors in some tasks

This commit is contained in:
Egor 2024-06-06 02:15:25 +03:00
parent e0ed0a4cde
commit 6fe051eea5
5 changed files with 30 additions and 38 deletions

View file

@ -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()
resp, err := http.Get(url)
if err != nil {
ch <- HTTPGetResult{Success: false, Error: err}
ch <- fmt.Sprint(err)
return
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
n, err := io.Copy(io.Discard, resp.Body)
if err != nil {
ch <- HTTPGetResult{Success: false, Error: err}
ch <- fmt.Sprint(err)
return
}
ch <- HTTPGetResult{
Success: true,
Body: string(data),
Elapsed: time.Since(startTime),
Size: len(data),
URL: url,
}
ch <- fmt.Sprintf("%.2fs\t%v\t%v", time.Since(startTime).Seconds(), n, url)
}
func main() {
startTime := time.Now()
urls, ch := os.Args[1:], make(chan HTTPGetResult)
urls, ch := os.Args[1:], make(chan string)
for _, url := range urls {
go GetHTTPBody(url, ch)
}
for i := 0; i < len(urls); i++ {
for range urls {
fmt.Println(<-ch)
}
fmt.Println(time.Since(startTime), "elapsed")
fmt.Printf("%.2fs elapsed\n", time.Since(startTime).Seconds())
}

View file

@ -41,7 +41,7 @@ func (e *Evaluator) arithmeticOp(op func(int, int) int) func() error {
return func() error {
a, b, err := e.popTwo()
if err != nil {
return errors.New("not enough elements on stack")
return err
}
e.push(op(a, b))
return nil
@ -55,7 +55,7 @@ func (e *Evaluator) initBasicWords() {
e.words["/"] = func() error {
a, b, err := e.popTwo()
if err != nil {
return errors.New("not enough elements on stack")
return err
}
if b == 0 {
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 {
_, err := strconv.Atoi(word)
if err == nil {
if _, err := strconv.Atoi(word); err == nil {
return errors.New("word definition must not be numeric")
}
definitionWords := strings.Fields(def)

View file

@ -11,7 +11,7 @@ func singleCourseList(
course string, prereqs map[string][]string, list []string, traversed []string,
) []string {
if slices.Contains(traversed, course) {
log.Panic("Recursive prereqs, aborting...")
log.Panic("recursive prereqs, aborting...")
}
traversed = append(traversed, course)
for _, prereqCourse := range prereqs[course] {

View file

@ -6,24 +6,23 @@ import (
"strings"
)
var digits = []string{
var (
digits = []string{
"", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine",
}
var teens = []string{
teens = []string{
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
}
var decs = []string{
decs = []string{
"", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety",
}
var large = []string{
large = []string{
"billion", "million", "thousand",
}
)
func digitsReversedSlice(n int64) []uint8 {
digits := make([]uint8, 12)

View file

@ -22,6 +22,6 @@ func main() {
if err != nil {
log.Panic("fetch:", err)
}
fmt.Println(string(body))
fmt.Printf("%s", body)
}
}