{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-01T14:41+0000",
     "start_time": "2019-02-01T14:41+0000"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "null"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "println(\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-01T15:13+0000",
     "start_time": "2019-02-01T15:13+0000"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10.0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "null"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "show = { println it }\n",
    "square_root = { Math.sqrt(it) }\n",
    "\n",
    "def please(action) {\n",
    "    [the: { what ->\n",
    "        [of: { n -> action(what(n)) }]\n",
    "    }]\n",
    "}\n",
    "\n",
    "// equivalent to: please(show).the(square_root).of(100)\n",
    "please show the square_root of 100\n",
    "// ==> 10.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-01T14:45+0000",
     "start_time": "2019-02-01T14:45+0000"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]"
     ]
    },
    {
     "data": {
      "text/plain": [
       "null"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def lst = [1,2,3,4] \n",
    "print lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-01T14:45+0000",
     "start_time": "2019-02-01T14:45+0000"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "How are things? We are doing well. Take care"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class EmailDsl {  \n",
    "   String toText \n",
    "   String fromText \n",
    "   String body \n",
    "\t\n",
    "   /** \n",
    "   * This method accepts a closure which is essentially the DSL. Delegate the \n",
    "   * closure methods to \n",
    "   * the DSL class so the calls can be processed \n",
    "   */ \n",
    "   \n",
    "   def static make(closure) { \n",
    "      EmailDsl emailDsl = new EmailDsl() \n",
    "      // any method called in closure will be delegated to the EmailDsl class \n",
    "      closure.delegate = emailDsl\n",
    "      closure() \n",
    "   }\n",
    "   \n",
    "   /** \n",
    "   * Store the parameter as a variable and use it later to output a memo \n",
    "   */ \n",
    "\t\n",
    "   def to(String toText) { \n",
    "      this.toText = toText \n",
    "   }\n",
    "   \n",
    "   def from(String fromText) { \n",
    "      this.fromText = fromText \n",
    "   }\n",
    "   \n",
    "   def body(String bodyText) { \n",
    "      this.body = bodyText \n",
    "   } \n",
    "}\n",
    "\n",
    "EmailDsl.make { \n",
    "   to \"Nirav Assar\" \n",
    "   from \"Barack Obama\" \n",
    "   body \"How are things? We are doing well. Take care\" \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-01T14:52+0000",
     "start_time": "2019-02-01T14:52+0000"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[a , b_ , c]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@Grab('com.google.guava:guava:r09')\n",
    "import com.google.common.base.*\n",
    "def result = Splitter.on(',').trimResults(CharMatcher.is('_' as char)).split(\"_a ,_b_ ,c__\").iterator().toList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-01T14:52+0000",
     "start_time": "2019-02-01T14:52+0000"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "null"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@Grab('com.google.guava:guava:r09')\n",
    "import com.google.common.base.*\n",
    "def split(string) {\n",
    "  [on: { sep ->\n",
    "    [trimming: { trimChar ->\n",
    "      Splitter.on(sep).trimResults(CharMatcher.is(trimChar as char)).split(string).iterator().toList()\n",
    "    }]\n",
    "  }]\n",
    "}\n",
    "\n",
    "// def result = split \"_a ,_b_ ,c__\" on ',' trimming '_\\'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-01T14:52+0000",
     "start_time": "2019-02-01T14:52+0000"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "null"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// def result = split \"_a ,_b_ ,c__\" on ',' trimming '_\\'\n",
    "def binding = new Binding()             \n",
    "def shell = new GroovyShell(binding)    \n",
    "binding.setVariable('x',1)              \n",
    "binding.setVariable('y',3)\n",
    "shell.evaluate 'z=2*x+y'                \n",
    "assert binding.getVariable('z') == 5    \n",
    "println(binding.getVariable('z'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-01T15:13+0000",
     "start_time": "2019-02-01T15:13+0000"
    }
   },
   "outputs": [
    {
     "ename": "java.lang.ClassCastException",
     "evalue": " class [B cannot be cast to class [C ([B and [C are in module java.base of loader 'bootstrap')",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mjava.lang.ClassCastException: class [B cannot be cast to class [C ([B and [C are in module java.base of loader 'bootstrap')\u001b[0;0m",
      "\u001b[1;31m\tat script1549034005586.jsonProc(script1549034005586:5)\u001b[0;0m",
      "\u001b[1;31m\tat this cell line 11\u001b[0;0m",
      "\u001b[0;31m\tat com.twosigma.beakerx.groovy.evaluator.GroovyCodeRunner.runScript(GroovyCodeRunner.java:94)\u001b[0;0m",
      "\u001b[0;31m\tat com.twosigma.beakerx.groovy.evaluator.GroovyCodeRunner.call(GroovyCodeRunner.java:59)\u001b[0;0m",
      "\u001b[0;31m\tat com.twosigma.beakerx.groovy.evaluator.GroovyCodeRunner.call(GroovyCodeRunner.java:32)\u001b[0;0m"
     ]
    }
   ],
   "source": [
    "import groovy.json.JsonSlurper\n",
    "\n",
    "static void jsonProc() {\n",
    "    def jsonSlurper = new JsonSlurper()\n",
    "    def object = jsonSlurper.parseText('{ \"name\": \"John\", \"ID\" : \"1\"}')\n",
    "\n",
    "    println(object.name);\n",
    "    println(object.ID);\n",
    "}\n",
    "\n",
    "jsonProc()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Groovy",
   "language": "groovy",
   "name": "groovy"
  },
  "language_info": {
   "codemirror_mode": "groovy",
   "file_extension": ".groovy",
   "mimetype": "",
   "name": "Groovy",
   "nbconverter_exporter": "",
   "version": "2.4.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": false,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": false,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
