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
f1 = lambda a: a + 1. | |
f2 = lambda a: 1. / a | |
f3 = lambda a: 2. * a | |
apply_ = lambda *funs: lambda arg: funs[0](arg) if len(funs) == 1 \ | |
else funs[-1](apply_(*funs[:-1])(arg)) | |
t1 = apply_(f1, f2, f3) | |
print t1(-2.) | |
try: |
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
p = lambda fun: lambda arg: (lambda m: fun(arg(m))) if callable(arg) \ | |
else fun(arg) | |
agent = p(lambda m: m.agent) | |
parent = p(lambda m: m.parent) | |
class D: pass | |
m1 = D() | |
m1.agent = 'agentm1' |
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
In [1]: class Person(object): | |
...: def __init__(self, name): | |
...: self.name = name | |
...: | |
...: def __str__(self): | |
...: return self.name | |
...: | |
...: def __add__(self, other): | |
...: return Person('%s and %s' % (self.name, other.name)) | |
...: |
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
/* POC code, abstract classes might be better */ | |
interface RootObject {} | |
class Machine implements RootObject { | |
private Agent agent; | |
private Machine parent; | |
public Machine(Agent agent) { | |
this.agent = agent; | |
this.parent = null; |
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
/* POC code, abstract classes might be better */ | |
class Machine { | |
private Agent agent; | |
private Machine parent; | |
public Machine(Agent agent) { | |
this.agent = agent; | |
this.parent = null; | |
} |
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
sealed case class Agent(name: String) | |
sealed case class Machine(agent: Agent, parent: Machine) | |
trait Applicable[I, O] { | |
def apply(a: Applicable[I, I]): Applicable[I, O] = { | |
val outer_apply: Function1[I, O] = apply | |
new Applicable[I, O] { | |
def apply(obj: I): O = outer_apply(a(obj)) | |
} |
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
sealed case class Agent(name: String) | |
sealed case class Machine(agent: Agent, parent: Machine) | |
trait Applicable[I, O] { | |
self => | |
def apply(a: Applicable[I, I]): Applicable[I, O] = { | |
new Applicable[I, O] { | |
def apply(obj: I): O = self(a(obj)) | |
} |
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
sealed case class Agent(name: String) | |
sealed case class Machine(agent: Agent, parent: Machine) | |
trait Applicable[I, O] { | |
self => | |
def apply(a: Applicable[I, I]): Applicable[I, O] = { | |
new Applicable[I, O] { | |
def apply(obj: I): O = self(a(obj)) | |
} |
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
// Some demo data classes | |
// Case classes are great! | |
sealed case class Agent(name: String) | |
sealed case class Machine(agent: Agent, parent: Machine) | |
// Our actual worker code. An Applicable is a Function1 which can also take | |
// another Applicable as argument, which will be combined. | |
// This works pretty much similar to the compose method of Function1, but | |
// without explicit call to it in client code | |
class Applicable[-T1, +R](fun: Function1[T1, R]) extends Function1[T1, R] { |
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 Parser: | |
def parse(self, text): | |
self.onElement(text[::-1]) | |
def onElement(self, t): | |
raise NotImplementedError | |
def cb_helper(): | |
value = yield |