{ "cells": [ { "cell_type": "code", "execution_count": 28, "id": "taken-punch", "metadata": {}, "outputs": [], "source": [ "import copy\n", "import itertools" ] }, { "cell_type": "markdown", "id": "driving-booth", "metadata": {}, "source": [ "testing a recursive function builder" ] }, { "cell_type": "code", "execution_count": 1, "id": "permanent-training", "metadata": {}, "outputs": [], "source": [ "def compose(f,g):\n", " return lambda *args, **kwds: f(g(*args,**kwds))" ] }, { "cell_type": "code", "execution_count": 2, "id": "editorial-alliance", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'b'] c\n" ] } ], "source": [ "print([\"a\",\"b\"],\"c\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "isolated-headset", "metadata": {}, "outputs": [], "source": [ "def appnd(lst,val):\n", " lst.append(val)\n", " return lst" ] }, { "cell_type": "code", "execution_count": 4, "id": "thirty-latter", "metadata": {}, "outputs": [], "source": [ "def traverse_split_right(tuplists):\n", " lst1,lst2 = tuplists\n", " lst1.append(lst2.pop(0))\n", " return (lst1,lst2)" ] }, { "cell_type": "code", "execution_count": 5, "id": "oriented-context", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(['a', 'b'], ['c'])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "traverse_split_right(([\"a\"],[\"b\",\"c\"]))" ] }, { "cell_type": "code", "execution_count": 6, "id": "unsigned-settlement", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(['a', 'b', 'c'], ['d', 'e', 'f'])\n", "(['a', 'b', 'c', 'd', 'e'], ['f'])\n" ] } ], "source": [ "args = (\n", " [\"a\",\"b\",\"c\"],\n", " [\"d\",\"e\",\"f\"]\n", " )\n", "print(args)\n", "\n", "flist = [traverse_split_right,traverse_split_right,print]\n", "rflist = list(reversed(flist))\n", "\n", "co = compose(rflist[0],compose(rflist[1],rflist[2]))\n", "\n", "co(args)" ] }, { "cell_type": "code", "execution_count": 34, "id": "satellite-sacrifice", "metadata": {}, "outputs": [], "source": [ "flist = [traverse_split_right,traverse_split_right,traverse_split_right]\n", "\n", "out_func = None\n", "out_func_list =[]\n", "\n", "for f in itertools.repeat(traverse_split_right,3):\n", " if out_func == None:\n", " out_func = f\n", " else:\n", " out_func = compose(f,out_func)\n", " \n", " out_func_list.append(out_func)" ] }, { "cell_type": "code", "execution_count": 39, "id": "certified-track", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(['a'], ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])\n", "(['a', 'b'], ['c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])\n", "(['a', 'b', 'c'], ['d', 'e', 'f', 'g', 'h', 'i', 'j'])\n", "(['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h', 'i', 'j'])\n" ] } ], "source": [ "#test the list\n", "args = (\n", " [\"a\"],\n", " [\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"h\",\"i\",\"j\"]\n", " )\n", "print(args)\n", "\n", "for fn in out_func_list:\n", " #reset the data each time\n", " a = copy.deepcopy(args)\n", " print(fn(a))" ] }, { "cell_type": "code", "execution_count": 38, "id": "interesting-moral", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(['a'], ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])\n", "(['a', 'b'], ['c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])\n", "(['a', 'b'], ['c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])\n", "(['a', 'b'], ['c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])\n", "(['a', 'b'], ['c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])\n" ] } ], "source": [ "#test a non-composed list of functions\n", "args = (\n", " [\"a\"],\n", " [\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"h\",\"i\",\"j\"]\n", " )\n", "print(args)\n", "for fn in itertools.repeat(traverse_split_right,4):\n", " #Reset the data each time\n", " a = copy.deepcopy(args)\n", " \n", " print(fn(a))\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "incorrect-migration", "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.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }