{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from fn import op, _\n", "from itertools import repeat\n", "from functools import reduce\n", "# from objproxies import LazyProxy\n", "import json\n", "from copy import deepcopy, copy\n", "from pipetools import pipe\n", "from functools import partial" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# def pipeline(*steps):\n", "# return reduce(lambda x, y: y(x), list(steps))\n", "# def compose(*funcs):\n", "# return lambda x: reduce(lambda f, g: g(f), list(funcs), x)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# ToDo:\n", "# Handle case where Mechanisms have no input. Perhaps the sentinel value of 0" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# UI Behavior Mechanisms per Mechanism\n", "def b1m1(s):\n", " return s['s1']\n", "def b2m1(s):\n", " return s['s1'] * s['s2']\n", "\n", "def b1m2(s):\n", " return s['s1']\n", "def b2m2(s):\n", " return s['s1'] / s['s2']\n", "\n", "# UI State Mechanisms per Mechanism\n", "def s1m1(s, _input):\n", " s['s1'] = s['s1']**2 + _input\n", "def s2m1(s, _input):\n", " s['s2'] = s['s2'] + 1 + _input \n", "def s3m1(s, _input):\n", " s['s3'] = s['s3']\n", "\n", "def s1m2(s, _input):\n", " s['s1'] = s['s1'] + _input\n", "def s2m2(s, _input):\n", " s['s2'] = s['s2']\n", "def s3m2(s, _input):\n", " s['s3'] = s['s3'] + 1\n", " \n", "j = {\n", " \"m1\": {\n", " \"behaviors\": {\n", " \"b1\": b1m1,\n", " \"b2\": b2m1\n", " },\n", " \"states\": {\n", " \"s1\": s1m1,\n", " \"s2\": s2m1,\n", " \"s3\": s3m1\n", " }\n", " },\n", " \"m2\": {\n", " \"behaviors\": {\n", " \"b1\": b1m2,\n", " \"b2\": b2m2\n", " },\n", " \"states\": {\n", " \"s1\": s1m2,\n", " \"s2\": s2m2,\n", " \"s3\": s3m2\n", " }\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def generate_configs(j):\n", " return list(\n", " map(\n", " lambda x: (\n", " list(j[x][\"states\"].values()),\n", " list(j[x][\"behaviors\"].values())\n", " ), \n", " j.keys()\n", " )\n", " )\n", "\n", "def getColResults(s, funcs):\n", " return list(map(lambda f: f(s), funcs))\n", "\n", "def getBehaviorInput(s, funcs): \n", " return op.foldr(_ + _)(getColResults(s, funcs))\n", "\n", "def mech_step(sL, state_funcs, behavior_funcs):\n", " in_copy, out_copy, mutatable_copy = deepcopy(sL), deepcopy(sL), deepcopy(sL)\n", " last_in_obj, last_mut_obj = in_copy[-1], mutatable_copy[-1]\n", " \n", " _input = getBehaviorInput(last_in_obj, behavior_funcs)\n", "\n", " for f in state_funcs:\n", " f(last_mut_obj, _input)\n", " \n", " out_copy.append(last_mut_obj)\n", " return out_copy\n", "\n", "def pipeline(states_list, configs):\n", " for config in configs:\n", " s_conf, b_conf = config[0], config[1]\n", " states_list = mech_step(states_list, s_conf, b_conf)\n", " return states_list" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'s1': 2, 's2': 4, 's3': 300},\n", " {'s1': 14, 's2': 15, 's3': 300},\n", " {'s1': 28.933333333333334, 's2': 15, 's3': 301}]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "state_dict = {\n", " 's1': 2,\n", " 's2': 4,\n", " 's3': 300\n", "}\n", "states_list = [state_dict]\n", "configs = generate_configs(j)\n", "pipeline(states_list, configs)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }