-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add parallel-letter-frequency exercise (#174)
- Loading branch information
Showing
10 changed files
with
436 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
5 changes: 5 additions & 0 deletions
5
exercises/practice/parallel-letter-frequency/.docs/instructions.append.md
This file contains 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 @@ | ||
# ignore | ||
|
||
You can read about concurrency in Wren [in the manual][concurrency]. | ||
|
||
[concurrency]: https://wren.io/concurrency.html |
7 changes: 7 additions & 0 deletions
7
exercises/practice/parallel-letter-frequency/.docs/instructions.md
This file contains 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 @@ | ||
# Instructions | ||
|
||
Count the frequency of letters in texts using parallel computation. | ||
|
||
Parallelism is about doing things in parallel that can also be done sequentially. | ||
A common example is counting the frequency of letters. | ||
Create a function that returns the total frequency of each letter in a list of texts and that employs parallelism. |
17 changes: 17 additions & 0 deletions
17
exercises/practice/parallel-letter-frequency/.meta/config.json
This file contains 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,17 @@ | ||
{ | ||
"authors": [ | ||
"glennj" | ||
], | ||
"files": { | ||
"solution": [ | ||
"parallel-letter-frequency.wren" | ||
], | ||
"test": [ | ||
"parallel-letter-frequency.spec.wren" | ||
], | ||
"example": [ | ||
".meta/proof.ci.wren" | ||
] | ||
}, | ||
"blurb": "Count the frequency of letters in texts using parallel computation." | ||
} |
78 changes: 78 additions & 0 deletions
78
exercises/practice/parallel-letter-frequency/.meta/proof.ci.wren
This file contains 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,78 @@ | ||
// ------------------------------------------------------------------ | ||
class Frequency { | ||
static ofText(text) { | ||
return text.codePoints.reduce(Frequency.new()) {|freq, cp| | ||
var char | ||
if (65 <= cp && cp <= 90) char = String.fromCodePoint(cp + 32) | ||
if (97 <= cp && cp <= 122) char = String.fromCodePoint(cp) | ||
if (char != null) freq.incr(char) | ||
return freq | ||
} | ||
} | ||
|
||
construct new() { | ||
_map = {} | ||
} | ||
|
||
keys { _map.keys } | ||
[key] { _map[key] } | ||
[key]=(value) { _map[key] = value } | ||
|
||
toMap { _map } | ||
toString { _map.toString } | ||
|
||
incr(key) { incr(key, 1) } | ||
incr(key, amount) { _map[key] = (_map.containsKey(key) ? _map[key] : 0) + amount } | ||
|
||
addAll(other) { | ||
if (other is Frequency) { | ||
other.keys.each {|key| | ||
incr(key, other[key]) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// ------------------------------------------------------------------ | ||
class ParallelLetterFrequency { | ||
construct new(inputs) { | ||
_texts = inputs | ||
} | ||
|
||
nonConcurrent() { | ||
return _texts.reduce(Frequency.new()) {|freq, text| | ||
freq.addAll(Frequency.ofText(text)) | ||
return freq | ||
}.toMap | ||
} | ||
|
||
concurrent() { | ||
// create a fiber for each input text | ||
var fibers = _texts.map {|text| | ||
return Fiber.new {|str| | ||
Fiber.yield() // so the first `call` doesn't wait for the result | ||
var freq = Frequency.ofText(str) | ||
Fiber.yield(freq) | ||
} | ||
}.toList | ||
|
||
// then send a piece of text to each fiber | ||
(0..._texts.count).each {|i| | ||
fibers[i].call(_texts[i]) | ||
} | ||
|
||
// then collect the results | ||
var result = Frequency.new() | ||
fibers.each {|fiber| | ||
var freq = fiber.call() | ||
result.addAll(freq) | ||
} | ||
|
||
return result.toMap | ||
} | ||
|
||
calculateFrequencies() { | ||
//return nonConcurrent() | ||
return concurrent() | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
exercises/practice/parallel-letter-frequency/.meta/tests.toml
This file contains 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. | ||
|
||
[c054d642-c1fa-4234-8007-9339f2337886] | ||
description = "no texts" | ||
|
||
[818031be-49dc-4675-b2f9-c4047f638a2a] | ||
description = "one text with one letter" | ||
|
||
[c0b81d1b-940d-4cea-9f49-8445c69c17ae] | ||
description = "one text with multiple letters" | ||
|
||
[708ff1e0-f14a-43fd-adb5-e76750dcf108] | ||
description = "two texts with one letter" | ||
|
||
[1b5c28bb-4619-4c9d-8db9-a4bb9c3bdca0] | ||
description = "two texts with multiple letters" | ||
|
||
[6366e2b8-b84c-4334-a047-03a00a656d63] | ||
description = "ignore letter casing" | ||
|
||
[92ebcbb0-9181-4421-a784-f6f5aa79f75b] | ||
description = "ignore whitespace" | ||
|
||
[bc5f4203-00ce-4acc-a5fa-f7b865376fd9] | ||
description = "ignore punctuation" | ||
|
||
[68032b8b-346b-4389-a380-e397618f6831] | ||
description = "ignore numbers" | ||
|
||
[aa9f97ac-3961-4af1-88e7-6efed1bfddfd] | ||
description = "Unicode letters" | ||
include = false | ||
comment = "wren doesn't have a lot of string manipulation functionality" | ||
comment = "it can't lowercase an arbitrary unicode letter" | ||
|
||
[7b1da046-701b-41fc-813e-dcfb5ee51813] | ||
description = "combination of lower- and uppercase letters, punctuation and white space" | ||
|
||
[4727f020-df62-4dcf-99b2-a6e58319cb4f] | ||
description = "large texts" | ||
|
||
[adf8e57b-8e54-4483-b6b8-8b32c115884c] | ||
description = "many small texts" |
This file contains 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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Exercism | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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,14 @@ | ||
import "wren-package" for WrenPackage, Dependency | ||
import "os" for Process | ||
|
||
class Package is WrenPackage { | ||
construct new() {} | ||
name { "exercism/parallel-letter-frequency" } | ||
dependencies { | ||
return [ | ||
Dependency.new("wren-testie", "0.3.0", "https://github.com/joshgoebel/wren-testie.git") | ||
] | ||
} | ||
} | ||
|
||
Package.new().default() |
211 changes: 211 additions & 0 deletions
211
exercises/practice/parallel-letter-frequency/parallel-letter-frequency.spec.wren
Large diffs are not rendered by default.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
exercises/practice/parallel-letter-frequency/parallel-letter-frequency.wren
This file contains 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,9 @@ | ||
class ParallelLetterFrequency { | ||
construct new(inputs) { | ||
// implement me | ||
} | ||
|
||
calculateFrequencies() { | ||
// implement me | ||
} | ||
} |