add spread

This commit is contained in:
Rohit Tanwar
2018-01-09 02:08:05 +05:30
parent 2ab9ef8cd0
commit edd116a981
4 changed files with 23 additions and 6 deletions

20
snippets/spread.md Normal file
View File

@ -0,0 +1,20 @@
### spread
Implements javascript's spread syntax as a function. Flattens the list(non-deep) and returns an array.
```python
def spread(arg):
ret = []
for i in arg:
if isinstance(i, list):
ret.extend(i)
else:
ret.append(i)
return ret
```
```python
spread([1,2,3,[4,5,6],[7],8,9]) # [1,2,3,4,5,6,7,8,9]
```