Last active
August 29, 2015 14:01
-
-
Save ibizaman/1ff9a8092f4bbac79292 to your computer and use it in GitHub Desktop.
Error log as applicative function
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
| class MyClass: | |
| def __init__(self): | |
| self.errors = [] | |
| self.warnings = [] | |
| def apply(self, function, *args): | |
| result, self.errors, self.warnings = function(*args, errors=self.errors, warnings=self.warnings) | |
| return result | |
| def applicative_functor_style(self, ast): | |
| a = self.apply(f_a, ast) | |
| b = self.apply(f_b, a) | |
| def old_way(self, ast): | |
| a, self.errors, self.warnings = f_a(ast, self.errors, self.warnings) | |
| b, self.errors, self.warnings = f_b(a, self.errors, self.warnings) | |
| def f_a(ast, errors, warnings): | |
| # compute | |
| errors.append('Error #1') | |
| # compute | |
| warnings.append('Warning #1') | |
| # compute | |
| result = #... | |
| return (result, errors, warnings) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment