Skip to content

Commit

Permalink
chore: Add example for Monte Carlo simulation of Pi using Gleam streams
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatScalaGuy committed Jan 3, 2025
1 parent 522dcb4 commit adeeb9c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/05-pi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump
10 changes: 10 additions & 0 deletions examples/05-pi/gleam.toml
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"
15 changes: 15 additions & 0 deletions examples/05-pi/manifest.toml
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.51.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "14AFA8D3DDD7045203D422715DBB822D1725992A31DF35A08D97389014B74B68" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ name = "gs", version = "0.0.9", 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 = "../.." }
25 changes: 25 additions & 0 deletions examples/05-pi/src/example.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import gleam/float
import gleam/int
import gs

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() {
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.debug()
|> gs.to_nil()
}

0 comments on commit adeeb9c

Please sign in to comment.