Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir utility function to return sublist from List based on index and size

Tags:

elixir

Is there any utility function in Elixir from which I want to get sublist from array based on index and size?

Enum utility doesn't provide this functionality

arr = [1,2,3,4,5,6,7]
from=2
size=3
res = sublist(arr,from,size)
#res should return [3,4,5]
like image 525
shivakumar Avatar asked Dec 18 '22 03:12

shivakumar


1 Answers

You can use Enum.slice/3 like this:

[1,2,3,4,5,6,7] |> Enum.slice(2, 3)
[3, 4, 5]

Or without the pipe operator like this:

Enum.slice([1,2,3,4,5,6,7], 2, 3)
[3, 4, 5]
like image 58
jacruzca Avatar answered Jun 09 '23 23:06

jacruzca