exercism/elixir/series/lib/string_series.ex

13 lines
451 B
Elixir

defmodule StringSeries do
@doc """
Given a string `s` and a positive integer `size`, return all substrings
of that size. If `size` is greater than the length of `s`, or less than 1,
return an empty list.
"""
@spec slices(s :: String.t(), size :: integer) :: list(String.t())
def slices(s, size) do
length = String.length(s)
for start <- 0..(length - size), size in 1..length, into: [], do: String.slice(s, start, size)
end
end