Sebb
String.split vs Enum.split_with
There seems to be no way to split an Enum like String.split does. Actually those functions have very different semantics.
String.split("123045067809", "0") #=> ["123", "45", "678", "9"]
function I’d like to have:
l = [1,2,3,0,4,5,0,6,7,8,0,9]
Enum.split(l, &(&1 == 0)) #=>[[1, 2, 3], [4, 5], [6, 7, 8], [9]]
this is there, but not what I want:
Enum.split_with(l, &(&1 == 0)) #=> {[0, 0, 0], [1, 2, 3, 4, 5, 6, 7, 8, 9]}
close:
Enum.chunk_by(l, &(&1 == 0)) #=> [[1, 2, 3], [0], [4, 5], [0], [6, 7, 8], [0], '\t']
I think this is the first time, I miss a funciton in stdlib, that I expected to be there.
String.split(string, pattern, options \\ [])Divides a string into parts based on a pattern. [split]
Enum.split_with(enumerable, fun)Splits the
enumerablein two lists according to the given functionfun. [split_with]
Marked As Solved
dimitarvp
Also Liked
adamu
Obligatory benchmarks.
Name ips average deviation median 99th %
recursion 3.98 M 251.49 ns ±11214.50% 188 ns 456 ns
foldr 2.99 M 334.99 ns ±12387.85% 223 ns 506 ns
reduce 2.70 M 370.10 ns ±9843.80% 258 ns 567 ns
chunk_while 1.40 M 712.55 ns ±4515.89% 532 ns 968 ns
chunk_by_reject 0.86 M 1164.60 ns ±2888.22% 896 ns 1427 ns
Comparison:
recursion 3.98 M
foldr 2.99 M - 1.33x slower +83.50 ns
reduce 2.70 M - 1.47x slower +118.61 ns
chunk_while 1.40 M - 2.83x slower +461.06 ns
chunk_by_reject 0.86 M - 4.63x slower +913.11 ns
Operating System: macOS
CPU Information: Intel(R) Core(TM) i5-6600 CPU @ 3.30GHz
Number of Available Cores: 4
Available memory: 24 GB
Elixir 1.14.0
Erlang 25.0
Out of curiosity, I included this reduce version too:
Enum.reduce(list, {_group = [], _acc = []}, fn
0, {[], acc} -> {[], acc}
0, {group, acc} -> {[], [Enum.reverse(group) | acc]}
el, {group, acc} -> {[el | group], acc}
end)
|> case do
{[], acc} -> Enum.reverse(acc)
{group, acc} -> Enum.reverse([Enum.reverse(group) | acc])
end
4
Eiji
Here you go:
defmodule Example do
def sample(list) when is_list(list) do
# we start with one empty list
List.foldr(list, [[]], fn
# in case we got 0
# we are adding new empty list at beginning of result
0, acc -> [[] | acc]
# otherwise we are appending element
# as a head of first list in result
element, [head | tail] -> [[element | head] | tail]
end)
end
end
[1, 2, 3, 0, 4, 5, 0, 6, 7, 8, 0, 9]
|> Example.sample()
|> IO.inspect(charlists: :as_lists)
# [[1, 2, 3], [4, 5], [6, 7, 8], [9]]
See List.foldr/3 documentation.
3
hst337
Actually, this is not that hard to write. Just
def split(list, splitter, acc \\ [])
def split([], _, []), do: []
def split([], _, acc), do: [:lists.reverse acc]
def split([splitter | tail], splitter, acc) do
[:lists.reverse(acc) | split(tail, splitter, [])]
end
def split([item | tail], splitter, acc) do
split(tail, splitter, [item | acc])
end
2
Last Post!
adamu
Obligatory benchmarks.
Name ips average deviation median 99th %
recursion 3.98 M 251.49 ns ±11214.50% 188 ns 456 ns
foldr 2.99 M 334.99 ns ±12387.85% 223 ns 506 ns
reduce 2.70 M 370.10 ns ±9843.80% 258 ns 567 ns
chunk_while 1.40 M 712.55 ns ±4515.89% 532 ns 968 ns
chunk_by_reject 0.86 M 1164.60 ns ±2888.22% 896 ns 1427 ns
Comparison:
recursion 3.98 M
foldr 2.99 M - 1.33x slower +83.50 ns
reduce 2.70 M - 1.47x slower +118.61 ns
chunk_while 1.40 M - 2.83x slower +461.06 ns
chunk_by_reject 0.86 M - 4.63x slower +913.11 ns
Operating System: macOS
CPU Information: Intel(R) Core(TM) i5-6600 CPU @ 3.30GHz
Number of Available Cores: 4
Available memory: 24 GB
Elixir 1.14.0
Erlang 25.0
Out of curiosity, I included this reduce version too:
Enum.reduce(list, {_group = [], _acc = []}, fn
0, {[], acc} -> {[], acc}
0, {group, acc} -> {[], [Enum.reverse(group) | acc]}
el, {group, acc} -> {[el | group], acc}
end)
|> case do
{[], acc} -> Enum.reverse(acc)
{group, acc} -> Enum.reverse([Enum.reverse(group) | acc])
end
4
Popular in Questions
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Other popular topics
Hello, how can I check the Phoenix version ?
Thanks !
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









