Skip to content

Commit

Permalink
change strain to use generics
Browse files Browse the repository at this point in the history
  • Loading branch information
junedev committed Sep 8, 2023
1 parent 7967631 commit ecc5877
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 165 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@
"uuid": "7fbb0d9d-71d1-4654-8ca0-bb07ddbe6cb5",
"practices": [],
"prerequisites": [],
"difficulty": 2,
"difficulty": 4,
"topics": [
"arrays",
"filtering",
Expand Down
3 changes: 2 additions & 1 deletion exercises/practice/strain/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"robphoenix",
"sebito91",
"soniakeys",
"eklatzer"
"eklatzer",
"junedev"
],
"files": {
"solution": [
Expand Down
42 changes: 12 additions & 30 deletions exercises/practice/strain/.meta/example.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,25 @@
package strain

type Ints []int
func Keep[T any](list []T, fn func(T) bool) []T {
result := []T{}

func (s Ints) Keep(f func(int) bool) (r Ints) {
for _, e := range s {
if f(e) {
r = append(r, e)
for _, entry := range list {
if fn(entry) {
result = append(result, entry)
}
}
return
}

func (s Ints) Discard(f func(int) bool) (r Ints) {
for _, e := range s {
if !f(e) {
r = append(r, e)
}
}
return
return result
}

type Strings []string
func Discard[T any](list []T, fn func(T) bool) []T {
result := []T{}

func (s Strings) Keep(f func(string) bool) (r Strings) {
for _, e := range s {
if f(e) {
r = append(r, e)
for _, entry := range list {
if !fn(entry) {
result = append(result, entry)
}
}
return
}

type Lists [][]int

func (s Lists) Keep(f func([]int) bool) (r Lists) {
for _, e := range s {
if f(e) {
r = append(r, e)
}
}
return
return result
}
52 changes: 52 additions & 0 deletions exercises/practice/strain/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[26af8c32-ba6a-4eb3-aa0a-ebd8f136e003]
description = "keep on empty list returns empty list"

[f535cb4d-e99b-472a-bd52-9fa0ffccf454]
description = "keeps everything"

[950b8e8e-f628-42a8-85e2-9b30f09cde38]
description = "keeps nothing"

[92694259-6e76-470c-af87-156bdf75018a]
description = "keeps first and last"

[938f7867-bfc7-449e-a21b-7b00cbb56994]
description = "keeps neither first nor last"

[8908e351-4437-4d2b-a0f7-770811e48816]
description = "keeps strings"

[2728036b-102a-4f1e-a3ef-eac6160d876a]
description = "keeps lists"

[ef16beb9-8d84-451a-996a-14e80607fce6]
description = "discard on empty list returns empty list"

[2f42f9bc-8e06-4afe-a222-051b5d8cd12a]
description = "discards everything"

[ca990fdd-08c2-4f95-aa50-e0f5e1d6802b]
description = "discards nothing"

[71595dae-d283-48ca-a52b-45fa96819d2f]
description = "discards first and last"

[ae141f79-f86d-4567-b407-919eaca0f3dd]
description = "discards neither first nor last"

[daf25b36-a59f-4f29-bcfe-302eb4e43609]
description = "discards strings"

[a38d03f9-95ad-4459-80d1-48e937e4acaf]
description = "discards lists"
22 changes: 4 additions & 18 deletions exercises/practice/strain/strain.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
package strain

type Ints []int
type Lists [][]int
type Strings []string
// Implement the "Keep" and "Discard" function in this file.

func (i Ints) Keep(filter func(int) bool) Ints {
panic("Please implement the Keep function")
}

func (i Ints) Discard(filter func(int) bool) Ints {
panic("Please implement the Discard function")
}

func (l Lists) Keep(filter func([]int) bool) Lists {
panic("Please implement the Keep function")
}

func (s Strings) Keep(filter func(string) bool) Strings {
panic("Please implement the Keep function")
}
// You will need typed parameters (aka "Generics") to solve this exercise.
// They are not part of the Exercism syllabus yet but you can learn about
// them here: https://go.dev/tour/generics/1
Loading

0 comments on commit ecc5877

Please sign in to comment.