Skip to content

Instantly share code, notes, and snippets.

View NicolasT's full-sized avatar

Nicolas Trangez NicolasT

View GitHub Profile
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:
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'
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))
...:
/* 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;
/* 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;
}
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))
}
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))
}
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))
}
// 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] {
class Parser:
def parse(self, text):
self.onElement(text[::-1])
def onElement(self, t):
raise NotImplementedError
def cb_helper():
value = yield