Phase 12.7: Nyash文法革命とANCP 90%圧縮技法の発見 - 文法改革完了とFunctionBox実装
This commit is contained in:
5
apps/fn-call-demo/main.nyash
Normal file
5
apps/fn-call-demo/main.nyash
Normal file
@ -0,0 +1,5 @@
|
||||
// Lambda immediate call demo
|
||||
local result = (fn(a){ a + 1 })(41)
|
||||
local con = new ConsoleBox()
|
||||
con.println(result) // 42 を期待
|
||||
|
||||
10
apps/fn-capture-demo/main.nyash
Normal file
10
apps/fn-capture-demo/main.nyash
Normal file
@ -0,0 +1,10 @@
|
||||
// Capture demo:
|
||||
// - locals are captured by reference using RefCellBox (use .get/.set)
|
||||
// - non-locals (global/statics) are captured by value (P1)
|
||||
local y = 41
|
||||
local ycell = new RefCellBox(y)
|
||||
local f = fn(x){ ycell.set(ycell.get() + 1); x + ycell.get() }
|
||||
// outer writes through RefCellBox
|
||||
// y itself remains 41; ycell holds the mutable state
|
||||
local con = new ConsoleBox()
|
||||
con.println(f(1)) // 43 (ycell started 41, incremented to 42, x=1 -> 43)
|
||||
8
apps/fn-lambda-demo/main.nyash
Normal file
8
apps/fn-lambda-demo/main.nyash
Normal file
@ -0,0 +1,8 @@
|
||||
// fn{} lambda minimal (value placeholder in P1)
|
||||
local f = fn(a) {
|
||||
// 最後の式が返り値
|
||||
a + 1
|
||||
}
|
||||
local con = new ConsoleBox()
|
||||
// 値として持ち回れる(toStringは関数情報を表示)
|
||||
con.println(f.toString())
|
||||
6
apps/fn-store-and-call/main.nyash
Normal file
6
apps/fn-store-and-call/main.nyash
Normal file
@ -0,0 +1,6 @@
|
||||
// Store lambda into a variable and call later
|
||||
local inc = fn(a){ a + 1 }
|
||||
local x = inc(41)
|
||||
local con = new ConsoleBox()
|
||||
con.println(x) // 42
|
||||
|
||||
19
apps/p2p-function-handler-demo/main.nyash
Normal file
19
apps/p2p-function-handler-demo/main.nyash
Normal file
@ -0,0 +1,19 @@
|
||||
// P2PBox + FunctionBox handler demo
|
||||
|
||||
local alice, bob, i
|
||||
|
||||
// create two nodes (inprocess transport)
|
||||
alice = new P2PBox("alice", "inprocess")
|
||||
bob = new P2PBox("bob", "inprocess")
|
||||
|
||||
// register a FunctionBox handler on bob
|
||||
bob.on("hello", function(intent, from) {
|
||||
print("[bob] received '" + intent.getName() + "' from " + from)
|
||||
})
|
||||
|
||||
// build Intent and send from alice to bob
|
||||
i = new IntentBox("hello", new MapBox())
|
||||
alice.send("bob", i)
|
||||
|
||||
print("done")
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
// peek expression demo (P0: arms are single expressions)
|
||||
// peek expression demo (P1: arms support blocks)
|
||||
local animal = "cat"
|
||||
local sound = peek animal {
|
||||
"dog" => "bark"
|
||||
"cat" => "meow"
|
||||
else => "silent"
|
||||
"dog" => { "bark" }
|
||||
"cat" => { local s = "meow" s }
|
||||
else => { "silent" }
|
||||
}
|
||||
local con = new ConsoleBox()
|
||||
con.println(sound)
|
||||
|
||||
6
apps/result-qmark-demo/main.nyash
Normal file
6
apps/result-qmark-demo/main.nyash
Normal file
@ -0,0 +1,6 @@
|
||||
// ? operator demo with await (Ok path)
|
||||
// await <expr> returns ResultBox Ok(expr) in this minimal path
|
||||
local x = (await 42)?
|
||||
local con = new ConsoleBox()
|
||||
con.println(x)
|
||||
|
||||
Reference in New Issue
Block a user