Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement flat_map #3

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,74 @@ rail-*.tar
/tmp/

mix.lock

# Created by https://www.toptal.com/developers/gitignore/api/macos,linux,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,linux,visualstudiocode

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### macOS Patch ###
# iCloud generated files
*.icloud

### VisualStudioCode ###
.vscode/*
# !.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix

### VisualStudioCode Patch ###
# Ignore all local history of files
.history
.ionide

# End of https://www.toptal.com/developers/gitignore/api/macos,linux,visualstudiocode
34 changes: 34 additions & 0 deletions lib/rail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ defmodule Rail do
:error
iex> {:error, :noent} |> Rail.map_error(fn :noent -> :not_found end)
{:error, :not_found}
iex> {:ok, 1} |> Rail.map_error(fn :noent -> :not_found end)
{:ok, 1}

"""
def map_error({:error, value}, fun) when is_function(fun, 1), do: {:error, fun.(value)}
Expand Down Expand Up @@ -373,4 +375,36 @@ defmodule Rail do
def normalize({tag, v1, v2, v3}) when tag in [:ok, :error], do: {tag, {v1, v2, v3}}
def normalize({tag, v1, v2, v3, v4}) when tag in [:ok, :error], do: {tag, {v1, v2, v3, v4}}
def normalize(untagged), do: {:ok, untagged}

@doc """
Return a new tuple for value of {:ok, value}, otherwise bypass

## Examples

iex> :ok |> Rail.flat_map_ok(fn 1 -> {:noreply, 10} end)
:ok
iex> {:ok, 1} |> Rail.flat_map_ok(fn 1 -> {:noreply, 10} end)
{:noreply, 10}
iex> {:error, 1} |> Rail.flat_map_ok(fn 1 -> {:noreply, 10} end)
{:error, 1}

"""
def flat_map_ok({:ok, value}, fun) when is_function(fun, 1), do: fun.(value)
def flat_map_ok(other, _), do: other

@doc """
Return a new tuple for error of {:error, error}, otherwise bypass

## Examples

iex> :error |> Rail.flat_map_error(fn :noent -> {:noreply, :not_found} end)
:error
iex> {:error, :noent} |> Rail.flat_map_error(fn :noent -> {:noreply, :not_found} end)
{:noreply, :not_found}
iex> {:ok, 1} |> Rail.flat_map_error(fn :noent -> {:noreply, :not_found} end)
{:ok, 1}

"""
def flat_map_error({:error, value}, fun) when is_function(fun, 1), do: fun.(value)
def flat_map_error(other, _), do: other
end
Loading