All Projects β†’ CrowdHailer β†’ Ok

CrowdHailer / Ok

Licence: apache-2.0
Elegant error/exception handling in Elixir, with result monads.

Programming Languages

elixir
2628 projects
macros
77 projects

Projects that are alternatives of or similar to Ok

Fp Resources
Functional programming great resources
Stars: ✭ 369 (-28.63%)
Mutual labels:  monad
Open Solution Home Credit
Open solution to the Home Credit Default Risk challenge 🏑
Stars: ✭ 397 (-23.21%)
Mutual labels:  pipeline
Pipeline
Pipeline is a package to build multi-staged concurrent workflows with a centralized logging output.
Stars: ✭ 433 (-16.25%)
Mutual labels:  pipeline
Piper
piper - a distributed workflow engine
Stars: ✭ 374 (-27.66%)
Mutual labels:  pipeline
Flowex
Flow-Based Programming framework for Elixir
Stars: ✭ 383 (-25.92%)
Mutual labels:  pipeline
Fluokitten
Category theory concepts in Clojure - Functors, Applicatives, Monads, Monoids and more.
Stars: ✭ 408 (-21.08%)
Mutual labels:  monad
Kedro
A Python framework for creating reproducible, maintainable and modular data science code.
Stars: ✭ 4,764 (+821.47%)
Mutual labels:  pipeline
Gaia
Build powerful pipelines in any programming language.
Stars: ✭ 4,534 (+776.98%)
Mutual labels:  pipeline
Jx
Jenkins X provides automated CI+CD for Kubernetes with Preview Environments on Pull Requests using Cloud Native pipelines from Tekton
Stars: ✭ 4,041 (+681.62%)
Mutual labels:  pipeline
Papermill
πŸ“š Parameterize, execute, and analyze notebooks
Stars: ✭ 4,458 (+762.28%)
Mutual labels:  pipeline
Kotlin Result
A multiplatform Result monad for modelling success or failure operations.
Stars: ✭ 369 (-28.63%)
Mutual labels:  monad
Tsmonad
Little monad library designed for TypeScript
Stars: ✭ 379 (-26.69%)
Mutual labels:  monad
Great expectations
Always know what to expect from your data.
Stars: ✭ 5,808 (+1023.4%)
Mutual labels:  pipeline
Kashti
Kashti is a dashboard for your Brigade pipelines.
Stars: ✭ 370 (-28.43%)
Mutual labels:  pipeline
Dashboard
A dashboard for Tekton!
Stars: ✭ 448 (-13.35%)
Mutual labels:  pipeline
Fun Task
Abstraction for managing asynchronous code in JS
Stars: ✭ 363 (-29.79%)
Mutual labels:  monad
Serving
A flexible, high-performance carrier for machine learning modelsοΌˆγ€Žι£žζ‘¨γ€ζœεŠ‘εŒ–ιƒ¨η½²ζ‘†ζžΆοΌ‰
Stars: ✭ 403 (-22.05%)
Mutual labels:  pipeline
Eff
Eff monad for cats - https://atnos-org.github.io/eff
Stars: ✭ 479 (-7.35%)
Mutual labels:  monad
Dry Monads
Useful, common monads in idiomatic Ruby
Stars: ✭ 453 (-12.38%)
Mutual labels:  monad
Rush
A cross-platform command-line tool for executing jobs in parallel
Stars: ✭ 421 (-18.57%)
Mutual labels:  pipeline

OK

Elegant error/exception handling in Elixir, with result monads.

Hex pm Build Status License

Result tuples

The OK module works with result tuples by treating them as a result monad.

{:ok, value} | {:error, reason}

See Handling Errors in Elixir for a more detailed explanation.

See FAQ at end of README for a few common question.

OK.for

OK.for/1 combines several functions that may fail.

  • Use the <- operator to match & extract a value for an :ok tuple.
  • Use the = operator as you normally would for pattern matching an untagged result.
require OK

OK.for do
  user <- fetch_user(1)             # `<-` operator means func returns {:ok, user}
  cart <- fetch_cart(1)             # `<-` again, {:ok, cart}
  order = checkout(cart, user)      # `=` allows pattern matching on non-tagged funcs
  saved_order <- save_order(order)
after
  saved_order                       # Value will be wrapped if not already a result tuple
end

OK.for/1 guarantees that it's return value is also in the structure of a result tuple.

OK.try

OK.try/1 combines several functions that may fail, and handles errors.

This is useful when writing code that has it's own representation of errors. e.g. HTTP Responses.

For example when using raxx to build responses the following code will always return a response.

require OK
import Raxx

OK.try do
  user <- fetch_user(1)             # `<-` operator means func returns {:ok, user}
  cart <- fetch_cart(1)             # `<-` again, {:ok, cart}
  order = checkout(cart, user)      # `=` allows pattern matching on non-tagged funcs
  saved_order <- save_order(order)
after
  response(:created)                # Value will be returned unwrapped
rescue
  :user_not_found ->
    response(:not_found)
  :could_not_save ->
    response(:internal_server_error)
end

OK Pipe

The pipe (~>>) is equivalent to bind/flat_map. The pipe (~>) is equivalent to map.

These macros allows pipelining result tuples through multiple functions for an extremely concise happy path.

use OK.Pipe

def get_employee_data(file, name) do
  {:ok, file}
  ~>> File.read
  ~> String.upcase
end

Use ~>> for File.read because it returns a result tuple. Use ~> for String.upcase because it returns a bare value that should be wrapped in an ok tuple.

Semantic matches

OK provides macros for matching on success and failure cases. This allows for code to check if a result returned from a function was a success or failure while hiding implementation details about how that result is structured.

import OK, only: [success: 1, failure: 1]

case fetch_user(id) do
  success(user) ->
    user
  failure(:not_found) ->
    create_guest_user()
end

FAQ

Why does OK not catch raised errors?

For the main rational behind this decision see the article Errors are not exceptional

Two other reasons:

  • Exceptional input and errors are not the same thing, OK leaves raising exceptions as a way to handle errors that should never happen.
  • Calls inside try/1 are not tail recursive since the VM needs to keep the stacktrace in case an exception happens. see source.

What about other shapes of error and success?

  • Accepting any extra forms is a slippery slope, and they are not always unambiguous. If a library is not returning errors as you like it is very easy to wrap in a custom function.

    def fetch_foo(map) do
      case Map.fetch(map, :foo) do
        {:ok, foo} -> {:ok, foo}
        :error -> {:error, :no_foo}
      end
    end
    

What changed in version 2.0

  • OK.with was deprecated.
  • use OK.Pipe was added.
  • OK.bind was renamed OK.flat_map.

Additional External Links and Resources

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].