June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,52 +1,34 @@
void
show_list(list l)
{
integer i;
text s;
integer i, k;
o_text("[");
s = "";
i = 0;
while (i < l_length(l)) {
o_text(s);
if (l_s_integer(l, i)) {
o_integer(l[i]);
while (i < ~l) {
o_text(i ? ", " : "");
if (l_j_integer(k, l, i)) {
o_integer(k);
} else {
show_list(l[i]);
}
s = ", ";
i += 1;
}
o_text("]");
}
void
flat(list c, object o)
list
flatten(list c, object o)
{
if (__id(o) == INTEGER_ID) {
l_append(c, o);
c.append(o);
} else {
l_ucall(o, flat, 1, c);
l_ucall(o, flatten, 1, c);
}
}
list
flatten(list l)
{
list c;
l_ucall(l, flat, 1, c);
return c;
}
list
nl(...)
{
return xcall(l_assemble);
c;
}
integer
@ -54,12 +36,13 @@ main(void)
{
list l;
l = nl(nl(1), 2, nl(nl(3, 4), 5), nl(nl(nl())), nl(nl(nl(6))), 7, 8, nl());
l = list(list(1), 2, list(list(3, 4), 5),
list(list(list())), list(list(list(6))), 7, 8, list());
show_list(l);
o_byte('\n');
show_list(flatten(l));
show_list(flatten(list(), l));
o_byte('\n');
return 0;

View file

@ -1,3 +1,2 @@
let flatten = list => list.reduce(
(a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []
);
// flatten :: Tree a -> [a]
const flatten = t => Array.isArray(t) ? [].concat(...t.map(flatten)) : t;

View file

@ -1 +1 @@
flat(A) = mapreduce(x->isa(x,Array)? flat(x): x, vcat, [], A)
flat(arr::Array) = mapreduce(x -> isa(x, Array) ? flat(x) : x, append!, [], arr)

View file

@ -1,8 +1,11 @@
function flat(A)
result = Any[]
grep(a) = for x in a
isa(x,Array) ? grep(x) : push!(result,x)
end
grep(A)
result
end
function flat2(arr)
rst = Any[]
grep(v) = for x in v
if isa(x, Array) grep(x) else push!(rst, x) end
end
grep(arr)
rst
end
arr = [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]
@show flat(arr)

View file

@ -0,0 +1,7 @@
my @l = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []];
say .perl given gather @l.deepmap(*.take); # lazy recursive version
# Another way to do it is with a recursive function (here actually a Block calling itself with the &?BLOCK dynamic variable):
say { |(@$_ > 1 ?? map(&?BLOCK, @$_) !! $_) }(@l)

View file

@ -4,3 +4,11 @@ flatten: function [
][
load form block
]
red>> flatten [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []]
== [1 2 3 4 5 6 7 8]
;flatten a list to a string
>> blk: [1 2 ["test"] "a" [["bb"]] 3 4 [[[99]]]]
>> form blk
== "1 2 test a bb 3 4 99"

View file

@ -1,10 +1,11 @@
aList = "[[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]"
bList = ""
cList = ""
for n=1 to len(aList)
if ascii(aList[n]) >= 48 and ascii(aList[n]) <= 57
bList = bList + ", " + aList[n] ok
aString = "[[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]"
bString = ""
cString = ""
for n=1 to len(aString)
if ascii(aString[n]) >= 48 and ascii(aString[n]) <= 57
bString = bString + ", " + aString[n]
ok
next
cList = substr(bList,3,Len(bList)-2)
dList = "[" + cList + "]"
see dList + nl
cString = substr(bString,3,Len(bString)-2)
cString = '"' + cString + '"'
see cString + nl