Sebb

Sebb

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 enumerable in two lists according to the given function fun. [split_with]

Showing Posts 1 to 9

dimitarvp

dimitarvp

You can just pipe the chunk_by result like this: |> Enum.reject(&1 == [0])?

hst337

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
LostKobrakai

LostKobrakai

Not much shorter, but using Enum instead of recursion:

l =  [1,2,3,0,4,5,0,6,7,8,0,9]

Enum.chunk_while(l, [], fn 
  0, acc -> {:cont, Enum.reverse(acc), []}
  element, acc -> {:cont, [element | acc]}
end, fn
  [] -> {:cont, []}
  acc -> {:cont, Enum.reverse(acc), []}
end)
# [[1, 2, 3], [4, 5], [6, 7, 8], [9]]
Sebb

Sebb OP

Thats what I’m doing. Was just wondering why Enum does not do this and if s.o. else bothers.

dimitarvp

dimitarvp

The answer is probably in the question: because it’s fairly easy to assemble the desired solution and because it’s not preferable to devise a lot of list/stream combinators that can confuse people.

adamu

adamu

iex(2)> l |> Enum.join() |> String.split("0")
["123", "45", "678", "9"]

:troll:

Interesting though, especially as Enum.intersperse/2 exists.

Sebb

Sebb OP

:see_no_evil:

Eiji

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.

adamu

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
— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
subsaharancoder
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews