Skip to content

Commit 9e7f9fc

Browse files
committed
stdlib: Use new destructuring assignment syntax
1 parent 5b5f39c commit 9e7f9fc

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

src/stdlib/btc.minsc

+7-7
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ fn tx::sighash::tr_leaf($tx, $vin, $script_leaf, $utxos, $sighash_ty) = psbt::si
6363
// TOOD tx::sighash::sw0() with just the signed utxo & tx::sighash::presw() with just the utxo's script
6464

6565
fn psbt::finalize_witness($psbt, $input_witnesses) = psbt::update($psbt, [
66-
"inputs": map($input_witnesses, |$in_wit|
67-
$in_wit.0: [ "final_script_witness": $in_wit.1 ]
66+
"inputs": map($input_witnesses, |[ $input_index, $witness ]|
67+
$input_index: [ "final_script_witness": $witness ]
6868
)
6969
]);
7070

@@ -197,7 +197,7 @@ fn nFromAlt($max_n) = `
197197
// ]
198198
fn ifelseif($clauses) = if isEmpty($clauses) then `` else `
199199
// Check all clauses but the last, running the first one that matches
200-
map(initial($clauses), |$clause| `$clause.0 OP_IF $clause.1 OP_ELSE`)
200+
map(initial($clauses), |[ $condition, $body ]| `$condition OP_IF $body OP_ELSE`)
201201

202202
// The last clause is checked using OP_VERIFY instead of another IF..ELSE to save some bytes.
203203
{ if last($clauses).0 != default then `last($clauses).0 OP_VERIFY` else ``}
@@ -216,7 +216,7 @@ fn match($clauses) = if isEmpty($clauses) then `` else
216216
ifelseif(
217217
// DUP the value being matched to keep it available for the next clause, while allowing condition
218218
// scripts to consume it (with e.g. OP_EQUAL). It will be removed before the matching script is run.
219-
map(initial($clauses), |$clause| `OP_DUP $clause.0`: `OP_DROP $clause.1`)
219+
map(initial($clauses), |[ $condition, $body ]| `OP_DUP $condition`: `OP_DROP $body`)
220220

221221
// The last clause doesn't need to DUP/DROP as there are no more conditions that need the value.
222222
// In the case of a `default` branch, the value will remain available on stack.
@@ -225,9 +225,9 @@ fn match($clauses) = if isEmpty($clauses) then `` else
225225

226226
// Match the item at the top of the stack for equality.
227227
// For example: switch[ 0: `$alice_pk OP_CHECKSIG`, 1: `<6 months> OP_CSV OP_DROP` ]
228-
fn switch($clauses) = match(map($clauses, |$clause| {
229-
$condition = if $clause.0 != default then `$clause.0 OP_EQUAL` else default;
230-
$condition: $clause.1
228+
fn switch($clauses) = match(map($clauses, |[ $condition, $body ]| {
229+
$condition = if $condition != default then `$condition OP_EQUAL` else default;
230+
$condition: $body
231231
}));
232232

233233
// Pop an index number off the stack and execute the `$scripts` branch with that index.

src/stdlib/elements.minsc

+3-4
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@ fn checkOutput($vout, $asset_id, $amount, $spk_tup) = `
5252
checkOutputSpk($vout, $spk_tup)
5353
`;
5454

55-
fn checkOutputSpk($vout, $spk_tup) = `
56-
// $spk_tup is a tuple of the witness version and program
55+
fn checkOutputSpk($vout, [ $witness_version, $witness_prog ]) = `
5756
$vout OP_INSPECTOUTPUTSCRIPTPUBKEY
58-
$spk_tup.0 OP_EQUALVERIFY
59-
$spk_tup.1 OP_EQUALVERIFY
57+
$witness_version OP_EQUALVERIFY
58+
$witness_prog OP_EQUALVERIFY
6059
`;
6160

6261
fn checkSameAsset($vin, $vout) = `

src/stdlib/stdlib.minsc

+7-9
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ fn reduce($arr, $fn) = if !isEmpty($arr) then fold(tail($arr), $arr.0, $fn) else
3030
fn filterMap($arr, $fn) = flatMap($arr, |$el| { $r = $fn($el); if $r != filterMap::skip then [$r] else [] });
3131
filterMap::skip = symbol("filterMap::skip");
3232

33-
fn find($arr, $fn) = foldUntil($arr, null, |$acc, $el| if $fn($el) then true:$el else false:null);
34-
fn some($arr, $fn) = foldUntil($arr, false, |$acc, $el| if $fn($el) then true:true else false:false);
33+
fn find($arr, $fn) = foldUntil($arr, null, |$acc, $el| if $fn($el) then true:$el else false:null);
34+
fn some($arr, $fn) = foldUntil($arr, false, |$acc, $el| if $fn($el) then true:true else false:false);
3535
fn every($arr, $fn) = !some($arr, |$el| !$fn($el));
3636

3737
fn contains($arr, $needle) = some($arr, |$el| $el == $needle);
@@ -59,9 +59,8 @@ fn join($strs, $sep) =
5959
else if len($strs) == 1 then $strs.0
6060
else concat([ $strs.0 ] + map(tail($strs), |$str| $sep + $str));
6161

62-
// Array tag extraction with multiple values (`$tagged->$tag` can be used for a single value)
63-
fn t::multi($tagged, $tag) = $tagged | filter(|$kv| $kv.0 == $tag) | map(|$kv| $kv.1);
64-
62+
// Array tag extraction with multiple values (`$tagged->tag` can be used for a single value)
63+
fn t::multi($tagged, $tag) = $tagged | filter(|[$k, _]| $k == $tag) | map(|[_, $v]| $v);
6564

6665
//
6766
// Dev utilities
@@ -73,12 +72,11 @@ fn assert::eq($a, $b) = ($a == $b) || throw("Not equal: "+$a+" != "+$b+"");
7372
fn assert::msg($bool, $msg) = $bool || throw($msg);
7473

7574
// Scope inspection
76-
dyn fn env::pretty() = env() | map(|$kv| $kv.0+" = "+pretty($kv.1)+" // "+typeof($kv.1)) | join("\n\n")| symbol();
77-
dyn fn env::repr() = env() | filter(|$kv| !isFunction($kv.1)) | map(|$kv| $kv.0+" = "+repr($kv.1)+"; // "+typeof($kv.1)) | join("\n\n") | symbol();
78-
dyn fn env::debug() = env() | map(|$kv| $kv.0+" = "+_debug_nonfn($kv.1)) | join("\n\n") | symbol();
75+
dyn fn env::pretty() = env() | map(|[$k:$v]| $k+" = "+pretty($v)+" // "+typeof($v)) | join("\n\n") | symbol();
76+
dyn fn env::repr() = env() | filter(|[$k:$v]| !isFunction($v)) | map(|[$k:$v]| $k+" = "+repr($v)+"; // "+typeof($v)) | join("\n\n") | symbol();
77+
dyn fn env::debug() = env() | map(|[$k:$v]| $k+" = "+_debug_nonfn($v)) | join("\n\n") | symbol();
7978
fn _debug_nonfn($v) = if !isFunction($v) then debug($v) else $v;
8079

81-
8280
dyn fn log::env($label) = log("variables:\n----\n" + env::pretty() + "\n----");
8381
dyn fn log::debug_env($label) = log("variables:\n----\n" + env::debug() + "\n----");
8482

0 commit comments

Comments
 (0)