Add head, tail, initial snippets

This commit is contained in:
Angelos Chalaris
2019-08-20 14:08:52 +03:00
parent 213824718a
commit 5e65e3bc18
3 changed files with 52 additions and 0 deletions

17
snippets/head.md Normal file
View File

@ -0,0 +1,17 @@
---
title: head
tags: list,beginner
---
Returns the head of a list.
use `lst[0]` to return the first element of the passed list.
```py
def head(lst):
return lst[0]
```
```py
head([1, 2, 3]); # 1
```

17
snippets/initial.md Normal file
View File

@ -0,0 +1,17 @@
---
title: initial
tags: list,beginner
---
Returns all the elements of a list except the last one.
Use `lst[0:-1]` to return all but the last element of the list.
```py
def initial(lst):
return lst[0:-1]
```
```py
initial([1, 2, 3]); # [1,2]
```

18
snippets/tail.md Normal file
View File

@ -0,0 +1,18 @@
---
title: tail
tags: list,beginner
---
Returns all elements in a list except for the first one.
Return `lst[1:]` if the list's length is more than `1`, otherwise, return the whole list.
```py
def tail(lst):
return lst[1:] if len(lst) > 1 else lst
```
```py
tail([1, 2, 3]); # [2,3]
tail([1]); # [1]
```