Last active
April 22, 2017 11:14
-
-
Save lpil/20443e197ee4786cc17387daebbec7ae to your computer and use it in GitHub Desktop.
Examples of Ruby Result monad in use
This file contains hidden or 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
# Given methods that return Results... | |
get_params | |
.and_then(method(:validate_params)) | |
.and_then(method(:save_record)) | |
.and_then(method(:send_email)) | |
.match(ok: method(:render_success), | |
error: method(:render_failure)) |
This file contains hidden or 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
# Given objects/classes that have a `.call` method which return Results... | |
[Result.ok(initial_state), | |
CheckVideoSize, | |
CheckVideoFrameRate, | |
CheckVideoFreezeDuration, | |
CheckVideoSilenceDuration, | |
SendToVideoService] | |
.reduce(:and_then) | |
# This array can be built dynamically. :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment