2022-03-27 01:56:03 +00:00
|
|
|
# lambda
|
|
|
|
|
|
|
|
package lambda provides tools for performing operations on generic array types
|
|
|
|
|
|
|
|
features:
|
|
|
|
|
|
|
|
|
|
|
|
features:
|
|
|
|
- Map [T] -> [T]
|
|
|
|
- MapV [T] -> [V]
|
|
|
|
- MapError [T] -> ([T], [error]
|
|
|
|
- MapErrorV [T] -> ([V], [error]
|
|
|
|
- Foldr [T] -> T
|
|
|
|
- Foldl [T] -> T
|
|
|
|
- Entries map[K]V -> ([K], [V])
|
|
|
|
- Flatten [[T]] -> [T]
|
|
|
|
- Merge ([T], [T]) -> [T]
|
|
|
|
- Split ([T], int) -> [[T]]
|
|
|
|
|
|
|
|
todo:
|
|
|
|
- idk
|
|
|
|
|
2022-03-27 01:57:38 +00:00
|
|
|
example: adding some numbers
|
|
|
|
|
|
|
|
```
|
|
|
|
package main
|
2022-03-27 01:58:15 +00:00
|
|
|
|
2022-03-27 01:57:38 +00:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2022-03-27 01:57:56 +00:00
|
|
|
"git.tuxpa.in/a/lambda"
|
2022-03-27 01:57:38 +00:00
|
|
|
)
|
2022-03-27 01:58:15 +00:00
|
|
|
|
2022-03-27 01:57:38 +00:00
|
|
|
func main(){
|
|
|
|
arr := []int{1, 2, 3, 4, 6, 5}
|
|
|
|
ans := lambda.Map(arr, func(x int) int { return x + 4 })
|
|
|
|
// ans = []int{5, 6, 7, 8, 10, 9}
|
|
|
|
log.Println(ans)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2022-03-27 01:56:03 +00:00
|
|
|
## parallel
|
|
|
|
|
|
|
|
subpackage parallel provides parallelized operations on generic array types
|
|
|
|
|
|
|
|
features:
|
|
|
|
- Map [T] -> [T]
|
|
|
|
- MapV [T] -> [V]
|
|
|
|
- MapError [T] -> ([T], [error]
|
|
|
|
- MapErrorV [T] -> ([V], [error]
|
|
|
|
|
|
|
|
todo:
|
|
|
|
- idk
|
|
|
|
|
|
|
|
example usage:
|
|
|
|
|
|
|
|
making a request
|
|
|
|
|
|
|
|
```
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.tuxpa.in/a/lambda/parallel"
|
|
|
|
)
|
|
|
|
|
|
|
|
type someResult struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Id int `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var requests = make([]*http.Request, 20)
|
|
|
|
|
|
|
|
for i := range requests {
|
|
|
|
r, _ := http.NewRequest("GET", fmt.Sprintf("https://jsonplaceholder.typicode.com/todos/%d", i), nil)
|
|
|
|
requests[i] = r
|
|
|
|
}
|
|
|
|
|
|
|
|
results, errs := parallel.MapErrorV(requests, func(r *http.Request) (*someResult, error) {
|
|
|
|
out := &someResult{}
|
|
|
|
res, err := http.DefaultClient.Do(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
err = json.NewDecoder(res.Body).Decode(out)
|
|
|
|
return out, err
|
|
|
|
}, 4)
|
|
|
|
|
|
|
|
for i, v := range results {
|
|
|
|
if errs[i] != nil {
|
|
|
|
log.Printf("err: %s", errs[i])
|
|
|
|
} else {
|
|
|
|
log.Printf("result: %+v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|