-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathList.pq
More file actions
67 lines (64 loc) · 2.82 KB
/
List.pq
File metadata and controls
67 lines (64 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
section List;
/////////////////////////
// List //
/////////////////////////
shared List.Flatten = Document(
"List.Flatten",
"Recursively flattens list elements. The end result is a single list",
{ [ Description = "Flattening nested lists into one", Code = "List.Flatten({ 1, 2, Table.FromRecords({[x=1]}), {3, 4, 5} })", Result = "{ 1, 2, Table.FromRecords({[x=1]}), 3, 4, 5}"] },
(list as list) => List.Accumulate(list, {}, (state, current) =>
let
currentListContent = if current is list then @List.Flatten(current) else {current}
in
List.Combine({state, currentListContent})
)
);
shared List.From = Document(
"List.From",
"Converts a text representation of a list into a list of the elements. Items are considered to be split by ,",
{ [ Description = "Convert a text list", Code = "List.From(""{A, B, C}"")", Result = "{ ""A"", ""B"", ""C"" }"] },
(simpleTextList as text) =>
let
trimWhitespace = Text.Trim(simpleTextList),
listToSplit = Text.TrimEnd(Text.TrimStart(trimWhitespace, "{"), "}"),
Result = List.Transform(Text.Split(listToSplit, ","), each Text.Trim(_))
in
Result
);
shared List.ToText = Document(
"List.ToText",
"Converts a list to a textual representation. Inverse of List.From",
{ [ Description = "Conver to text", Code = "List.ToText({ 1, 2, 3})", Result = """{1, 2, 3}"""] },
(list as list) =>
List.Accumulate(list, "{", (state, current) => current & Text.From(current)) & "}"
);
shared List.Swap = (_,_from as number ,_to as number) =>
let
from = List.Min({_from,_to}),
to = List.Max({_from,_to})
in if from=to then _ else
List.Range(_,0,from)
&{_{to}}
&List.Range(_,from+1,to-from-1)
&{_{from}}
&List.Range(_,to+1);
shared List.Shuffle = (n) =>
List.Accumulate({0..(n-2)},{0..n},(_,iterator)=>
List.Swap(List.Buffer(_),
Number.Round(Number.RandomBetween(iterator,n)),
iterator
));
/////////////////////////
// Dependencies //
/////////////////////////
Document = (name as text, description as text, valueOrExample as any, optional valueIfExample as any) =>
let
value = if valueIfExample is null then valueOrExample else valueIfExample,
examples = if valueIfExample is null then {} else valueOrExample
in
Value.ReplaceType(value, Value.Type(value) meta [
Documentation.Name = name,
Documentation.Description = description,
// [Description = "", Code="", Result =""]
Documentation.Examples = examples
]);