键盘快捷键

使用 跳转章节

使用 S/ 在本书内搜索

使用 ? 显示帮助页面

使用 Esc 隐藏帮助页面

FileMeta

FileMeta[path]

Retrieves detailed metadata for the file or directory at path.

  • Returns an Object with type tag MetaData.
  • An error is raised if the path does not exist or permissions are insufficient.

The returned object contains the following keys:

KeyValue TypeDescription
#nameStringThe name of the file.
#kindAtom#directory, #symlink, or #unknown.
#sizeNumberThe size in bytes.
#readonlyAtom#t if read-only, otherwise #f.
#extensionStringThe file extension.
#createdNumberCreation timestamp in milliseconds.
#modifiedNumberLast modification timestamp in milliseconds.
Let[meta, FileMeta["ksl/src/main.rs"]];

Print[meta];
(* => Object[MetaData]{readonly, created, size, extension, kind, modified, name} *)

Get[meta, #kind];  (* => #file *)
Get[meta, #size];  (* => 15958 *)

Do[
  Print[Concat[ToString[key], ": ", ToString[Get[meta, key]]]],
  key,
  Keys[meta]
];
(* =>
  #readonly: #f
  #created: 1765785032517
  #size: 15958
  #extension: rs
  #kind: #file
  #modified: 1775653936858
  #name: main.rs
*)

Filter

Filter[list, predicate]

Returns a new list containing only those elements of list for which predicate returns #t. The original order is preserved.

  • Returns a List. If no elements match or list is empty, returns { }.
Let[numbers, { 1, 2, 3, 4, 5, 6 }];

Filter[numbers, Fun[{ x }, Eq[Mod[x, 2], 0]]];  (* => {2, 4, 6} *)
Filter[numbers, Fun[{ x }, Greater[x, 3]]];     (* => {4, 5, 6} *)
Filter[{ }, Fun[{ x }, Eq[x, 1]]];              (* => {} *)

Let[mixed_list, { 1, "two", 3 }];
Filter[mixed_list, Fun[{ x }, Greater[x, 0]]];
(*? Error[ksl::builtin::Greater]: Unexpected value: `"two"`. *)

Find

Find[list, element]

Searches list for the first occurrence of element.

  • Returns a Number representing the 1-based index of the element if found.
  • Returns 0 if the element is not present or the list is empty.
Let[items, {10, #twenty, {30}, #twenty}];

Find[items, #twenty];  (* => 2 *)
Find[items, { 30 }];   (* => 3 *)
Find[items, 40];       (* => 0 *)
Find[{ }, 1];          (* => 0 *)

Floor

Floor[n]

Computes the floor of n: the largest integer less than or equal to n.

  • Returns a Number.
Floor[5.8];   (* => 5 *)
Floor[-5.3];  (* => -6 *)
Floor[4];     (* => 4 *)
Floor[0];     (* => 0 *)

FromJSON

FromJSON[json]

Parses a JSON-formatted string json and converts it into an Object.

  • Returns an Object with type tag JSON. JSON string keys become KSL Atoms for property access.
  • An error is raised if the input is not valid JSON.
(* Construct a JSON string: {"a":1,"b":2} *)
(* #34 is the double quote character (") *)
Let[json, Concat["{", #34, "a", #34, ":1,", #34, "b", #34, ":2}"]];
Print[json];  (* => {"a":1,"b":2} *)

Let[jsonObj, FromJSON[json]];
Print[jsonObj];        (* => Object[JSON]{a, b} *)

Get[jsonObj, #a];      (* => 1 *)
Get[jsonObj, #b];      (* => 2 *)

Fstr

Fstr[pattern, arg_obj]

Performs string interpolation using a template pattern and an arguments object arg_obj.

  • Placeholders in the pattern have the form @key and are replaced with the value of the corresponding atom #key in arg_obj.

  • Unmatched @ sequences remain unchanged.

  • arg_obj must be an Object with type tag Args.

  • Returns the interpolated String.

Let[pattern, "My name is @name, age is @age, email is fake@e.io."];

Fstr[pattern, Object[Args, { { #name, "Kevin" }, { #age, 24 } }]];
(* => "My name is Kevin, age is 24, email is fake@e.io." *)

Fun

Fun[parameters, body]

Defines a first-class anonymous function (lambda) that captures its lexical environment.

  • parameters is a list of Symbols (use { } for no arguments).

  • body is a single expression evaluated when the lambda is invoked.

  • Returns a Lambda object, which can be stored, passed as an argument, or returned from other functions.

Let[add, Fun[{ x, y }, Add[x, y]]];
add[5, 10];  (* => 15 *)

Fun[{ x }, Fun[{ y }, Add[x, y]]][10][20];  (* => 30 *)

(* Scope isolation: Let creates a local shadow *)
Let[a, 0];
Let[tryAdd, Fun[{ }, Let[a, Add[a, 1]]]];
tryAdd[];
Print[Eq[a, 0]];  (* => #t *)

(* Use Update to modify the captured variable *)
Fun[{ }, Update[a, Add[a, 1]]][];
Print[Eq[a, 1]];  (* => #t *)