Giter VIP home page Giter VIP logo

option.cr's Introduction

option.cr

Crystal implementation of Scala's Option Monad

Installation

Add this to your application's shard.yml:

dependencies:
  option:
    github: marceloboeira/option.cr

Global

By requiring the library you add the following functions/classes to your global scope

Functions

  • none - Creates an instance of the None(Nil) class
  • some(value) - Creates an instance of the Some(T) class with the value type. e.g.: some("foo") -> Some(String)
  • option(value) - Returns either a None or a Some(T) instance

Classes

  • Option(T) - Abstract class that defines the contract followed by None(T)/Some(T)
  • None(T) - Class that defines the interaction with nil values
  • Some(T) - Class that defines the interaction with the value

Interaction

  • get - Returns the value or raises an exception in case of nil
  • or_else(default) - Returns the value or the defined default
  • empty? - Returns true when the value is not nil, false when is nil

Usage

option("value") #<Some(String):0x10de55bc0 @value="value">
option(123) #<Some(Int32):0x10de5ef10 @value=123>
option(nil) #<None(Nil):0x10de5ef00>

option("123").or_else("0") # "123"
option(nil).or_else("0") # "0"

Examples

require "option"

module UnsafeIO
  class User
    def count : Int32?
      # IO operation can fail
      [10, nil].sample
    end
  end
end

10.times do
  users = option(UnsafeIO::User.new.count).or_else(0)
  puts "#{users} on storage"
end

Check the examples folder for more.

option.cr's People

Contributors

marceloboeira avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

option.cr's Issues

Pattern matching?

count = option(UnsafeIO::User.new.count)

case count
when is_some(0..Int32::Infinity)
  puts "#{users} signed up"
when is_none
  puts "no users signed up yet"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.