Fix Celsius typo

This commit is contained in:
Wesley Moore 2024-02-20 22:57:28 +10:00
parent 1dc3ede6be
commit 7b2f7dfeb8
No known key found for this signature in database

View file

@ -3,7 +3,7 @@ title = "Building a Hybrid Native Application With Gleam and Tauri"
date = 2024-02-19T09:56:49+10:00 date = 2024-02-19T09:56:49+10:00
[extra] [extra]
updated = 2024-02-20T11:12:06+10:00 updated = 2024-02-20T22:57:15+10:00
+++ +++
I took a few hours this weekend to experiment with building a hybrid I took a few hours this weekend to experiment with building a hybrid
@ -47,8 +47,8 @@ pub type Temperature {
C(Float) C(Float)
} }
pub type Celcius { pub type Celsius {
Celcius(Float) Celsius(Float)
} }
pub fn main() { pub fn main() {
@ -56,29 +56,29 @@ pub fn main() {
io.debug(avg(temps)) io.debug(avg(temps))
} }
pub fn avg(measurements: List(Temperature)) -> Celcius { pub fn avg(measurements: List(Temperature)) -> Celsius {
let sum = let sum =
list.fold(measurements, 0.0, fn(sum, val) { list.fold(measurements, 0.0, fn(sum, val) {
let Celcius(c) = to_c(val) let Celsius(c) = to_c(val)
sum +. c sum +. c
}) })
let length = let length =
list.length(measurements) list.length(measurements)
|> int.to_float |> int.to_float
Celcius(sum /. length) Celsius(sum /. length)
} }
fn to_c(temp: Temperature) -> Celcius { fn to_c(temp: Temperature) -> Celsius {
case temp { case temp {
C(c) -> Celcius(c) C(c) -> Celsius(c)
F(f) -> Celcius({ f -. 32.0 } /. 1.8) F(f) -> Celsius({ f -. 32.0 } /. 1.8)
} }
} }
``` ```
When run it outputs: When run it outputs:
Celcius(1.8444444444444443) Celsius(1.8444444444444443)
The generated JavaScript (as of Gleam v1.0.0-rc2) is shown below. While it's The generated JavaScript (as of Gleam v1.0.0-rc2) is shown below. While it's
certainly longer than what you might naively write in JavaScript directly it's certainly longer than what you might naively write in JavaScript directly it's