Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Implement pattern matching using Qo #21

Closed
wants to merge 2 commits into from

Conversation

bolshakov
Copy link
Owner

@bolshakov bolshakov commented Feb 17, 2019

See https://github.com/baweaver/qo

Some(42).match do |m|
  m.some { |x| x * 2 }
  m.none { 'none' }
end #=> 84

you can pass a conditions to #some method

Some(41).match do |m|
  m.some(:even?) { |x| x / 2 }
  m.some(:odd?, ->(v) { v > 0 }) { |x| x * 2 }
  m.none { 'none' }
end #=> 82

it raises Fear::MatchError if pattern match is not exhaustive

Some(42).match do |m|
  m.some(:odd?) { |x| x * 2 }
  m.none { 'none' }
end #=> raises Fear::MatchError

to avoid exception, you can pass #else branch

Some(42).match do |m|
  m.some(:odd?) { |x| x * 2 }
  m.else { 'nothing' }
end #=> nothing

Waiting the following PR to be released:

@bolshakov bolshakov force-pushed the feature/pattern-matching branch 3 times, most recently from 4943cac to 0807b33 Compare February 18, 2019 09:44
@bolshakov bolshakov changed the title [WIP] Implement pattern matching for Option using Qo [WIP] Implement pattern matching using Qo Feb 18, 2019
See https://github.com/baweaver/qo

```ruby
Some(42).match do |m|
  m.some { |x| x * 2 }
  m.none { 'none' }
end #=> 84
```

you can pass a conditions to #some method

```ruby
Some(41).match do |m|
  m.some(:even?) { |x| x / 2 }
  m.some(:odd?, ->(v) { v > 0 }) { |x| x * 2 }
  m.none { 'none' }
end #=> 82
```

it raises `Fear::MatchError` if pattern match is not exhaustive

```ruby
Some(42).match do |m|
  m.some(:odd?) { |x| x * 2 }
  m.none { 'none' }
end #=> raises Fear::MatchError
```

to avoid exception, you can pass `#else` branch

```ruby
Some(42).match do |m|
  m.some(:odd?) { |x| x * 2 }
  m.else { 'nothing' }
end #=> nothing
@bolshakov
Copy link
Owner Author

Decided to implement in other way #28

@bolshakov bolshakov closed this Mar 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant