FileMeta
FileMeta[path]
Retrieves detailed metadata for the file or directory at path.
- Returns an
Objectwith type tagMetaData. - An error is raised if the path does not exist or permissions are insufficient.
The returned object contains the following keys:
| Key | Value Type | Description |
|---|---|---|
#name | String | The name of the file. |
#kind | Atom | #directory, #symlink, or #unknown. |
#size | Number | The size in bytes. |
#readonly | Atom | #t if read-only, otherwise #f. |
#extension | String | The file extension. |
#created | Number | Creation timestamp in milliseconds. |
#modified | Number | Last 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 orlistis 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
Numberrepresenting the 1-based index of the element if found. - Returns
0if 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
Objectwith type tagJSON. JSON string keys become KSLAtoms 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
@keyand are replaced with the value of the corresponding atom#keyinarg_obj. -
Unmatched
@sequences remain unchanged. -
arg_objmust be anObjectwith type tagArgs. -
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.
-
parametersis a list ofSymbols (use{ }for no arguments). -
bodyis a single expression evaluated when the lambda is invoked. -
Returns a
Lambdaobject, 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 *)