{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Demonstration of Pypi package OCaml for polyglot notebook\n", "\n", "TL;DR: [this Pypi `ocaml` package](https://pypi.org/project/ocaml/) allows to embed a real and complete OCaml runtime (with full standard library) in an IPython console or Jupyter notebook running IPython kernel (Python 3). OCaml functions and tiny programs can then be called directly from the IPython/Jupyter console, and results can be printed, or affected to Python variables! It even supports [Currying](https://en.wikipedia.org/wiki/Currying) multi-argument functions!\n", "\n", "See also: note, if you really want to use OCaml in a Jupyter notebook, the best solution is [OCaml-jupyter](https://github.com/akabe/ocaml-jupyter/) kernel!\n", "(i opened [an issue](https://github.com/akabe/ocaml-jupyter/issues/167) there to present this to the developer, just for his curiosity)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "- Author: [Lilian Besson (@Naereen)](https://github.com/Naereen/)\n", "- Date: 23/02/2021\n", "- License: [MIT License](https://lbesson.mit-license.org/)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Introduction\n", "\n", "> See [this issue](https://github.com/Naereen/notebooks/issues/28) I opened and assigned to myself." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "If you run this:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2021-02-23T02:31:38.720378Z", "start_time": "2021-02-23T02:31:38.717772Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "!pip3 install ocaml" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Then you can use basic OCaml expressions and standard library... from IPython or Jupyter notebook with IPython kernel, without having to install OCaml yourself on your laptop!\n", "It's pre-compiled. I don't know what version thought..." ] }, { "cell_type": "code", "execution_count": 176, "metadata": { "ExecuteTime": { "end_time": "2021-02-23T04:28:17.694520Z", "start_time": "2021-02-23T04:28:17.691326Z" }, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "import ocaml" ] }, { "cell_type": "code", "execution_count": 177, "metadata": { "ExecuteTime": { "end_time": "2021-02-23T04:28:18.246375Z", "start_time": "2021-02-23T04:28:18.238076Z" } }, "outputs": [], "source": [ "answer_to_life = %ocaml 40 + 2" ] }, { "cell_type": "code", "execution_count": 178, "metadata": { "ExecuteTime": { "end_time": "2021-02-23T04:28:18.743192Z", "start_time": "2021-02-23T04:28:18.734826Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n", "\n" ] } ], "source": [ "print(answer_to_life)\n", "print(type(answer_to_life)) # a real integer!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It would be a great project to show to students studying Python and C and OCaml : this tiny IPython magic makes the link between Python and OCaml (one way) using OCaml compiled as a dynamic extension to CPython 😍 !" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Reference and missing documentation of `ocaml` Pypi package\n", "\n", "This [Jupyter notebook](https://jupyter.org) uses the [ocaml v0.0.11](https://pypi.org/project/ocaml/) package from Pypi.\n", "\n", "There is no documentation, I asked the [author](https://github.com/LaurentMazare/), who works at [JaneStreet](https://www.janestreet.com/) and he redirected me to [this blog post](https://blog.janestreet.com/using-python-and-ocaml-in-the-same-jupyter-notebook/) by JaneStreet." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Apparently, being professional developers doesn't mean they restrain themselves from shipping unfinished and undocumented packages to Pypi. Okay... Why? It seems highly unprofessional! I understand being in a hurry, but then just don't publish to Pypi, and let user install it using `pip` but from a Git repository:\n", "```bash\n", "$ pip install git+https://github.com//\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "[Their blog post](https://blog.janestreet.com/using-python-and-ocaml-in-the-same-jupyter-notebook/) states\n", "> *Note that this package is not currently very well polished but it should give some ideas of what can be done through this Python-OCaml integration.*\n", "\n", "But come on, writing a short README or description in the `setup.py` is the least that they should do..." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Iframe showing the page:\n", "