Skip to content

Commit

Permalink
fix: adjust to some linter findings
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
  • Loading branch information
CarstenLeue committed Feb 5, 2024
1 parent 02ec50c commit e166806
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 42 deletions.
4 changes: 2 additions & 2 deletions context/readerioeither/http/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ import (
func TestBuilderWithQuery(t *testing.T) {
// add some query
withLimit := R.WithQueryArg("limit")("10")
withUrl := R.WithUrl("http://www.example.org?a=b")
withURL := R.WithUrl("http://www.example.org?a=b")

b := F.Pipe2(
R.Default,
withLimit,
withUrl,
withURL,
)

req := F.Pipe3(
Expand Down
2 changes: 1 addition & 1 deletion context/readerioeither/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

type PostItem struct {
UserId uint `json:"userId"`
UserID uint `json:"userId"`
Id uint `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
Expand Down
4 changes: 2 additions & 2 deletions http/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var (
noBody = O.None[E.Either[error, []byte]]()
noQueryArg = O.None[string]()

parseUrl = E.Eitherize1(url.Parse)
parseURL = E.Eitherize1(url.Parse)
parseQuery = E.Eitherize1(url.ParseQuery)

// WithQuery creates a [Endomorphism] for a complete set of query parameters
Expand Down Expand Up @@ -153,7 +153,7 @@ func (builder *Builder) GetTargetUrl() E.Either[error, string] {
return F.Pipe3(
builder,
Url.Get,
parseUrl,
parseURL,
E.Chain(F.Flow4(
T.Replicate2[*url.URL],
T.Map2(
Expand Down
26 changes: 26 additions & 0 deletions internal/foldable/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2024 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package foldable

import (
M "github.com/IBM/fp-go/monoid"
)

type Foldable[A, B, HKTA any] interface {
Reduce(func(B, A) B, B) func(HKTA) B
ReduceRight(func(B, A) B, B) func(HKTA) B
FoldMap(m M.Monoid[B]) func(func(A) B) func(HKTA) B
}
24 changes: 24 additions & 0 deletions internal/fromeither/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2024 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fromeither

import (
ET "github.com/IBM/fp-go/either"
)

type FromEither[E, A, HKTA any] interface {
FromEither(ET.Either[E, A]) HKTA
}
20 changes: 20 additions & 0 deletions internal/fromio/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2024 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fromio

type FromIO[A, GA ~func() A, HKTA any] interface {
FromIO(GA) HKTA
}
4 changes: 2 additions & 2 deletions ioeither/http/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ import (
func TestBuilderWithQuery(t *testing.T) {
// add some query
withLimit := R.WithQueryArg("limit")("10")
withUrl := R.WithUrl("http://www.example.org?a=b")
withURL := R.WithUrl("http://www.example.org?a=b")

b := F.Pipe2(
R.Default,
withLimit,
withUrl,
withURL,
)

req := F.Pipe3(
Expand Down
2 changes: 1 addition & 1 deletion ioeither/http/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var testLogPolicy = R.CapDelay(
)

type PostItem struct {
UserId uint `json:"userId"`
UserID uint `json:"userId"`
Id uint `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
Expand Down
4 changes: 2 additions & 2 deletions iterator/stateless/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func isPrimeNumber(num int) bool {
if num <= 2 {
return true
}
sq_root := int(math.Sqrt(float64(num)))
for i := 2; i <= sq_root; i++ {
sqRoot := int(math.Sqrt(float64(num)))
for i := 2; i <= sqRoot; i++ {
if num%i == 0 {
return false
}
Expand Down
14 changes: 7 additions & 7 deletions samples/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (
)

type PostItem struct {
UserId uint `json:"userId"`
Id uint `json:"id"`
UserID uint `json:"userId"`
ID uint `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
}
Expand All @@ -43,7 +43,7 @@ type CatFact struct {
Fact string `json:"fact"`
}

func idxToUrl(idx int) string {
func idxToURL(idx int) string {
return fmt.Sprintf("https://jsonplaceholder.typicode.com/posts/%d", idx+1)
}

Expand All @@ -59,7 +59,7 @@ func TestMultipleHttpRequests(t *testing.T) {
count := 10

data := F.Pipe3(
A.MakeBy(count, idxToUrl),
A.MakeBy(count, idxToURL),
R.TraverseArray(F.Flow3(
H.MakeGetRequest,
readSinglePost,
Expand All @@ -74,7 +74,7 @@ func TestMultipleHttpRequests(t *testing.T) {
assert.Equal(t, E.Of[error](count), result())
}

func heterogeneousHttpRequests() R.ReaderIOEither[T.Tuple2[PostItem, CatFact]] {
func heterogeneousHTTPRequests() R.ReaderIOEither[T.Tuple2[PostItem, CatFact]] {
// prepare the http client
client := H.MakeClient(HTTP.DefaultClient)
// readSinglePost sends a GET request and parses the response as [PostItem]
Expand All @@ -97,7 +97,7 @@ func heterogeneousHttpRequests() R.ReaderIOEither[T.Tuple2[PostItem, CatFact]] {
// TestHeterogeneousHttpRequests shows how to execute multiple HTTP requests in parallel when
// the response structure of these requests is different. We use [R.TraverseTuple2] to account for the different types
func TestHeterogeneousHttpRequests(t *testing.T) {
data := heterogeneousHttpRequests()
data := heterogeneousHTTPRequests()

result := data(context.Background())

Expand All @@ -108,6 +108,6 @@ func TestHeterogeneousHttpRequests(t *testing.T) {
// the response structure of these requests is different. We use [R.TraverseTuple2] to account for the different types
func BenchmarkHeterogeneousHttpRequests(b *testing.B) {
for n := 0; n < b.N; n++ {
heterogeneousHttpRequests()(context.Background())()
heterogeneousHTTPRequests()(context.Background())()
}
}
16 changes: 8 additions & 8 deletions samples/mostly-adequate/chapter06_exampleapplication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,26 @@ func Example_application() {
S.Format[string](fmt.Sprintf("https://%s%s%%s", host, path)),
)
// flick returns jsonP, we extract the JSON body, this is handled by jquery in the original code
sanitizeJsonP := Replace(regexp.MustCompile(`(?s)^\s*\((.*)\)\s*$`))("$1")
sanitizeJSONP := Replace(regexp.MustCompile(`(?s)^\s*\((.*)\)\s*$`))("$1")
// parse jsonP
parseJsonP := F.Flow3(
sanitizeJsonP,
parseJSONP := F.Flow3(
sanitizeJSONP,
S.ToBytes,
J.Unmarshal[FlickrFeed],
)
// markup
img := S.Format[string]("<img src='%s'/>")
// lenses
mediaUrl := F.Flow2(
mediaURL := F.Flow2(
FlickrItem.getMedia,
FlickrMedia.getLink,
)
mediaUrls := F.Flow2(
mediaURLs := F.Flow2(
FlickrFeed.getItems,
A.Map(mediaUrl),
A.Map(mediaURL),
)
images := F.Flow2(
mediaUrls,
mediaURLs,
A.Map(img),
)

Expand All @@ -97,7 +97,7 @@ func Example_application() {
url,
H.MakeGetRequest,
H.ReadText(client),
R.ChainEitherK(parseJsonP),
R.ChainEitherK(parseJSONP),
R.Map(images),
)

Expand Down
8 changes: 4 additions & 4 deletions samples/mostly-adequate/chapter10_applicativefunctor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

type (
PostItem struct {
UserId uint `json:"userId"`
UserID uint `json:"userId"`
Id uint `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
Expand Down Expand Up @@ -77,15 +77,15 @@ func (player Player) getName() string {
return player.Name
}

func (player Player) getId() int {
func (player Player) getID() int {
return player.Id
}

func (item PostItem) getTitle() string {
return item.Title
}

func idxToUrl(idx int) string {
func idxToURL(idx int) string {
return fmt.Sprintf("https://jsonplaceholder.typicode.com/posts/%d", idx+1)
}

Expand All @@ -101,7 +101,7 @@ func Example_renderPage() {

// get returns the title of the nth item from the REST service
get := F.Flow4(
idxToUrl,
idxToURL,
H.MakeGetRequest,
H.ReadJson[PostItem](client),
R.Map(PostItem.getTitle),
Expand Down
14 changes: 7 additions & 7 deletions samples/mostly-adequate/chapter11_transformagain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
S "github.com/IBM/fp-go/string"
)

func findUserById(id int) IOE.IOEither[error, Chapter08User] {
func findUserByID(id int) IOE.IOEither[error, Chapter08User] {
switch id {
case 1:
return IOE.Of[error](albert08)
Expand All @@ -52,15 +52,15 @@ func Example_solution11A() {
}

func Example_solution11B() {
findByNameId := F.Flow2(
findUserById,
findByNameID := F.Flow2(
findUserByID,
IOE.Map[error](Chapter08User.getName),
)

fmt.Println(findByNameId(1)())
fmt.Println(findByNameId(2)())
fmt.Println(findByNameId(3)())
fmt.Println(findByNameId(4)())
fmt.Println(findByNameID(1)())
fmt.Println(findByNameID(2)())
fmt.Println(findByNameID(3)())
fmt.Println(findByNameID(4)())

// Output:
// Right[<nil>, string](Albert)
Expand Down
2 changes: 1 addition & 1 deletion samples/mostly-adequate/chapter12_traversing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var (
}

// validate :: Player -> Either error Player
validatePlayer = E.FromPredicate(P.ContraMap(Player.getName)(S.IsNonEmpty), F.Flow2(Player.getId, errors.OnSome[int]("player %d must have a name")))
validatePlayer = E.FromPredicate(P.ContraMap(Player.getName)(S.IsNonEmpty), F.Flow2(Player.getID, errors.OnSome[int]("player %d must have a name")))

// readfile :: String -> String -> Task Error String
readfile = F.Curry2(func(encoding, file string) IOE.IOEither[error, string] {
Expand Down
10 changes: 5 additions & 5 deletions samples/presentation/benchmarks/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

type PostItem struct {
UserId uint `json:"userId"`
UserID uint `json:"userId"`
Id uint `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
Expand All @@ -41,7 +41,7 @@ type CatFact struct {
Fact string `json:"fact"`
}

func heterogeneousHttpRequests(count int) R.ReaderIOEither[[]T.Tuple2[PostItem, CatFact]] {
func heterogeneousHTTPRequests(count int) R.ReaderIOEither[[]T.Tuple2[PostItem, CatFact]] {
// prepare the http client
client := H.MakeClient(HTTP.DefaultClient)
// readSinglePost sends a GET request and parses the response as [PostItem]
Expand All @@ -64,7 +64,7 @@ func heterogeneousHttpRequests(count int) R.ReaderIOEither[[]T.Tuple2[PostItem,
)
}

func heterogeneousHttpRequestsIdiomatic(count int) ([]T.Tuple2[PostItem, CatFact], error) {
func heterogeneousHTTPRequestsIdiomatic(count int) ([]T.Tuple2[PostItem, CatFact], error) {
// prepare the http client
var result []T.Tuple2[PostItem, CatFact]

Expand Down Expand Up @@ -109,13 +109,13 @@ func BenchmarkHeterogeneousHttpRequests(b *testing.B) {

b.Run("functional", func(b *testing.B) {
for n := 0; n < b.N; n++ {
benchResults = heterogeneousHttpRequests(count)(context.Background())()
benchResults = heterogeneousHTTPRequests(count)(context.Background())()
}
})

b.Run("idiomatic", func(b *testing.B) {
for n := 0; n < b.N; n++ {
benchResults, _ = heterogeneousHttpRequestsIdiomatic(count)
benchResults, _ = heterogeneousHTTPRequestsIdiomatic(count)
}
})

Expand Down

0 comments on commit e166806

Please sign in to comment.