summaryrefslogtreecommitdiff
path: root/starts
diff options
context:
space:
mode:
authorolpc user <olpc@xo-5d-f7-86.localdomain>2019-12-07 17:14:57 -0800
committerolpc user <olpc@xo-5d-f7-86.localdomain>2019-12-07 17:14:57 -0800
commit5295068d67545c1149896ccd5ee6a7778a30f671 (patch)
tree183f5b19e2c55fbe5d3b2cbcb79831c509857989 /starts
parentd341a2bbad41fd1def2bf46806fc572744b66533 (diff)
downloadstandingwithresilience-5295068d67545c1149896ccd5ee6a7778a30f671.tar.gz
standingwithresilience-5295068d67545c1149896ccd5ee6a7778a30f671.zip
habit sugar
Diffstat (limited to 'starts')
-rw-r--r--starts/meaning-vm/habit-starts/habit.hpp23
-rw-r--r--starts/meaning-vm/habit-starts/rhythm.cpp77
2 files changed, 45 insertions, 55 deletions
diff --git a/starts/meaning-vm/habit-starts/habit.hpp b/starts/meaning-vm/habit-starts/habit.hpp
new file mode 100644
index 0000000..5c6f5e8
--- /dev/null
+++ b/starts/meaning-vm/habit-starts/habit.hpp
@@ -0,0 +1,23 @@
+#pragma once
+
+#include <stdlib.h> // int rand(); void srand(int seed);
+#include <time.h> // int time(0);
+#include <unistd.h> // usleep(unsigned int usecs)
+
+__attribute__ ((visibility ("hidden"))) int __seed = (srand(time(0)), time(0));
+#define habitdelay \
+ static int thisdelay = (double(rand()) / RAND_MAX * 400000 + 200000); \
+ usleep(thisdelay)
+
+#undef self
+#define ahabit(name, ...) \
+ a(habit, name); \
+ (name).fun((std::function<ref(ref)>) \
+ [=](ref ctx) \
+ { \
+ habitdelay; \
+ ref self = name; \
+ (void)self; \
+ __VA_ARGS__ \
+ return intellect::level1::concepts::nothing; \
+ });
diff --git a/starts/meaning-vm/habit-starts/rhythm.cpp b/starts/meaning-vm/habit-starts/rhythm.cpp
index a89723c..3085da0 100644
--- a/starts/meaning-vm/habit-starts/rhythm.cpp
+++ b/starts/meaning-vm/habit-starts/rhythm.cpp
@@ -4,19 +4,12 @@
// Ideally, a human would run the rhythm.
#include "../level-1/level-1.hpp"
+#include "habit.hpp"
#include <iostream>
-#include <unistd.h> // usleep(unsigned int usecs)
-#include <stdlib.h> // int rand(); void srand(int seed);
-#include <time.h> // int time(0);
-
using namespace intellect::level1;
-#define habitdelay \
- static int thisdelay = (double(rand()) / RAND_MAX * 400000 + 200000); \
- usleep(thisdelay); \
-
int main()
{
@@ -29,7 +22,8 @@ int main()
// hence this time is kept secret, as this pattern is about learning
// to work with the timing of other processes.
- // four habits: output beat, wait, next-habit, and keep-doing
+ // six habits: next-habit, start-habit, keep-doing, output beat, wait, and start-beat
+ // not sure if one is redundant in there somewhere
decls(active, habit, step);
decls(beat, wait, next, keep, doing);
@@ -37,50 +31,39 @@ int main()
// structure habit
// next -> habit that follows
- // context -> data shared between habits
#undef self
- a(habit-step, next-habit);
- (next-habit).fun((std::function<ref(ref)>)
- [=](ref self)
+ ahabit(next-habit,
{
- habitdelay;
- ref ctx = self.get(context);
- ref n = self.get(next);
- n.set(context, ctx);
+ ref n = ctx.get(active-habit).get(next);
ctx.set(active-habit, n);
- return n(n);
+ return n(ctx);
});
- a(habit-step, start-habit);
- (start-habit).fun((std::function<ref(ref)>)
- [=](ref self)
+ ahabit(start-habit,
{
- habitdelay;
- ref ctx = self.get(context);
ref s = ctx.get(start);
ctx.set(active-habit, s);
- s.set(context, ctx);
- return s(s);
+ return s(ctx);
});
- a(habit-step, keep-doing-habit);
- (keep-doing-habit).fun((std::function<void(ref)>)
- [=](ref self)
+ ahabit(keep-doing-habit,
{
- habitdelay;
- ref ctx = self.get(context);
- ref(start-habit)(self);
+ ref(start-habit)(ctx);
while (true) {
- ref(next-habit)(ctx.get(active-habit));
+ ref(next-habit)(ctx);
}
});
- a(habit-step, beat-habit);
- (beat-habit).fun((std::function<void(ref)>)
- [=](ref self)
+ ahabit(start-beat,
{
- habitdelay;
- int & b = self.get(context).vget<int>(beat);
+ ctx.vset(beat, int(0));
+ self.set(next, wait-habit);
+ (beat-habit).set(next, wait-habit);
+ (wait-habit).set(next, beat-habit);
+ });
+ ahabit(beat-habit,
+ {
+ int & b = ctx.vget<int>(beat);
char const * beats[] = {
"A one!",
"and a two",
@@ -136,30 +119,14 @@ int main()
std::cout << beats[b] << std::endl;
b = (b + 1) % (sizeof(beats) / sizeof(*beats));
});
- a(habit-step, wait-habit);
- (wait-habit).fun((std::function<void(ref)>)
- [=](ref self)
+ ahabit(wait-habit,
{
habitdelay;
usleep(micros);
});
- a(habit-step, start-beat);
- (start-beat).fun((std::function<void(ref)>)
- [=](ref self)
- {
- habitdelay;
- self.get(context).vset(beat, int(0));
- self.set(next, wait-habit);
- (beat-habit).set(next, wait-habit);
- (wait-habit).set(next, beat-habit);
- });
-
a(context, habit-context);
(habit-context).set(start, start-beat);
-
- (keep-doing-habit).set(context, habit-context);
-
- (keep-doing-habit)(keep-doing-habit);
+ (keep-doing-habit)(habit-context);
}