键盘快捷键

使用 跳转章节

使用 S/ 在本书内搜索

使用 ? 显示帮助页面

使用 Esc 隐藏帮助页面

Delete

Delete[object, key]

Deletes the property identified by key from object. The original object is not modified; a new Object is returned. If key does not exist, the returned object is identical to the original.

  • Returns a new Object with the specified key removed.
Let[a, Object[A, { { #a, 10 } }]];
Print[a];  (* => Object[A]{a} *)

Delete[a, #a];  (* => Object[A]{} *)
Print[a];       (* => Object[A]{a} *)

Let[a, Delete[a, #a]];

Div

Div[x, y]

Performs floating-point division of x by y.

  • Returns a Number representing $\frac{x}{y}$.
  • Following f64 semantics, the result may be inf if it overflows.
  • An error is raised if y is zero.
Div[10, 4];      (* => 2.5 *)
Div[1, 1e-308];  (* => 1e+308 *)
Div[1, 1e-312];  (* => inf *)

Div[1, 1e-324];
(*? Error[ksl::builtin::Div]: Division by zero. *)

Do

Do[expression, symbol, list]

Executes expression for each element in list, binding the current element to symbol during each iteration. Primarily used for side effects.

  • Always returns Unit[].
  • symbol is local to the loop and does not leak into the surrounding scope.
Do[Print[n, " -> "], n, { 1, 2, 3, 5, 8, 13 }]; Print["S"];
(* => 1 -> 2 -> 3 -> 5 -> 8 -> 13 -> S *)
Let[sum, 0];
Do[Update[sum, Add[sum, n]], n, { 1, 2, 3, 5, 8, 13 }];
Print[sum];  (* => 32 *)
Has[n];      (* => #f *)

Drop

Drop[list, count]

Returns a new list consisting of the elements of list after removing the first count elements. The original list is unchanged.

  • Returns a List. If count is greater than or equal to the length of list, returns { }.
  • An error is raised if count is negative.
Drop[{ 1, 2, 3 }, 2];   (* => {3} *)
Drop[{ 1, 2, 3 }, 3];   (* => {} *)
Drop[{ 1, 2, 3 }, 4];   (* => {} *)

Drop[{ 1, 2, 3 }, -1];
(*? Error[ksl::builtin::Drop]: Unexpected value: `-1`. *)

DropReceiver

DropReceiver[receiver]

Disconnects and drops the receiver endpoint (MpscReceiver) of a channel. Manually releasing native resources is recommended for long-running scripts to avoid leaks.

  • Returns Unit[].
  • As a side effect, rebinds receiver to Unit[] in the current scope.
DropReceiver[rx];

DropSender

DropSender[sender]

Disconnects and drops the sender endpoint (MpscSender) of a channel. Manually releasing native resources is recommended for long-running scripts to avoid leaks.

  • Returns Unit[].
  • As a side effect, rebinds sender to Unit[] in the current scope.
DropSender[tx];

Channel Lifecycle Example

The following example demonstrates the complete lifecycle of a channel, including creation, message passing, consumption of threads, and manual resource cleanup.

OpenChannel[rx, tx];

Thread[logger, { rx }, Block[
  Let[msg, #nil],
  While[Not[Eq[msg, #stop]], {
    Update[msg, ReceiveValue[rx]],
    Or[Eq[msg, #stop],
      Print[Concat["[Logger] << ", If[IsString[msg], msg, ToString[msg]]]]]
  }],
  Print["[Logger] Connection closed. Mamba Out!"]
]];

Thread[workerA, { tx }, Block[
  SendValue[tx, 101], Sleep[0.1],
  SendValue[tx, 102], Sleep[0.1],
  SendValue[tx, 103]
]];

Thread[workerB, { tx }, Block[
  Sleep[0.05], SendValue[tx, "USER_LOGIN"],
  Sleep[0.1],  SendValue[tx, "USER_ACTION"]
]];

SendValue[tx, "SYSTEM_READY"];

Let[_, Consume[workerA]];
Let[_, Consume[workerB]];

SendValue[tx, #stop];
Let[_, Consume[logger]];

DropReceiver[rx];
DropSender[tx];

(* =>
[Logger] << SYSTEM_READY
[Logger] << 101
[Logger] << USER_LOGIN
[Logger] << 102
[Logger] << USER_ACTION
[Logger] << 103
[Logger] Connection closed. Mamba Out!
*)