Skip to content

Instantly share code, notes, and snippets.

@EgZvor
Created March 13, 2015 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EgZvor/84d9088be0e5aa554d80 to your computer and use it in GitHub Desktop.
Save EgZvor/84d9088be0e5aa554d80 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "",
"signature": "sha256:767a7d71da34f21ef483ff6e9864b38cdeb9361d1e0edf037f79f8a4df0138e2"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"def my_foldl(f, x0, lst):\n",
" for x in lst:\n",
" x0 = f(x0, x)\n",
" return x0\n",
"\n",
"def my_foldr(f, x0, lst):\n",
" for x in lst[::-1]:\n",
" x0 = f(x0, x)\n",
" return x0\n",
"\n",
"def concat(a, b):\n",
" return a + b"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def my_foldl2(f, x0, lst):\n",
" \"\"\"\u0424\u0443\u043d\u043a\u0446\u0438\u044f, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0430\u044f \u0441\u0432\u0451\u0440\u0442\u043a\u0443 \u0441\u043b\u0435\u0432\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0432\u0451\u0440\u0442\u043a\u0443 \u0441\u043f\u0440\u0430\u0432\u0430.\"\"\"\n",
" return (my_foldr(lambda a, b: lambda x: a(f(x, b)), lambda x:x, lst)(x0))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(my_foldl2(concat, 'a', ['b', 'c']))\n",
"print(my_foldl(concat, 'a', ['b', 'c']))\n",
"print(my_foldr(concat, 'a', ['b', 'c']))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"abc\n",
"abc\n",
"acb\n"
]
}
],
"prompt_number": 17
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment