Skip to content

Instantly share code, notes, and snippets.

@rlb3
Created January 17, 2022 07:14
Show Gist options
  • Save rlb3/23f223dfd2175f205737639e7fbd1258 to your computer and use it in GitHub Desktop.
Save rlb3/23f223dfd2175f205737639e7fbd1258 to your computer and use it in GitHub Desktop.
# Day List
## Module
```elixir
defmodule DayList do
@days [:sun, :mon, :tues, :wed, :thurs, :fri, :sat]
@spec build_list(first :: atom(), last :: atom()) :: list() | {:error, :invalid_day}
def build_list(first, last) when first in @days and last in @days do
@days
|> Stream.cycle()
|> Stream.drop_while(&(&1 != first))
|> Stream.take_while(&(&1 != last))
|> Enum.to_list()
|> Kernel.++([last])
end
def build_list(_, _), do: {:error, :invalid_day}
end
```
```output
{:module, DayList, <<70, 79, 82, 49, 0, 0, 9, ...>>, {:build_list, 2}}
```
```elixir
DayList.build_list(:sat, :fri)
```
```output
[:sat, :sun, :mon, :tues, :wed, :thurs, :fri]
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment