Created
August 24, 2019 19:21
-
-
Save jairoFernandez/85bae72a59321f7113e00d0aea5d3564 to your computer and use it in GitHub Desktop.
Sum Types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rdl' | |
extend RDL::Annotate | |
x = [1,2,3,6] | |
class Maybe | |
attr_accessor :data | |
end | |
class Just < Maybe | |
def initialize(data) | |
@data = data | |
end | |
end | |
class Empty < Maybe | |
end | |
type '(Array) -> Maybe' | |
def numeros_pares(data) | |
x = [] | |
data.each do |a| | |
x.push(a) if a%2 == 0 | |
end | |
if x.count == 0 | |
return Empty.new | |
end | |
return Just.new(x) | |
end | |
puts '===============' | |
puts 'Returns Just' | |
result = numeros_pares(x) | |
puts result.class | |
puts result.data | |
puts '===============' | |
puts 'Returns Empty' | |
x = [1,3,7] | |
result2 = numeros_pares(x) | |
puts result2.class | |
puts result2.data | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment