Created
January 17, 2022 07:14
-
-
Save rlb3/23f223dfd2175f205737639e7fbd1258 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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