Skip to content

Commit 6adcc4b

Browse files
authored
add strain (exercism#206)
* Add strain
1 parent f502895 commit 6adcc4b

10 files changed

+290
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@
7474
"prerequisites": [],
7575
"difficulty": 1
7676
},
77+
{
78+
"slug": "strain",
79+
"name": "Strain",
80+
"uuid": "af541ed6-4112-4b61-99dd-fd29424ae859",
81+
"practices": [],
82+
"prerequisites": [],
83+
"difficulty": 1
84+
},
7785
{
7886
"slug": "high-scores",
7987
"name": "High Scores",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Instructions append
2+
3+
## Implementaton Notes
4+
5+
The test suite passes a Function class instance to the `keep` and `discard` methods as a [block argument][block-argument].
6+
To successfully pass the tests, you'll need to call that function inside your code.
7+
If you're unfamiliar with how to do this, please consult the [Function class documentation][function-class].
8+
9+
[block-argument]: https://wren.io/functions.html#block-arguments
10+
[function-class]: https://wren.io/modules/core/fn.html
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Instructions
2+
3+
Implement the `keep` and `discard` operation on collections.
4+
Given a collection and a predicate on the collection's elements, `keep` returns a new collection containing those elements where the predicate is true, while `discard` returns a new collection containing those elements where the predicate is false.
5+
6+
For example, given the collection of numbers:
7+
8+
- 1, 2, 3, 4, 5
9+
10+
And the predicate:
11+
12+
- is the number even?
13+
14+
Then your keep operation should produce:
15+
16+
- 2, 4
17+
18+
While your discard operation should produce:
19+
20+
- 1, 3, 5
21+
22+
Note that the union of keep and discard is all the elements.
23+
24+
The functions may be called `keep` and `discard`, or they may need different names in order to not clash with existing functions or concepts in your language.
25+
26+
## Restrictions
27+
28+
Keep your hands off that filter/reject/whatchamacallit functionality provided by your standard library!
29+
Solve this one yourself using other basic tools instead.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"strain.wren"
8+
],
9+
"test": [
10+
"strain.spec.wren"
11+
],
12+
"example": [
13+
".meta/proof.ci.wren"
14+
]
15+
},
16+
"blurb": "Implement the `keep` and `discard` operation on collections.",
17+
"source": "Conversation with James Edward Gray II",
18+
"source_url": "http://graysoftinc.com/"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Strain {
2+
static keep(list, predicate) {
3+
var kept = []
4+
for (item in list) {
5+
if (predicate.call(item)) {
6+
kept.add(item)
7+
}
8+
}
9+
return kept
10+
}
11+
12+
static discard(list, predicate) {
13+
return keep(list) { |x| !predicate.call(x) }
14+
}
15+
}
16+
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[26af8c32-ba6a-4eb3-aa0a-ebd8f136e003]
13+
description = "keep on empty list returns empty list"
14+
15+
[f535cb4d-e99b-472a-bd52-9fa0ffccf454]
16+
description = "keeps everything"
17+
18+
[950b8e8e-f628-42a8-85e2-9b30f09cde38]
19+
description = "keeps nothing"
20+
21+
[92694259-6e76-470c-af87-156bdf75018a]
22+
description = "keeps first and last"
23+
24+
[938f7867-bfc7-449e-a21b-7b00cbb56994]
25+
description = "keeps neither first nor last"
26+
27+
[8908e351-4437-4d2b-a0f7-770811e48816]
28+
description = "keeps strings"
29+
30+
[2728036b-102a-4f1e-a3ef-eac6160d876a]
31+
description = "keeps lists"
32+
33+
[ef16beb9-8d84-451a-996a-14e80607fce6]
34+
description = "discard on empty list returns empty list"
35+
36+
[2f42f9bc-8e06-4afe-a222-051b5d8cd12a]
37+
description = "discards everything"
38+
39+
[ca990fdd-08c2-4f95-aa50-e0f5e1d6802b]
40+
description = "discards nothing"
41+
42+
[71595dae-d283-48ca-a52b-45fa96819d2f]
43+
description = "discards first and last"
44+
45+
[ae141f79-f86d-4567-b407-919eaca0f3dd]
46+
description = "discards neither first nor last"
47+
48+
[daf25b36-a59f-4f29-bcfe-302eb4e43609]
49+
description = "discards strings"
50+
51+
[a38d03f9-95ad-4459-80d1-48e937e4acaf]
52+
description = "discards lists"

exercises/practice/strain/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Exercism
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import "wren-package" for WrenPackage, Dependency
2+
import "os" for Process
3+
4+
class Package is WrenPackage {
5+
construct new() {}
6+
name { "exercism/strain" }
7+
dependencies {
8+
return [
9+
Dependency.new("wren-testie", "0.3.0", "https://github.com/joshgoebel/wren-testie.git")
10+
]
11+
}
12+
}
13+
14+
Package.new().default()
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import "wren-testie/testie" for Testie, Expect
2+
import "./strain" for Strain
3+
4+
Testie.test("Strain") { |do, skip|
5+
do.describe("keep") {
6+
do.test("keep on empty list returns empty list") {
7+
var actual = Strain.keep([]) { |x| true }
8+
Expect.value(actual).toEqual([])
9+
}
10+
11+
skip.test("keeps everything") {
12+
var actual = Strain.keep([1, 3, 5]) { |x| true }
13+
Expect.value(actual).toEqual([1, 3, 5])
14+
}
15+
16+
skip.test("keeps nothing") {
17+
var actual = Strain.keep([1, 3, 5]) { |x| false }
18+
Expect.value(actual).toEqual([])
19+
}
20+
21+
skip.test("keeps first and last") {
22+
var actual = Strain.keep([1, 2, 3]) { |x| x % 2 == 1 }
23+
Expect.value(actual).toEqual([1, 3])
24+
}
25+
26+
skip.test("keeps neither first nor last") {
27+
var actual = Strain.keep([1, 2, 3]) { |x| x % 2 == 0 }
28+
Expect.value(actual).toEqual([2])
29+
}
30+
31+
skip.test("keeps strings") {
32+
var list = ["apple", "zebra", "banana", "zombies", "cherimoya", "zealot"]
33+
var actual = Strain.keep(list) { |x| x.startsWith("z") }
34+
var expected = ["zebra", "zombies", "zealot"]
35+
Expect.value(actual).toEqual(expected)
36+
}
37+
38+
skip.test("keeps lists") {
39+
var list = [
40+
[1, 2, 3],
41+
[5, 5, 5],
42+
[5, 1, 2],
43+
[2, 1, 2],
44+
[1, 5, 2],
45+
[2, 2, 1],
46+
[1, 2, 5]
47+
]
48+
var actual = Strain.keep(list) { |x| x.contains(5) }
49+
var expected = [
50+
[5, 5, 5],
51+
[5, 1, 2],
52+
[1, 5, 2],
53+
[1, 2, 5]
54+
]
55+
Expect.value(actual).toEqual(expected)
56+
}
57+
}
58+
59+
do.describe("discard") {
60+
skip.test("discard on empty list returns empty list") {
61+
var actual = Strain.discard([]) { |x| true }
62+
Expect.value(actual).toEqual([])
63+
}
64+
65+
skip.test("discards everything") {
66+
var actual = Strain.discard([1, 3, 5]) { |x| true }
67+
Expect.value(actual).toEqual([])
68+
}
69+
70+
skip.test("discards nothing") {
71+
var actual = Strain.discard([1, 3, 5]) { |x| false }
72+
Expect.value(actual).toEqual([1, 3, 5])
73+
}
74+
75+
skip.test("discards first and last") {
76+
var actual = Strain.discard([1, 2, 3]) { |x| x % 2 == 1 }
77+
Expect.value(actual).toEqual([2])
78+
}
79+
80+
skip.test("discards neither first nor last") {
81+
var actual = Strain.discard([1, 2, 3]) { |x| x % 2 == 0 }
82+
Expect.value(actual).toEqual([1, 3])
83+
}
84+
85+
skip.test("discards strings") {
86+
var list = ["apple", "zebra", "banana", "zombies", "cherimoya", "zealot"]
87+
var actual = Strain.discard(list) { |x| x.startsWith("z") }
88+
var expected = ["apple", "banana", "cherimoya"]
89+
Expect.value(actual).toEqual(expected)
90+
}
91+
92+
skip.test("discards lists") {
93+
var list = [
94+
[1, 2, 3],
95+
[5, 5, 5],
96+
[5, 1, 2],
97+
[2, 1, 2],
98+
[1, 5, 2],
99+
[2, 2, 1],
100+
[1, 2, 5]
101+
]
102+
var actual = Strain.discard(list) { |x| x.contains(5) }
103+
var expected = [
104+
[1, 2, 3],
105+
[2, 1, 2],
106+
[2, 2, 1]
107+
]
108+
Expect.value(actual).toEqual(expected)
109+
}
110+
}
111+
}
112+

exercises/practice/strain/strain.wren

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Strain {
2+
static keep(list, predicate) {
3+
Fiber.abort("Remove this statement and implement this function")
4+
}
5+
6+
static discard(list, predicate) {
7+
Fiber.abort("Remove this statement and implement this function")
8+
}
9+
}

0 commit comments

Comments
 (0)