{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Writing Julia Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The purpose of these notes is to introduce [R](http://www.r-project.org) programmers to the [Julia](http://julialang.org) programming language.\n", "\n", "Julia functions are sufficiently similar to R functions that most R programmers can read most Julia functions. They are sufficiently different that initial attempts at writing a Julia function can be frustrating.\n", "\n", "One way of learning a programming language like Julia is to read programs written by experienced in that language. One of the amazing aspects of Julia is that much of the Base system is written in Julia itself. One of the current (June, 2014) deficiencies of Julia is that the documentation, especially package documentation, is quite sparse and you often need to read the source of a function to understand its purpose.\n", "\n", "In these notes I am assuming the use of julia-0.4.2 or later." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Julia Version 0.4.6\n", "Commit 2e358ce (2016-06-19 17:16 UTC)\n", "Platform Info:\n", " System: Linux (x86_64-unknown-linux-gnu)\n", " CPU: Intel(R) Core(TM) i5-3570 CPU @ 3.40GHz\n", " WORD_SIZE: 64\n", " BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)\n", " LAPACK: libopenblas64_\n", " LIBM: libopenlibm\n", " LLVM: libLLVM-3.3\n" ] } ], "source": [ "versioninfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tools for examining functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Method signatures and sources" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Entering the name of a function in R returns the definition of the function. Entering the name of a function in Julia provides information on the number of methods defined for that generic. Remember that all Julia functions are generic functions." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "mean (generic function with 4 methods)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mean" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `methods` function gives the signatures of the methods for the generic." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "4 methods for generic function mean:
μ | η | |
---|---|---|
1 | 0.0944217798994027 | -2.260801891104243 |
2 | 0.9366111003876687 | 2.692979387335428 |
3 | 0.2583267373061602 | -1.0546835920616155 |
4 | 0.9309236942019032 | 2.600965538110032 |
5 | 0.5552832964597973 | 0.22204096940697216 |
6 | 0.8715098619396013 | 1.9143750261699557 |
7 | 0.04155298306875843 | -3.138344970491189 |
8 | 0.9687790528318041 | 3.4349473171034735 |
9 | 0.6535657113441573 | 0.6347499234293605 |
10 | 0.4581009073306117 | -0.16799032669054131 |