Elixir Binary - what is the equivalent to C#'s Take and Skip?

Hi All,

When C# ,it can use Take and Skip to take byte list. How can i use this is Elixir?
thanks.

byte[] list = ...something value..;
var test = list.Skip(list.Length - 2);
var test2 = list.Take(2);

@sen Depends on use case (charlist vs String) you can use Enum or String functions. I believe that slice/2 and/or slice/3 would be a good start for you.

Enum.slice('abcdefg', 2..-2)
'cdef'

String.slice("abcdefg", 2..-2)  
"cdef"

For sure pattern matching (if possible) is always best. Make sure you check documentation especially for Enum and String modules.

1 Like

Or also you could take a look at the :binary module at http://erlang.org/doc/man/binary.html Especially :binary.part/2

4 Likes

For sure in erlang docs there are lots of really useful functions, but if we are going to teach newbies then it should be more step-by-step. Elixir documentation is much simpler to read and Erlang documentation could cause trouble for some people due to differences between Elixir and Erlang.

I only mentioned charlist if there is really a special need. For “everyday” private and small application Elixir documentation (with few libraries like HTTPoison) are enough for start. I believe that newbies are looking for much simpler solutions. For start with Elixir I believe that people should focus on things like naming and good practices for start a real work with code.

Later mid-developers would be more interested in further optimizations and specific use cases. Look that you would not see (without benchmarks) differences in simplest solutions. It start matter when you think about scaling your application like in this thread:

Personally in my first Elixir paid work I wrote lots of scrapers for which I did not needed to know about any Erlang function. Of course some of them were really useful like :erlang.term_to_binary/1 and :erlang.binary_to_term/1, but they were not important at all as using simplest JSON library solves all needs for saving simplest List and Map results into file.

4 Likes

Hi all,
Thanks for answer, For me it’s very helpful.
Currently i try to use Kernel.binary_part(list,0,2) to get it.