// 02. effect 변수와 구문 수준 제한 // // 파라미터 위치의 effect 절은 `effects <변수>` 또는 `effects {리터럴}`만 가능하다. // 합집합은 결과 위치에서만 쓸 수 있다 — 문법이 그렇게 생겼다. pub fn map[a, b, e: effects]( xs: List[a], f: fn(a) effects e -> b, ) effects e -> List[b] pub fn each[a, e: effects]( xs: List[a], f: fn(a) effects e, ) effects e pub fn fold[a, acc, e: effects]( xs: List[a], init: acc, f: fn(acc, a) effects e -> acc, ) effects e -> acc // 결과 위치의 합집합. 분해되지 않으므로 결정적이다. pub fn map_then_each[a, b, e1: effects, e2: effects]( xs: List[a], f: fn(a) effects e1 -> b, g: fn(b) effects e2, ) effects e1 | e2 { each(map(xs, f), g) } // 클로저를 저장하는 sink는 own으로 유표기한다. 빌린 값을 넘길 수 없다. pub fn register(handler: own fn()) effects {Registry.add} // 호출 지점: e는 인자의 시그니처에서 읽어온다. 추론이 아니라 읽기. pub fn total_size( fs: FileSystem, paths: List[Path], ) effects {FileSystem.stat} -> Int { fold(paths, 0, fn(acc, p) { acc + fs.stat(p).size }) } // effect-free 클로저는 effects 절을 생략한다 (기본값 {}, 추론 아님). pub fn lengths(xs: List[String]) -> List[Int] { map(xs, fn(s) { String.len(s) }) } // 합집합 위치는 명시적 인스턴스화를 요구한다. 후위 대괄호가 그 문법이다. pub fn explicit_call(fs: FileSystem, paths: List[Path]) effects {FileSystem.stat} -> List[Int] { map[Path, Int, {FileSystem.stat}](paths, fn(p) { fs.stat(p).size }) }