Skip to content

Instantly share code, notes, and snippets.

@davydovanton
Created July 3, 2019 10:17
Show Gist options
  • Save davydovanton/4f6d27d14babd7929b33ff30386721df to your computer and use it in GitHub Desktop.
Save davydovanton/4f6d27d14babd7929b33ff30386721df to your computer and use it in GitHub Desktop.
require 'dry/monads'
require 'dry/monads/right_biased'
require 'dry/monads/result'
require 'dry/monads/maybe'
module Dry
module Monads
module RightBiased
module Right
def deconstruct
[value!]
end
end
module Left
def deconstruct
[failure]
end
end
end
end
end
class Test
include Dry::Monads::Result::Mixin
include Dry::Monads::Maybe::Mixin
def call
matcher_method(Success([1, 2]))
matcher_method(Success([0, 3]))
matcher_method(Success(1))
matcher_method(Success('test'))
matcher_method(Failure(:not_found))
matcher_method(Failure(:other_error))
matcher_method(Maybe(1))
matcher_method(Maybe('1'))
matcher_method(Maybe(nil))
end
def matcher_method(monad)
case monad
in Success(0, x) then puts "X: #{x.inspect} in Success monad"
in Success(x: String) then puts "String #{x.inspect} in Success monad"
in Success(x) then puts "#{x.inspect} in Success monad"
in Some(x: Integer) then puts "Integer in Some monad"
in Some(x) then puts "#{x.inspect} in Some monad"
in None then puts "None in Maybe monad"
in Failure(:not_found) then puts "NOT FOUND in Failure monad"
in Failure(x) then puts "#{x.inspect} in Failure monad"
end
end
end
Test.new.call
# OUTPUT
# [1, 2] in Success monad
# [0, 3] in Success monad
# 1 in Success monad
# "test" in Success monad
# NOT FOUND in Failure monad
# :other_error in Failure monad
# 1 in Some monad
# "1" in Some monad
# None in Maybe monad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment