-
-
Notifications
You must be signed in to change notification settings - Fork 3
add strain
#96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add strain
#96
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| return { | ||
| default = { | ||
| ROOT = { '.' } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Instructions | ||
|
|
||
| Implement the `keep` and `discard` operation on collections. | ||
| 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. | ||
|
|
||
| For example, given the collection of numbers: | ||
|
|
||
| - 1, 2, 3, 4, 5 | ||
|
|
||
| And the predicate: | ||
|
|
||
| - is the number even? | ||
|
|
||
| Then your keep operation should produce: | ||
|
|
||
| - 2, 4 | ||
|
|
||
| While your discard operation should produce: | ||
|
|
||
| - 1, 3, 5 | ||
|
|
||
| Note that the union of keep and discard is all the elements. | ||
|
|
||
| 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. | ||
|
|
||
| ## Restrictions | ||
|
|
||
| Keep your hands off that filter/reject/whatchamacallit functionality provided by your standard library! | ||
| Solve this one yourself using other basic tools instead. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "authors": [ | ||
| "BNAndras" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
| "strain.moon" | ||
| ], | ||
| "test": [ | ||
| "strain_spec.moon" | ||
| ], | ||
| "example": [ | ||
| ".meta/example.moon" | ||
| ] | ||
| }, | ||
| "blurb": "Implement the `keep` and `discard` operation on collections.", | ||
| "source": "Conversation with James Edward Gray II", | ||
| "source_url": "http://graysoftinc.com/" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| keep: (list, predicate) -> | ||
| [item for item in *list when predicate(item)] | ||
|
|
||
| discard: (list, predicate) -> | ||
| [item for item in *list when not predicate(item)] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| format_predicate = (pred) -> | ||
| switch pred | ||
| when "fn(x) -> true" | ||
| "(_) -> true" | ||
| when "fn(x) -> false" | ||
| "(_) -> false" | ||
| when "fn(x) -> x % 2 == 1" | ||
| "(num) -> num % 2 == 1" | ||
| when "fn(x) -> x % 2 == 0" | ||
| "(num) -> num % 2 == 0" | ||
| when "fn(x) -> contains(x, 5)" | ||
| "(list) -> contains list, 5" | ||
| when "fn(x) -> starts_with(x, 'z')" | ||
| "(str) -> starts_with str, 'z'" | ||
| else | ||
| pred | ||
|
|
||
| format_list = (list) -> | ||
| if #list == 0 | ||
| "{}" | ||
| elseif type(list[1]) == 'string' | ||
| "{" .. table.concat([quote(elem) for elem in *list], ', ') .. "}" | ||
| else | ||
| "{" .. table.concat(list, ', ') .. "}" | ||
|
|
||
| format_value = (val, level) -> | ||
| if #val == 0 | ||
| '{}' | ||
| elseif type(val[1]) == 'table' | ||
| rows = [indent format_list(row), level + 1 for row in *val] | ||
| table.insert rows, 1, '{' | ||
| table.insert rows, indent('}', level) | ||
| table.concat rows, '\n' | ||
| else | ||
| format_list(val) | ||
|
|
||
| { | ||
| module_name: 'Strain' | ||
|
|
||
| test_helpers: [[ | ||
|
|
||
| starts_with = (str, prefix) -> | ||
| str\sub(1, #prefix) == prefix | ||
|
|
||
| contains = (list, element) -> | ||
| for item in *list | ||
| if item == element | ||
| return true | ||
| false | ||
| ]] | ||
|
|
||
| generate_test: (case, level) -> | ||
|
|
||
|
|
||
| lines = { | ||
| "result = Strain.#{case.property} #{format_value(case.input.list, level)}, #{format_predicate case.input.predicate}" | ||
| "expected = #{format_value(case.expected, level)}" | ||
| "assert.are.same expected, result" | ||
| } | ||
|
|
||
| table.concat [indent line, level for line in *lines], '\n' | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| keep: (list, predicate) -> | ||
| error 'Implement me' | ||
|
|
||
| discard: (list, predicate) -> | ||
| error 'Implement me' | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| Strain = require 'strain' | ||
|
|
||
| describe 'strain', -> | ||
| starts_with = (str, prefix) -> | ||
| str\sub(1, #prefix) == prefix | ||
|
|
||
| contains = (list, element) -> | ||
| for item in *list | ||
| if item == element | ||
| return true | ||
| false | ||
|
|
||
| it 'keep on empty list returns empty list', -> | ||
| result = Strain.keep {}, (_) -> true | ||
| expected = {} | ||
| assert.are.same expected, result | ||
|
|
||
| pending 'keeps everything', -> | ||
| result = Strain.keep {1, 3, 5}, (_) -> true | ||
| expected = {1, 3, 5} | ||
| assert.are.same expected, result | ||
|
|
||
| pending 'keeps nothing', -> | ||
| result = Strain.keep {1, 3, 5}, (_) -> false | ||
| expected = {} | ||
| assert.are.same expected, result | ||
|
|
||
| pending 'keeps first and last', -> | ||
| result = Strain.keep {1, 2, 3}, (num) -> num % 2 == 1 | ||
| expected = {1, 3} | ||
| assert.are.same expected, result | ||
|
|
||
| pending 'keeps neither first nor last', -> | ||
| result = Strain.keep {1, 2, 3}, (num) -> num % 2 == 0 | ||
| expected = {2} | ||
| assert.are.same expected, result | ||
|
|
||
| pending 'keeps strings', -> | ||
| result = Strain.keep {'apple', 'zebra', 'banana', 'zombies', 'cherimoya', 'zealot'}, (str) -> starts_with str, 'z' | ||
| expected = {'zebra', 'zombies', 'zealot'} | ||
| assert.are.same expected, result | ||
|
|
||
| pending 'keeps lists', -> | ||
| result = Strain.keep { | ||
| {1, 2, 3} | ||
| {5, 5, 5} | ||
| {5, 1, 2} | ||
| {2, 1, 2} | ||
| {1, 5, 2} | ||
| {2, 2, 1} | ||
| {1, 2, 5} | ||
| }, (list) -> contains list, 5 | ||
| expected = { | ||
| {5, 5, 5} | ||
| {5, 1, 2} | ||
| {1, 5, 2} | ||
| {1, 2, 5} | ||
| } | ||
| assert.are.same expected, result | ||
|
|
||
| pending 'discard on empty list returns empty list', -> | ||
| result = Strain.discard {}, (_) -> true | ||
| expected = {} | ||
| assert.are.same expected, result | ||
|
|
||
| pending 'discards everything', -> | ||
| result = Strain.discard {1, 3, 5}, (_) -> true | ||
| expected = {} | ||
| assert.are.same expected, result | ||
|
|
||
| pending 'discards nothing', -> | ||
| result = Strain.discard {1, 3, 5}, (_) -> false | ||
| expected = {1, 3, 5} | ||
| assert.are.same expected, result | ||
|
|
||
| pending 'discards first and last', -> | ||
| result = Strain.discard {1, 2, 3}, (num) -> num % 2 == 1 | ||
| expected = {2} | ||
| assert.are.same expected, result | ||
|
|
||
| pending 'discards neither first nor last', -> | ||
| result = Strain.discard {1, 2, 3}, (num) -> num % 2 == 0 | ||
| expected = {1, 3} | ||
| assert.are.same expected, result | ||
|
|
||
| pending 'discards strings', -> | ||
| result = Strain.discard {'apple', 'zebra', 'banana', 'zombies', 'cherimoya', 'zealot'}, (str) -> starts_with str, 'z' | ||
| expected = {'apple', 'banana', 'cherimoya'} | ||
| assert.are.same expected, result | ||
|
|
||
| pending 'discards lists', -> | ||
| result = Strain.discard { | ||
| {1, 2, 3} | ||
| {5, 5, 5} | ||
| {5, 1, 2} | ||
| {2, 1, 2} | ||
| {1, 5, 2} | ||
| {2, 2, 1} | ||
| {1, 2, 5} | ||
| }, (list) -> contains list, 5 | ||
| expected = { | ||
| {1, 2, 3} | ||
| {2, 1, 2} | ||
| {2, 2, 1} | ||
| } | ||
| assert.are.same expected, result | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be more readable like:
and the prediate variable can be more descriptive, like
I'll look at the generator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I definitely agree. A little deviance from the problem specs names is fine if it improves readability.