-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add example project with parallel processing and update .gitign…
…ore files
- Loading branch information
1 parent
84a525e
commit c271ccb
Showing
12 changed files
with
224 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
*.ez | ||
/build | ||
erl_crash.dump | ||
INTERNAL.md | ||
INTERNAL.md | ||
.vscode |
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
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,4 @@ | ||
*.beam | ||
*.ez | ||
/build | ||
erl_crash.dump |
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,10 @@ | ||
name = "example" | ||
version = "1.0.0" | ||
description = "Gleam Stream wxample" | ||
|
||
[dependencies] | ||
gleam_stdlib = ">= 0.34.0 and < 2.0.0" | ||
gs = { path = "../.." } | ||
|
||
[dev-dependencies] | ||
gleeunit = ">= 1.0.0 and < 2.0.0" |
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,15 @@ | ||
# This file was generated by Gleam | ||
# You typically do not need to edit this file | ||
|
||
packages = [ | ||
{ name = "gleam_erlang", version = "0.33.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "A1D26B80F01901B59AABEE3475DD4C18D27D58FA5C897D922FCB9B099749C064" }, | ||
{ name = "gleam_otp", version = "0.16.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "FA0EB761339749B4E82D63016C6A18C4E6662DA05BAB6F1346F9AF2E679E301A" }, | ||
{ name = "gleam_stdlib", version = "0.52.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "50703862DF26453B277688FFCDBE9DD4AC45B3BD9742C0B370DB62BC1629A07D" }, | ||
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" }, | ||
{ name = "gs", version = "1.1.0-beta.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], source = "local", path = "../.." }, | ||
] | ||
|
||
[requirements] | ||
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" } | ||
gleeunit = { version = ">= 1.0.0 and < 2.0.0" } | ||
gs = { path = "../.." } |
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,63 @@ | ||
import gleam/erlang | ||
import gleam/erlang/process | ||
import gleam/float | ||
import gleam/int | ||
import gleam/io | ||
import gs | ||
import gs/par | ||
|
||
pub fn measure(label: String, f: fn() -> a) -> a { | ||
let start = erlang.system_time(erlang.Millisecond) | ||
let result = f() | ||
let end = erlang.system_time(erlang.Millisecond) | ||
let duration = end - start | ||
|
||
io.println(label <> " took " <> int.to_string(duration) <> "ms") | ||
|
||
result | ||
} | ||
|
||
fn new_point() -> #(Float, Float) { | ||
#(float.random(), float.random()) | ||
} | ||
|
||
fn is_inside_circle(point: #(Float, Float)) -> Bool { | ||
let #(x, y) = point | ||
x *. x +. y *. y <=. 1.0 | ||
} | ||
|
||
pub fn main() { | ||
let num = 10 | ||
|
||
measure("Parallel", fn() { | ||
let a = | ||
gs.from_counter(1) | ||
|> par.par_map(6, fn(num) { #(num, new_point()) }) | ||
|> gs.filter(fn(num_point) { is_inside_circle(num_point.1) }) | ||
|> gs.count() | ||
|> gs.map(fn(e) { #(e.1, e.0.0) }) | ||
|> par.par_map(6, fn(element) { | ||
4.0 *. int.to_float(element.0) /. int.to_float(element.1) | ||
}) | ||
|> gs.take(num) | ||
|> gs.to_last | ||
|
||
io.debug(a) | ||
}) | ||
|
||
measure("Sequential", fn() { | ||
let b = | ||
gs.from_counter(1) | ||
|> gs.map(fn(num) { #(num, new_point()) }) | ||
|> gs.filter(fn(num_point) { is_inside_circle(num_point.1) }) | ||
|> gs.count() | ||
|> gs.map(fn(e) { #(e.1, e.0.0) }) | ||
|> gs.map(fn(element) { | ||
4.0 *. int.to_float(element.0) /. int.to_float(element.1) | ||
}) | ||
|> gs.take(num) | ||
|> gs.to_last | ||
|
||
io.debug(b) | ||
}) | ||
} |
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
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
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,39 @@ | ||
import gleam/erlang/process.{type Subject} | ||
import gleam/option.{type Option, None, Some} | ||
import gleam/otp/actor | ||
|
||
pub type Message(a, b) { | ||
Dispatch(Subject(Bool), a) | ||
GetResult(Subject(b)) | ||
} | ||
|
||
type State(b) { | ||
State(result: Option(b)) | ||
} | ||
|
||
pub fn start(f: fn(a) -> b) { | ||
actor.start(State(result: None), handle_message(f)) | ||
} | ||
|
||
fn handle_message(f: fn(a) -> b) { | ||
fn(msg: Message(a, b), state: State(b)) -> actor.Next(Message(a, b), State(b)) { | ||
case msg { | ||
Dispatch(reply_with, item) -> { | ||
process.send(reply_with, True) | ||
actor.continue(State(result: Some(f(item)))) | ||
} | ||
|
||
GetResult(reply_with) -> { | ||
case state.result { | ||
Some(result) -> { | ||
process.send(reply_with, result) | ||
actor.Stop(process.Normal) | ||
} | ||
None -> { | ||
actor.continue(state) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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
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