Please enable JavaScript to view the comments powered by Disqus.Gleam: Ints, Floats, Modules
Search
🧩

Gleam: Ints, Floats, Modules

태그
Gleam
공개여부
작성일자
2024/04/15
Elixir 는 type이 없어서 불편했는데 gleam은 elixir 의 컨셉도 지키면서 type이 존재하여 공부하게 되었다.
Gleam에 대한 한글 문서가 없어서 excercism에서 나오는 개념들을 한글로 번역해야겠다는 생각이 들어 포스팅을 시작한다.
let integer = 3
Elixir
복사
let float = 3.45 // -> 3.45
Elixir
복사
Floats are numbers with one or more digits behind the decimal separator
1.0 +. 1.4 // -> 2.4 5.0 -. 1.5 // -> 3.5 5.0 /. 2.0 // -> 2.5 3.0 *. 3.1 // -> 9.3 2.0 >. 1.0 // -> True 2.0 <. 1.0 // -> False 2.0 >=. 1.0 // -> True 2.0 <=. 1.0 // -> False
Elixir
복사

Convert pence to pounds

Currently, the price is stored as an integer number of pence (the bike shop is based in the UK). On the website, we want to show the price in pounds, where 1.00 pound amounts to 100 pence. Your first task is to implement the pence_to_pounds function, taking an Int amount of pence, and converting it into its equivalent pounds as a Float. You should also add type annotations for that function.
1.
이 문제는 IntFloat 으로 변경하는 과제가 포함되어 있다.
2.
100.0 을 나눈다.
pounds=pence100.0pounds = \frac{pence}{100.0}
Int 를 float으로 바꾸기
import gleam/int int.to_float(pence) /. 100.0
Elixir
복사

Convert pence to pounds

Currently, the price is stored as an integer number of pence (the bike shop is based in the UK). On the website, we want to show the price in pounds, where 1.00 pound amounts to 100 pence. Your first task is to implement the pence_to_pounds function, taking an Int amount of pence, and converting it into its equivalent pounds as a Float. You should also add type annotations for that function.
이 문제는 String + Float concat 을 실행한다.
pub fn pounds_to_string(pounds: Float) -> String { "£" |> string.append(float.to_string(pounds)) }
Elixir
복사
람다를 이용해 concat을 해야한다.
append 를 실행하다 보면 argument를 하나만 받아야 하는데 두개를 받는다는 에러 로그가 나온다.

String interpolation

name |> string.append("'s score: ") |> string.append(int.to_string(score)) |> string.append("!")
Elixir
복사
이 결과는 아래와 같다.
"#{name}'s score is #{score:int}!"
Elixir
복사
"#{name}'s score is #int{score}!"
Elixir
복사
"$name's score is $score:int!"
Elixir
복사
"${name}'s score is ${score:int}!"
Elixir
복사
"${name}'s score is $int{score}!"
Elixir
복사