次のようなAPIを備えたコンテナが必要です。
addKeyValuePair(containerRef, [key, value])
searchByKey(containerRef, key) #that would return NULL or `value`
Maple(15)でそのようなものを作成するにはどうすればよいですか?
次のようなAPIを備えたコンテナが必要です。
addKeyValuePair(containerRef, [key, value])
searchByKey(containerRef, key) #that would return NULL or `value`
Maple(15)でそのようなものを作成するにはどうすればよいですか?
テーブルへの通常のインデックス付けが必要な場合があります。以下の以前に割り当てられていない名前は、インデックス付きのエントリに割り当てることによってのみにY
なることに注意してください。table
Y[a]
restart:
Y[a]:=211:
Y[a];
211
Y[b];
Y[b]
assigned(Y[a]);
true
assigned(Y[b]);
false
eval(Y);
table([a = 211])
そうは言っても、説明した機能を取得するためのラッピングはほんの少しです。アクセスすると、まだ割り当てられていない場合にY[b]
戻ります。NULL
Y[b]
Mapleではより一般的にインデックスを作成できるためkey
、タイプのエラーチェックと制限をカスタマイズ(または削除)できます。symbol
table
restart:
addKeyValuePair:=proc(containerRef::{indexed,symbol}, L::list)
if nops(L) <> 2 then
error "expecting list with two entries"; end if;
if not type(L[1], symbol) then
error "expecting list with symbol as first entry"; end if;
containerRef[L[1]]:=L[2];
NULL;
end proc:
searchByKey:=proc(containerRef::{indexed,symbol}, key::name)
if assigned(containerRef[key]) then
containerRef[key];
else
NULL;
end if;
end proc:
addKeyValuePair( Y, [a, 4.5] );
searchByKey( Y, a );
4.5
searchByKey( Y, b );
searchByKey( Q, b );
addKeyValuePair( Y, [a, 211] );
searchByKey( Y, a );
211
addKeyValuePair( Y, [c] );
Error, (in addKeyValuePair) expecting list with two entries
addKeyValuePair( Y, [2.34, 211] );
Error, (in addKeyValuePair) expecting list with symbol as first entry
Atable
は可変データ構造です。これはlast_name_eval型であり、その結果、(事実上)プロシージャ呼び出しの引数として「参照によって」渡されます。