// Partial application captures the arguments it is given, whatever their // shape: a field, a call, a dictionary, a capability, a function value // (issue #321). Passed where its parameter's type declares an effect, it // receives that handler from its caller, as the lambda `fn n { f a n }` // would (ADR 0062, ADR 0010). import std/io import std/list type Box { Box(prefix: String) } type Fns { Fns(f: fn(String, Int) -> String) } effect log { fn log :: msg: String -> () } effect ask { fn ask :: -> Int } effect note { fn note :: tag: String n: Int -> () } fn tag :: prefix: String n: Int -> String { prefix <> show n } fn join3 :: a: String b: String n: Int -> String { a <> b <> show n } fn ident :: s: String -> String { s } fn pair :: x: a n: Int -> String where Show { show x <> ":" <> show n } fn tagged :: prefix: String n: Int -> String / { log ("tag " <> show n) prefix <> show (n + ask ()) } fn outer :: p: String -> List / { list.map [1, 2] (tagged p) } fn asked :: p: String -> List / { handle { outer p } with { ask -> 10 } } fn logged :: p: String -> List / { handle { asked p } with { log m -> io.println m } } fn apply :: f: fn(String, Int) -> String -> List { list.map [1, 2] (f "#") } fn logs :: prefix: String n: Int -> String / { log ("logs " <> show n) prefix <> show n } fn run :: g: fn(Int) -> String / -> List / { handle { list.map [1, 2] fn n { g n } } with { log m -> io.println ("run: " <> m) } } fn run_notes :: g: fn(Int) -> () / -> () / { handle { list.each [1, 2] fn n { g n } } with { note t n -> io.println ("run_notes: " <> t <> show n) } } fn relay :: f: fn(String, Int) -> String / -> List / { run (f "&") } fn notes :: box: Box -> () / { list.each [1, 2] (note box.prefix) } pub fn main :: -> () / { let box = Box("#") io.println (show (list.map [1, 2] (tag box.prefix))) //> [#1, #2] let g = tag box.prefix io.println (g 3) //> #3 io.println (show (list.map [4] (tag (ident box.prefix)))) //> [#4] let h = join3 box.prefix let k = h (ident "-") io.println (k 5) //> #-5 let a = ident "outer a" let b = ident "outer b" let f = fn n { let inner = Box("@") list.map [n] (tag inner.prefix) } io.println (show (f 6)) //> [@6] io.println (a <> ", " <> b) //> outer a, outer b io.println (show (list.map [7] (pair True))) //> [True:7] let p = "!" io.println (show (list.map [8] (join3 p p))) //> [!!8] let out = logged "%" //> tag 1 //> tag 2 io.println (show out) //> [%11, %12] io.println (show (apply tag)) //> [#1, #2] let r = Fns(tag) io.println (show (list.map [9] (r.f box.prefix))) //> [#9] handle { notes box } with { note t n -> io.println (t <> "note " <> show n) } //> #note 1 //> #note 2 let q = "$" io.println (show (run (logs q))) //> run: logs 1 //> run: logs 2 //> [$1, $2] run_notes (note box.prefix) //> run_notes: #1 //> run_notes: #2 io.println (show (relay logs)) //> run: logs 1 //> run: logs 2 //> [&1, &2] }