September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -12,9 +12,9 @@ show_list(list l)
|
|||
while (i < l_length(l)) {
|
||||
o_text(s);
|
||||
if (l_s_integer(l, i)) {
|
||||
o_integer(l_q_integer(l, i));
|
||||
o_integer(l[i]);
|
||||
} else {
|
||||
show_list(l_q_list(l, i));
|
||||
show_list(l[i]);
|
||||
}
|
||||
s = ", ";
|
||||
i += 1;
|
||||
|
|
@ -24,18 +24,12 @@ show_list(list l)
|
|||
}
|
||||
|
||||
void
|
||||
flat(list c, list l)
|
||||
flat(list c, object o)
|
||||
{
|
||||
integer i;
|
||||
|
||||
i = 0;
|
||||
while (i < l_length(l)) {
|
||||
if (l_s_integer(l, i)) {
|
||||
lb_p_integer(c, l_q_integer(l, i));
|
||||
} else {
|
||||
flat(c, l_q_list(l, i));
|
||||
}
|
||||
i += 1;
|
||||
if (__id(o) == INTEGER_ID) {
|
||||
l_append(c, o);
|
||||
} else {
|
||||
l_ucall(o, flat, 1, c);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -44,7 +38,7 @@ flatten(list l)
|
|||
{
|
||||
list c;
|
||||
|
||||
flat(c, l);
|
||||
l_ucall(l, flat, 1, c);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
|
@ -52,16 +46,7 @@ flatten(list l)
|
|||
list
|
||||
nl(...)
|
||||
{
|
||||
integer i;
|
||||
list l;
|
||||
|
||||
i = 0;
|
||||
while (i < count()) {
|
||||
l_append(l, $i);
|
||||
i += 1;
|
||||
}
|
||||
|
||||
return l;
|
||||
return xcall(l_assemble);
|
||||
}
|
||||
|
||||
integer
|
||||
|
|
@ -69,8 +54,7 @@ main(void)
|
|||
{
|
||||
list l;
|
||||
|
||||
l_set(l, nl(nl(1), 2, nl(nl(3, 4), 5), nl(nl(nl())), nl(nl(nl(6))), 7, 8,
|
||||
nl()));
|
||||
l = nl(nl(1), 2, nl(nl(3, 4), 5), nl(nl(nl())), nl(nl(nl(6))), 7, 8, nl());
|
||||
|
||||
show_list(l);
|
||||
o_byte('\n');
|
||||
|
|
|
|||
|
|
@ -1,59 +1,34 @@
|
|||
-- quickSort :: (Ord a) => [a] -> [a]
|
||||
on quickSort(xs)
|
||||
set headTail to uncons(xs)
|
||||
if headTail is not missing value then
|
||||
set {h, t} to headTail
|
||||
|
||||
-- lessOrEqual :: a -> Bool
|
||||
script lessOrEqual
|
||||
on lambda(x)
|
||||
x ≤ h
|
||||
end lambda
|
||||
end script
|
||||
|
||||
set {less, more} to partition(lessOrEqual, t)
|
||||
|
||||
quickSort(less) & h & quickSort(more)
|
||||
-- flatten :: Tree a -> [a]
|
||||
on flatten(t)
|
||||
if class of t is list then
|
||||
concatMap(flatten, t)
|
||||
else
|
||||
xs
|
||||
t
|
||||
end if
|
||||
end quickSort
|
||||
end flatten
|
||||
|
||||
|
||||
-- TEST
|
||||
-- TEST -----------------------------------------------------------------------
|
||||
on run
|
||||
|
||||
quickSort([11.8, 14.1, 21.3, 8.5, 16.7, 5.7])
|
||||
|
||||
--> {5.7, 8.5, 11.8, 14.1, 16.7, 21.3}
|
||||
flatten([[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []])
|
||||
|
||||
--> {1, 2, 3, 4, 5, 6, 7, 8}
|
||||
end run
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS ----------------------------------------------------------
|
||||
|
||||
-- GENERIC FUNCTIONS
|
||||
|
||||
-- partition :: predicate -> List -> (Matches, nonMatches)
|
||||
-- partition :: (a -> Bool) -> [a] -> ([a], [a])
|
||||
on partition(f, xs)
|
||||
-- concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
on concatMap(f, xs)
|
||||
set lst to {}
|
||||
set lng to length of xs
|
||||
tell mReturn(f)
|
||||
set lst to {{}, {}}
|
||||
repeat with x in xs
|
||||
set v to contents of x
|
||||
set end of item ((lambda(v) as integer) + 1) of lst to v
|
||||
repeat with i from 1 to lng
|
||||
set lst to (lst & |λ|(item i of xs, i, xs))
|
||||
end repeat
|
||||
return {item 2 of lst, item 1 of lst}
|
||||
end tell
|
||||
end partition
|
||||
|
||||
-- uncons :: [a] -> Maybe (a, [a])
|
||||
on uncons(xs)
|
||||
if length of xs > 0 then
|
||||
{item 1 of xs, rest of xs}
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end uncons
|
||||
return lst
|
||||
end concatMap
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
|
|
@ -62,7 +37,7 @@ on mReturn(f)
|
|||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
|
|
|||
11
Task/Flatten-a-list/BaCon/flatten-a-list.bacon
Normal file
11
Task/Flatten-a-list/BaCon/flatten-a-list.bacon
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
OPTION COLLAPSE TRUE
|
||||
|
||||
lst$ = "\"1\",2,\"\\\"3,4\\\",5\",\"\\\"\\\\\"\\\\\"\\\"\",\"\\\"\\\\\"6\\\\\"\\\"\",7,8,\"\""
|
||||
|
||||
PRINT lst$
|
||||
|
||||
REPEAT
|
||||
lst$ = FLATTEN$(lst$)
|
||||
UNTIL AMOUNT(lst$, ",") = AMOUNT(FLATTEN$(lst$), ",")
|
||||
|
||||
PRINT SORT$(lst$, ",")
|
||||
16
Task/Flatten-a-list/Gambas/flatten-a-list.gambas
Normal file
16
Task/Flatten-a-list/Gambas/flatten-a-list.gambas
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
'Code 'borrowed' from Run BASIC
|
||||
|
||||
Public Sub Main()
|
||||
Dim sComma, sString, sFlatter As String
|
||||
Dim siCount As Short
|
||||
|
||||
sString = "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]"
|
||||
For siCount = 1 To Len(sString)
|
||||
If InStr("[] ,", Mid$(sString, siCount, 1)) = 0 Then
|
||||
sFlatter = sFlatter & sComma & Mid(sString, siCount, 1)
|
||||
sComma = ","
|
||||
End If
|
||||
Next
|
||||
Print "["; sFlatter; "]"
|
||||
|
||||
End
|
||||
|
|
@ -1,24 +1,26 @@
|
|||
import Data.Tree
|
||||
import Data.Tree (Tree(..), flatten)
|
||||
|
||||
-- [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
|
||||
-- implemented as multiway tree:
|
||||
|
||||
-- Data.Tree represents trees where nodes have values too, unlike the trees in our problem.
|
||||
-- so we use a list as that value, where a node will have an empty list value,
|
||||
-- and a leaf will have a one-element list value and no subtrees
|
||||
list :: Tree [Int]
|
||||
list = Node [] [
|
||||
Node [] [Node [1] []],
|
||||
Node [2] [],
|
||||
Node [] [
|
||||
Node [] [ Node [3] [],Node [4] []],
|
||||
Node [5] []
|
||||
],
|
||||
Node [] [Node [] [Node [] []]],
|
||||
Node [] [Node [] [Node [6] []]],
|
||||
Node [7] [],
|
||||
Node [8] [],
|
||||
Node [] []
|
||||
]
|
||||
list =
|
||||
Node
|
||||
[]
|
||||
[ Node [] [Node [1] []]
|
||||
, Node [2] []
|
||||
, Node [] [Node [] [Node [3] [], Node [4] []], Node [5] []]
|
||||
, Node [] [Node [] [Node [] []]]
|
||||
, Node [] [Node [] [Node [6] []]]
|
||||
, Node [7] []
|
||||
, Node [8] []
|
||||
, Node [] []
|
||||
]
|
||||
|
||||
flattenList = concat.flatten
|
||||
flattenList :: Tree [a] -> [a]
|
||||
flattenList = concat . flatten
|
||||
|
||||
main :: IO ()
|
||||
main = print $ flattenList list
|
||||
|
|
|
|||
|
|
@ -1,16 +1,22 @@
|
|||
data Tree a = Leaf a | Node [Tree a]
|
||||
data Tree a
|
||||
= Leaf a
|
||||
| Node [Tree a]
|
||||
|
||||
flatten :: Tree a -> [a]
|
||||
flatten (Leaf x) = [x]
|
||||
flatten (Node xs) = concatMap flatten xs
|
||||
|
||||
main = print $ flatten $ Node [Node [Leaf 1],
|
||||
Leaf 2,
|
||||
Node [Node [Leaf 3, Leaf 4], Leaf 5],
|
||||
Node [Node [Node []]],
|
||||
Node [Node [Node [Leaf 6]]],
|
||||
Leaf 7,
|
||||
Leaf 8,
|
||||
Node []]
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
(print . flatten) $
|
||||
Node
|
||||
[ Node [Leaf 1]
|
||||
, Leaf 2
|
||||
, Node [Node [Leaf 3, Leaf 4], Leaf 5]
|
||||
, Node [Node [Node []]]
|
||||
, Node [Node [Node [Leaf 6]]]
|
||||
, Leaf 7
|
||||
, Leaf 8
|
||||
, Node []
|
||||
]
|
||||
-- output: [1,2,3,4,5,6,7,8]
|
||||
|
|
|
|||
|
|
@ -1,17 +1,29 @@
|
|||
data NestedList a = NList [NestedList a] | Entry a
|
||||
data NestedList a
|
||||
= NList [NestedList a]
|
||||
| Entry a
|
||||
|
||||
flatten :: NestedList a -> [a]
|
||||
flatten nl = flatten' nl []
|
||||
flatten nl = flatten_ nl []
|
||||
where
|
||||
-- By passing through a list which the results will be preprended to we allow efficient lazy evaluation
|
||||
flatten' :: NestedList a -> [a] -> [a]
|
||||
flatten' (Entry a) cont = a:cont
|
||||
flatten' (NList entries) cont = foldr flatten' cont entries
|
||||
flatten_ :: NestedList a -> [a] -> [a]
|
||||
flatten_ (Entry a) cont = a : cont
|
||||
flatten_ (NList entries) cont = foldr flatten_ cont entries
|
||||
|
||||
-- By passing through a list to which the results will be prepended,
|
||||
-- we allow for efficient lazy evaluation
|
||||
example :: NestedList Int
|
||||
example = NList [ NList [Entry 1], Entry 2, NList [NList [Entry 3, Entry 4], Entry 5], NList [NList [NList []]], NList [ NList [ NList [Entry 6]]], Entry 7, Entry 8, NList []]
|
||||
example =
|
||||
NList
|
||||
[ NList [Entry 1]
|
||||
, Entry 2
|
||||
, NList [NList [Entry 3, Entry 4], Entry 5]
|
||||
, NList [NList [NList []]]
|
||||
, NList [NList [NList [Entry 6]]]
|
||||
, Entry 7
|
||||
, Entry 8
|
||||
, NList []
|
||||
]
|
||||
|
||||
main :: IO ()
|
||||
main = print $ flatten example
|
||||
|
||||
-- output [1,2,3,4,5,6,7,8]
|
||||
|
|
|
|||
9
Task/Flatten-a-list/Hy/flatten-a-list.hy
Normal file
9
Task/Flatten-a-list/Hy/flatten-a-list.hy
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(defn flatten [lst]
|
||||
(sum (genexpr (if (isinstance x list)
|
||||
(flatten x)
|
||||
[x])
|
||||
[x lst])
|
||||
[]))
|
||||
|
||||
(print (flatten [[1] 2 [[3 4] 5] [[[]]] [[[6]]] 7 8 []]))
|
||||
; [1, 2, 3, 4, 5, 6, 7, 8]
|
||||
|
|
@ -1,10 +1 @@
|
|||
julia> t = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
|
||||
8-element Int32 Array:
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
flat(A) = mapreduce(x->isa(x,Array)? flat(x): x, vcat, [], A)
|
||||
|
|
|
|||
|
|
@ -1 +1,8 @@
|
|||
flat(A) = [[isa(x,Array)? flat(x): x for x in A]...]
|
||||
function flat(A)
|
||||
result = Any[]
|
||||
grep(a) = for x in a
|
||||
isa(x,Array) ? grep(x) : push!(result,x)
|
||||
end
|
||||
grep(A)
|
||||
result
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
flat(A) = mapreduce(x->isa(x,Array)? flat(x): x, vcat, [], A)
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
function flat(A)
|
||||
result = Any[]
|
||||
grep(a) = for x in a
|
||||
isa(x,Array) ? grep(x) : push!(result,x)
|
||||
end
|
||||
grep(A)
|
||||
result
|
||||
end
|
||||
29
Task/Flatten-a-list/Kotlin/flatten-a-list.kotlin
Normal file
29
Task/Flatten-a-list/Kotlin/flatten-a-list.kotlin
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// version 1.0.6
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
|
||||
fun flattenList(nestList: List<Any>, flatList: MutableList<Int>) {
|
||||
for (e in nestList)
|
||||
if (e is Int)
|
||||
flatList.add(e)
|
||||
else
|
||||
// using unchecked cast here as can't check for instance of 'erased' generic type
|
||||
flattenList(e as List<Any>, flatList)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val nestList : List<Any> = listOf(
|
||||
listOf(1),
|
||||
2,
|
||||
listOf(listOf(3, 4), 5),
|
||||
listOf(listOf(listOf<Int>())),
|
||||
listOf(listOf(listOf(6))),
|
||||
7,
|
||||
8,
|
||||
listOf<Int>()
|
||||
)
|
||||
println("Nested : " + nestList)
|
||||
val flatList = mutableListOf<Int>()
|
||||
flattenList(nestList, flatList)
|
||||
println("Flattened : " + flatList)
|
||||
}
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface NSArray (FlattenExt)
|
||||
-(NSArray *)flatten;
|
||||
@end
|
||||
|
||||
@implementation NSArray (FlattenExt)
|
||||
-(NSArray *)flatten
|
||||
{
|
||||
NSMutableArray *r = [NSMutableArray array];
|
||||
NSEnumerator *en = [self objectEnumerator];
|
||||
id o;
|
||||
while ( (o = [en nextObject]) ) {
|
||||
if ( [o isKindOfClass: [NSArray class]] )
|
||||
[r addObjectsFromArray: [o flatten]];
|
||||
else
|
||||
[r addObject: o];
|
||||
}
|
||||
return [NSArray arrayWithArray: r];
|
||||
}
|
||||
@end
|
||||
|
||||
int main()
|
||||
{
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
|
||||
NSArray *p = [NSArray
|
||||
arrayWithObjects:
|
||||
[NSArray arrayWithObjects: [NSNumber numberWithInt: 1], nil],
|
||||
[NSNumber numberWithInt: 2],
|
||||
[NSArray arrayWithObjects:
|
||||
[NSArray arrayWithObjects: [NSNumber numberWithInt: 3],
|
||||
[NSNumber numberWithInt: 4], nil],
|
||||
[NSNumber numberWithInt: 5], nil],
|
||||
[NSArray arrayWithObjects:
|
||||
[NSArray arrayWithObjects:
|
||||
[NSArray arrayWithObjects: nil], nil], nil],
|
||||
[NSArray arrayWithObjects:
|
||||
[NSArray arrayWithObjects:
|
||||
[NSArray arrayWithObjects:
|
||||
[NSNumber numberWithInt: 6], nil], nil], nil],
|
||||
[NSNumber numberWithInt: 7],
|
||||
[NSNumber numberWithInt: 8],
|
||||
[NSArray arrayWithObjects: nil], nil];
|
||||
NSArray *f = [p flatten];
|
||||
NSEnumerator *e = [f objectEnumerator];
|
||||
id o;
|
||||
while( (o = [e nextObject]) )
|
||||
{
|
||||
NSLog(@"%@", o);
|
||||
}
|
||||
|
||||
|
||||
[pool drain];
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface NSArray (FlattenExt)
|
||||
@property (nonatomic, readonly) NSArray *flattened;
|
||||
@end
|
||||
|
||||
@implementation NSArray (FlattenExt)
|
||||
-(NSArray *) flattened {
|
||||
NSMutableArray *flattened = [[NSMutableArray alloc] initWithCapacity:self.count];
|
||||
|
||||
for (id object in self) {
|
||||
if ([object isKindOfClass:[NSArray class]])
|
||||
[flattened addObjectsFromArray:((NSArray *)object).flattened];
|
||||
else
|
||||
[flattened addObject:object];
|
||||
}
|
||||
|
||||
return [flattened autorelease];
|
||||
}
|
||||
@end
|
||||
|
||||
int main() {
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
NSArray *unflattened = [NSArray arrayWithObjects:[NSArray arrayWithObject:[NSNumber numberWithInteger:1]],
|
||||
[NSNumber numberWithInteger:2],
|
||||
[NSArray arrayWithObjects:[NSArray arrayWithObjects:[NSNumber numberWithInteger:3], [NSNumber numberWithInteger:4], nil],
|
||||
[NSNumber numberWithInteger:5], nil],
|
||||
[NSArray arrayWithObject:[NSArray arrayWithObject:[NSArray array]]],
|
||||
[NSArray arrayWithObject:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInteger:6]]]],
|
||||
[NSNumber numberWithInteger:7],
|
||||
[NSNumber numberWithInteger:8],
|
||||
[NSArray array], nil];
|
||||
|
||||
for (id object in unflattened.flattened)
|
||||
NSLog(@"%@", object);
|
||||
|
||||
[pool drain];
|
||||
|
||||
return 0;
|
||||
}
|
||||
36
Task/Flatten-a-list/OoRexx/flatten-a-list.rexx
Normal file
36
Task/Flatten-a-list/OoRexx/flatten-a-list.rexx
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
sub1 = .array~of(1)
|
||||
sub2 = .array~of(3, 4)
|
||||
sub3 = .array~of(sub2, 5)
|
||||
sub4 = .array~of(.array~of(.array~new))
|
||||
sub5 = .array~of(.array~of(.array~of(6)))
|
||||
sub6 = .array~new
|
||||
|
||||
-- final list construction
|
||||
list = .array~of(sub1, 2, sub3, sub4, sub5, 7, 8, sub6)
|
||||
|
||||
-- flatten
|
||||
flatlist = flattenList(list)
|
||||
|
||||
say "["flatlist~toString("line", ", ")"]"
|
||||
|
||||
::routine flattenList
|
||||
use arg list
|
||||
-- we could use a list or queue, but let's just use an array
|
||||
accumulator = .array~new
|
||||
|
||||
-- now go to the recursive processing version
|
||||
call flattenSublist list, accumulator
|
||||
|
||||
return accumulator
|
||||
|
||||
::routine flattenSublist
|
||||
use arg list, accumulator
|
||||
|
||||
-- ask for the items explicitly, since this will allow
|
||||
-- us to flatten indexed collections as well
|
||||
do item over list~allItems
|
||||
-- if the object is some sort of collection, flatten this out rather
|
||||
-- than add to the accumulator
|
||||
if item~isA(.collection) then call flattenSublist item, accumulator
|
||||
else accumulator~append(item)
|
||||
end
|
||||
1
Task/Flatten-a-list/Perl-6/flatten-a-list-2.pl6
Normal file
1
Task/Flatten-a-list/Perl-6/flatten-a-list-2.pl6
Normal file
|
|
@ -0,0 +1 @@
|
|||
say { |(@$_ > 1 ?? map(&?BLOCK, @$_) !! $_) }(@l)
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
#lang racket
|
||||
(define (flatten l)
|
||||
(cond [(empty? l) null]
|
||||
[(not (list? l)) (list l)]
|
||||
[else (append (flatten (first l)) (flatten (rest l)))]))
|
||||
(flatten '(1 (2 (3 4 5) (6 7)) 8 9))
|
||||
92
Task/Flatten-a-list/Rust/flatten-a-list.rust
Normal file
92
Task/Flatten-a-list/Rust/flatten-a-list.rust
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
use std::{vec, mem, iter};
|
||||
|
||||
enum List<T> {
|
||||
Node(Vec<List<T>>),
|
||||
Leaf(T),
|
||||
}
|
||||
|
||||
impl<T> IntoIterator for List<T> {
|
||||
type Item = List<T>;
|
||||
type IntoIter = ListIter<T>;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
match self {
|
||||
List::Node(vec) => ListIter::NodeIter(vec.into_iter()),
|
||||
leaf @ List::Leaf(_) => ListIter::LeafIter(iter::once(leaf)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum ListIter<T> {
|
||||
NodeIter(vec::IntoIter<List<T>>),
|
||||
LeafIter(iter::Once<List<T>>),
|
||||
}
|
||||
|
||||
impl<T> ListIter<T> {
|
||||
fn flatten(self) -> Flatten<T> {
|
||||
Flatten {
|
||||
stack: Vec::new(),
|
||||
curr: self,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Iterator for ListIter<T> {
|
||||
type Item = List<T>;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
match *self {
|
||||
ListIter::NodeIter(ref mut v_iter) => v_iter.next(),
|
||||
ListIter::LeafIter(ref mut o_iter) => o_iter.next(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Flatten<T> {
|
||||
stack: Vec<ListIter<T>>,
|
||||
curr: ListIter<T>,
|
||||
}
|
||||
|
||||
// Flatten code is a little messy since we are shoehorning recursion into an Iterator
|
||||
impl<T> Iterator for Flatten<T> {
|
||||
type Item = T;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
loop {
|
||||
match self.curr.next() {
|
||||
Some(list) => {
|
||||
match list {
|
||||
node @ List::Node(_) => {
|
||||
self.stack.push(node.into_iter());
|
||||
let len = self.stack.len();
|
||||
mem::swap(&mut self.stack[len - 1], &mut self.curr);
|
||||
}
|
||||
List::Leaf(item) => return Some(item),
|
||||
}
|
||||
}
|
||||
None => {
|
||||
if let Some(next) = self.stack.pop() {
|
||||
self.curr = next;
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use List::*;
|
||||
fn main() {
|
||||
let list = Node(vec![Node(vec![Leaf(1)]),
|
||||
Leaf(2),
|
||||
Node(vec![Node(vec![Leaf(3), Leaf(4)]), Leaf(5)]),
|
||||
Node(vec![Node(vec![Node(vec![])])]),
|
||||
Node(vec![Node(vec![Node(vec![Leaf(6)])])]),
|
||||
Leaf(7),
|
||||
Leaf(8),
|
||||
Node(vec![])]);
|
||||
|
||||
for elem in list.into_iter().flatten() {
|
||||
print!("{} ", elem);
|
||||
}
|
||||
println!();
|
||||
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
' Flatten the example array...
|
||||
a = FlattenArray(Array(Array(1), 2, Array(Array(3,4), 5), Array(Array(Array())), Array(Array(Array(6))), 7, 8, Array()))
|
||||
|
||||
' Print the list, comma-separated...
|
||||
WScript.Echo Join(a, ",")
|
||||
|
||||
Function FlattenArray(a)
|
||||
If IsArray(a) Then DoFlatten a, FlattenArray: FlattenArray = Split(Trim(FlattenArray))
|
||||
End Function
|
||||
|
||||
Sub DoFlatten(a, s)
|
||||
For i = 0 To UBound(a)
|
||||
If IsArray(a(i)) Then DoFlatten a(i), s Else s = s & a(i) & " "
|
||||
Next
|
||||
End Sub
|
||||
5
Task/Flatten-a-list/Zkl/flatten-a-list.zkl
Normal file
5
Task/Flatten-a-list/Zkl/flatten-a-list.zkl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
fcn flatten(list){ list.pump(List,
|
||||
fcn(i){ if(List.isType(i)) return(Void.Recurse,i,self.fcn); i}) }
|
||||
|
||||
flatten(L(L(1), L(2), L(L(3,4), 5), L(L(L())), L(L(L(6))), 7, 8, L()))
|
||||
//-->L(1,2,3,4,5,6,7,8)
|
||||
Loading…
Add table
Add a link
Reference in a new issue