March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -0,0 +1,5 @@
function flatten(list) {
return list.reduce(function (acc, val) {
return acc.concat(val.constructor === Array ? flatten(val) : val);
}, []);
}

View file

@ -0,0 +1,5 @@
let flatten = list => {
return list.reduce(
(a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []
);
}

View file

@ -1,5 +0,0 @@
function flatten(arr){
return arr.reduce(function(acc, val){
return acc.concat(val.constructor === Array ? flatten(val) : val);
},[]);
}

View file

@ -0,0 +1,5 @@
L := [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]:
with(ListTools):
Flatten(L);

View file

@ -0,0 +1,7 @@
flatten := proc(x)
`if`(type(x,'list'),seq(procname(i),i = x),x);
end proc:
L := [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]:
[flatten(L)];

View file

@ -0,0 +1,40 @@
#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() {
@autoreleasepool {
NSArray *p = @[
@[ @1 ],
@2,
@[ @[@3, @4], @5],
@[ @[ @[ ] ] ],
@[ @[ @[ @6 ] ] ],
@7,
@8,
@[ ] ];
for (id object in unflattened.flattened)
NSLog(@"%@", object);
}
return 0;
}

View file

@ -1,6 +1,8 @@
list = translate (list, ' ', '[]' );
list = '[' || list || ']';
/* the above will erroneously return:
[ 1 , 2, 3,4 , 5 , , 6 , 7, 8, ]

View file

@ -1,6 +1,6 @@
/*REXX program demonstrates how to flatten a list. */
/*REXX pgm demonstrates how to flatten a list (it need not be numeric).*/
y = '[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]'
z = '['changestr(' ', space( translate(y, , '[,]')), ", ")']'
z = '['changestr(" ", space( translate(y, , '[,]')), ", ")']'
say ' input =' y
say 'output =' z
/*stick a fork in it, we're done.*/

View file

@ -1,11 +1,12 @@
/*REXX program demonstrates how to flatten a list. */
/*REXX pgm demonstrates how to flatten a list (it need not be numeric).*/
y = '[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]'
z = translate( y, ,'[,]' ) /*change brackets&commas──►blanks*/
z = space(z) /*remove extraneous blanks. */
z = changestr( ' ', z, ", " ) /*change blanks to "comma blank".*/
z = '[' || z || "]" /*add brackets via concatenation.*/
/*another version of above: */
z = '['z"]"
say ' input =' y
say 'output =' z
/*stick a fork in it, we're done.*/