Created
March 29, 2017 01:08
-
-
Save milesrout/0fd127a02bc0f2bedd431c7b74c3fb53 to your computer and use it in GitHub Desktop.
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
from enum import Enum | |
CounterEvent = Enum('CounterEvent', 'UP DOWN') | |
def counter(): | |
def apply(f, x): | |
return f(x) | |
def handle_event(e): | |
etype, data = e | |
if etype is CounterEvent.UP: | |
return (lambda x: x + data) | |
elif etype is CounterEvent.DOWN: | |
return (lambda x: x - data) | |
return accumulate(apply, 0, handle_event) | |
class accumulate: | |
def __init__(self, f, init, handle_event): | |
self.f = f | |
self.init = init | |
self.handle_event = self.handle_event | |
def get_value(self): | |
return self.init | |
def handle_event(self, e): | |
self.init = self.f(self.handle_event(e), self.init) |
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
module Counter where | |
counter :: Behaviour (Event CounterEvent) Integer | |
counter = accumulate ($) 0 (\e -> case e of | |
Up n -> (+n) | |
Down n -> (subtract n)) | |
data CounterEvent = Up Integer | Down Integer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment