Skip to content

Latest commit

 

History

History
34 lines (31 loc) · 927 Bytes

13.9.md

File metadata and controls

34 lines (31 loc) · 927 Bytes

Enum.take/2 を使って count の数だけデータを取り出す https://hexdocs.pm/elixir/master/Enum.html#take/2

Enum.take/2 を自分で書いてみた

> c "13.9.exs"
[EnumTake]
> a = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
> EnumTake.take(a, 5)
["1", "2", "3", "4", "5"]
> EnumTake.take(a, 15)
["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
> EnumTake.take(a, 0)
[]
> EnumTake.take(a, -1)
["10", "9", "8", "7", "6", "5", "4", "3", "2", "1"]
["10"]
> b = [one: 1, two: 2, three: 3, four: 4, five: 5, six: 6]
[one: 1, two: 2, three: 3, four: 4, five: 5, six: 6]
> EnumTake.take(b, 3)
[one: 1, two: 2, three: 3]
> EnumTake.take(b, 7)
[one: 1, two: 2, three: 3, four: 4, five: 5, six: 6]
> EnumTake.take(b, 1)
[one: 1]
> EnumTake.take(b, 0)
[]
> EnumTake.take(b, -2)
[six: 6, five: 5, four: 4, three: 3, two: 2, one: 1]
[six: 6, five: 5]