ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - vic/ok_jose: Pipe elixir functions that match ok/error tuples or custom patterns.
Pipe elixir functions that match ok/error tuples or custom patterns. - vic/ok_jose
Visit Site

GitHub - vic/ok_jose: Pipe elixir functions that match ok/error tuples or custom patterns.

GitHub - vic/ok_jose: Pipe elixir functions that match ok/error tuples or custom patterns.

Ok Jose

help maintain this lib

A tiny library for piping values on a given pattern like the erlang idiom {:ok, _} and {:error, _} tuples.

You can also define your own pattern-matched pipes besides ok and error.

Besides ok_jose I've made a couple more of related macro libraries that might be related (or useful depending on your needs and taste):

  • happy from back before with landed elixir 1.2
  • pit a kind of pipeable with.

Installation

  1. Add ok_jose to your list of dependencies in mix.exs:
  def deps do
    [{:ok_jose, "~> 3.0.0"}]
  end

Motivation

A lot of erlang libraries follow the convention of returning {:ok, _} and {:error, _} tuples to denote the success or failure of a computation. It's my opinion that more people should embrace this convention on their code, and this library helps by making it easier to pipe values by unwrapping them from the tagged tuples.

This library is my try at having a beautiful syntax for a happy pipe, that is, a pipe that expects {:ok, _} tuples to be returned by each piped function. If any piped function returns a non matched value, the remaining functions expecting an {:ok, _} value won't get executed.

So, for example, the following code

filename
|> File.read()
|> case do
  {:ok, content} ->
    content |> Poison.Parser.parse()
  {:error, _} = error -> error
end

can be written as:

use OkJose

{:ok, filename}
|> File.read
|> Poison.Parser.parse
|> Pipe.ok

The main advantage of the macros defined by OkJose is that you don't need to learn new syntax it's just plain old Elixir piping. It's also very easy to define your own, as they are just case clauses.

Usage

Read the OkJose.Pipe docs for examples.

use OkJose

Provides you with the defpipe macro and aliases OkJose.Pipe as Pipe in the current lexical context.

ok/2

Passes values down the pipe as long as they match {:ok, value}.

{:ok, filename}
|> File.read
|> Poison.Parser.parse
|> Pipe.ok

see also ok!/2, error/2, error!/2

defpipe/2

Lets you define a custom pipe, for example, for working with ok tuples and also valid ecto changesets.

defpipe ok_or_valid do
  {:ok, value} -> value
  valid = %{valid?: true} -> valid
end


{:ok, %User{}}
|> cast(params, @required)
|> validate_required(:email)
|> Repo.insert
|> Pipe.tap({:ok, send_welcome_email}) # discard email
|> ok_or_valid
# => {:ok, inserted_user}
if/2,

Lets you pipe values as long as they satisfy a function predicate. This can be useful for cases where you need to call functions and not only pattern match on the piped values.

[1]
|> fn x -> [5 | x] end.()
|> fn x -> [4 | x] end.()
|> fn x -> [3 | x] end.()
|> fn x -> [2 | x] end.()
|> Pipe.if(fn x -> Enum.sum(x) < 10 end)
# => [4, 5, 1]
cond/2

A more generic way to select which values can be passed down the pipe and which cause the pipe to stop.

{:ok, jwt_token}
|> User.find_by_jwt
|> User.new_session
|> (Pipe.cond do
  # stop piping if no user found
  nil -> {false, {:error, :invalid_token}}

  # user gets piped only if not currently logged in
  {:ok, user = %User{}} -> 
    if User.is_logged_in?(user) do
      {false, {:error, :already_logged_in}}
    else
      {true, user}
    end
end)
and more

About

OkJose was born a while ago, back before we had Elixir 1.0, and before Elixir had its own with form to deal with this kind of problems. There are a lot of other libraries for dealing with tagged tuples and in general for monads in elixir. I'd recommend you to checkout these in particular:

  • ok A growing library, also has a list of alternatives at their Readme.

  • happy Work on OkJose lead me to creating happy and later happy_with They are just a bit less-pipeable than OkJose, and more on the spirit of Elixir's with

  • pit A weird one, for transforming and matching data at every pipe step. I don't know what I was thinking while doing it, but maybe it can be of use to someone.

Is it any good?

Yes

Take a look at the tests for more code examples.

マクロス Makurosu

[Elixir macros,] The things I do for beautiful code ― George Martin, Game of Thrones

#myelixirstatus #FridayLiterally

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory