Tag Archives: Julia

Generating Julia doc into Org-Mode documents

By: Picaud Vincent

Re-posted from: https://pixorblog.wordpress.com/2018/04/29/generating-julia-doc-into-org-mode-documents/

1 Context

This post presents J4Org.jl a Julia package I have started to develop to include Julia doc into Org-Mode documents. My goal was to be able to code and document Julia packages without leaving Emacs and to reduce as much as possible the burden of documentation.

2 Julia code documentation

Here is a short example. Imagine that your package is:

module Foo

export Point, foo
    
import Base: norm

#+Point L:Point_struct
# This is my Point structure
#
# *Example:*
#
# Creates a point $p$ of coordinates $(x=1,y=2)$.
#
# #+BEGIN_SRC julia :eval never :exports code
# p=Point(1,2)
# #+END_SRC
#
# You can add any valid Org-Mode directive. If you want to use
# in-documentation link, use [[norm_link_example][]]
#
struct Point
    x::Float64
    y::Float64
end

#+Point
# Creates Point at origin $(0,0)$ 
Point() = Point(0,0)

#+Point,Method L:norm_link_example
# A simple function that computes $\sqrt{x^2+y^2}$
#
# *Example:*
#!p=Point(1.0,2.0);
#!norm(p) 
#
# See: [[Point_struct][]]
#
norm(p::Point)::Float64 = sqrt(p.x*p.x+p.y*p.y)

#+Method,Internal
# An internal function
#
# For symbol that are not exported, do not forget the "Foo." prefix:
#!p=Point(1.0,2.0)
#!Foo.foo(2.0,p)
foo(r::Float64,p::Point) = Point(r*p.x,r*p.y)

end

The documentation template is very simple. Before each item you want to document add these comment lines:

#+Tag1,Tag2,... L:an_extra_link_if_required 
#
# Here you can put any Org mode text, for instance $sin(x)$
#
#!sin(5) # julia code to be executed
#
# [[internal_link][]]
struct A_Documented_Struct 
...
end 
  • #+Tag1,Tag2,… is mandatory, “#+” is followed by a list of tags. Later when you want to extract doc you can do filtering according these tags.
  • L:an_extra_link_if_required is not mandatory. It defines a reference if you want to create doc links. The previous statement defines a link target named an_extra_link_if_required.
  • [[internal_link][]] creates a link to a previously defined L:internal_link.
  • !sin(5) will execute Julia code and include the output in the doc.

Also note that you can keep compatibility with docstring as follows:

""" 
    foo() 

foo function...
"""
#+Tag
#
# foo function
foo() = ...

3 Org-Mode side

You need Org-Mode plus ob-julia.el to be installed. For illustration we use this minimal Org-Mode document:

#+PROPERTY: header-args:julia :session *my_session* :exports code :eval no-export
#+OPTIONS: ^:{}
#+TITLE: Getting Started with a minimal example

#+BEGIN_SRC julia :results output none :eval no-export :exports none
using J4Org 
initialize_boxing_module(usedModules=["Foo"]) 
documented_items=create_documented_item_array("Foo.jl")
#+END_SRC

* Example

Prints all documented items, except those tagged with "Internal" 
#+BEGIN_SRC julia :results output drawer :eval no-export :exports results
print_org_doc(documented_items,tag_to_ignore=["Internal"],header_level=0)
#+END_SRC
  • using J4Org uses this package
  • initialize_boxing_module(usedModules=[“Foo”]) defines what are the modules to use when executing Julia code extracted from the doc (the “#!” statements). Here we are documenting the Foo module, hence we must use it.
  • create_documented_item_array(“Foo.jl”) creates the list of documented items from file “Foo.jl”. You can use a list of files or a directory.
  • print_org_doc(documented_items,tag_to_ignore=[“Internal”],header_level=0) prints all documented items, except those tagged with “Internal”.

4 Result after html-export

When html-exported with Org-Mode this will generate this document:

Index: [P] Point [n] norm

  • Point
struct Point

This is my Point structure

Example:

Creates a point p of coordinates (x=1,y=2).

p=Point(1,2)

You can add any valid Org mode directive. If you want to use in-documentation link, use norm(…)

Foo.jl:8, back to index

Point()

Creates Point at origin (0,0)

Foo.jl:27, back to index

  • norm
norm(p::Point)::Float64

A simple function that computes \sqrt{x^2+y^2}

Example:

p=Point(1.0,2.0);
norm(p) 
2.23606797749979

See: struct Point

Foo.jl:31, back to index

5 Further information

You can visit J4Org.jl for further details. You can even install it (Pkg.add("J4Org")). I have used it to document DirectConvolution.jl (this Direct Convolution Package html page for instance).

Disclaimer: this package is still in early development.

Converting Julia to Javascript and WebAssembly

By: Julia – Alex Mellnik

Re-posted from: http://alex.mellnik.net/converting-julia-to-javascript-and-webassembly/

I have a “two language problem”, but not this two language problem.  I often find myself prototyping small data analysis projects in Julia, and then rewriting them as web applications using Angular & Typescript to make them easily accessible to a broad audience.  Much of the work is similar in Julia and Typescript (such as creating plots) so it’s not that annoying to translate, but occasionally I run into analytical code that is really painful to rewrite in Typescript.  Some tasks that I could accomplish in a few lines in Julia (usually with the help of a package) can turn into hundreds of awkward lines in Typescript.

I’ve always dreamed of the day that I could write arbitrary Julia code and have it run in a browser, and while we’re definitely not there yet, we’re getting closer.  One first step forward was made by Tom Short, who developed two distinct ways to generate Emscripten-friendly LLVM bitcode from Julia v0.7.  Recently, Mike Innes (with contributions from Tom) developed a new approach (Charlotte.jl) that works with Julia v0.6 and generates Javascript/WebAssembly directly from Julia.  See the source repositories for details on how to use them, and you can also try both out in your browser using demos that I put together at julia2js.gotfork.net.

Julia and The Reincarnation of Lisp

By: Lispy Arnuld

Re-posted from: https://lispyarnuld.wordpress.com/2018/04/04/julia-and-the-reincarnation-of-lisp/

I have programmed in C for 5 years now. I can say from experience, after first 1.5 years, I began to understand pointers, malloc and arrays of pointers to pointers of structs containing pointers. I really mean begin. In K&R2, authors said that they feel after 10 years in C, they are still learning. I used to struggle to think in C for 3-4 years. 5th years totally changed that. I can solve problems in C after 5 years of experience which I never could never did earlier and I am very much confident in C now. Imagine 10+10 more years in C, what it will make me, a Samson of C ? I have, personally, coded in many languages, e.g.:

  • Common Lisp
  • Scheme
  • OCaml
  • Haskell
  • Ruby
  • ATS
  • At least 20 others I forgot.

I could never use them professionally on large scale. Just some toy programs here and there and sometimes bit bigger those of Practical Common Lisp, The Gigamonkeys Book. I loved Common Lisp but could never use it. When every good company started having Python in their job description, I was still loving Common Lisp and thinking that is from where Python got most of ideas. Then last month I came across Julia. It looked like Ruby to me (in syntax) but it was faster, way faster than any language, it is just bit slower than C,C++. It joined Petaflop club recently. Working with so many languages taught me 2 things: along with the tech and features, a Programming Language is lot about its design and whys. It has a mental model behind. I was looking for a language with speed of C, with an Open-Source License and where I could have dynamic typing too. A language where I could use my mind to solve a problem than memory problems and Segfaults. A language which is far simpler than C++ (By all means, C++ is a great language, if I have to create an OS, I would use it. Still, it is a very complex language to work in it). Then I came to realize, I was not the only one thinking along those lines. Few Lispers were thinking same. Then I read Why of Julia. I felt like it was written for me. Out of all those experiences and experiments over half a decade, I can tell you, Julia is really a great language to code in at least for 5 years of your life. I am sure, any person who likes to improve his skills, looking for better ideas, better design in software, will love Julia. I am typing this from my home computer running Arch Linux with LXQt. It is amazing to chat on Julia IRC gitter at 10:15 PM in night

Julia community is quite friendly and helpful. One thing i can’t take off my mind is when I was writing toy programs in Python and Ruby. Python looked much friendlier and Ruby looked alien (think less intuitive) and C hurt the head. I read Why’s Poignant guide to Ruby at that time. After 5 years coding totally in C and C++, I feel quite comfortable with both, they still surprise me though sometimes. I don’t know but whenever I look at Julia code/syntax, it always reminds me of Ruby code/syntax . The syntax feels same, less intuitive. I struggle a bit too. Writing Python code I felt was much easier but that easiness and friendliness and intuitiveness wore-off in a week. At the same time of trying out a 100 different languages and environments, I think this easiness/friendliness or lack of it has not much to do with mastering the language and its design and liking it at heart. It all comes with practice and associating with people who already know the language, you just have to follow their guidance. So, yeah, Julia community is full of helpful people. I am still wondering though how long it will take me to get over this mental obstacle of lack of intuitiveness towards Julia syntax. I only know one way, get totally immersed in the language and community and write good code, read reference docs/manuals, read blogs and articles on Julia.

I like Lisp a lot, you will too if you work with it. Lisp could never break out into the mainstream. The languages it influenced (Python, Ruby, Scala, Perl etc., even Julia) all became mainstream but Lisp could not. I have even read Kent M Pitman‘s really long conversations on Scheme Language Standard on comp.lang.scheme newsgroup (try Aioe and Pan Newsreader ) when I was choosing to learn between Scheme and Common Lisp (one such conversation can be found on Google Groups) . I read all that out of strong interests, now I realize it was all a part of experience towards greater understanding of programming languages. I liked Lisp and I wanted performance. I think Julia will be mainstream in few years and it is the language I was waiting for since the day I wrote my first program. If you like coding/programming even a bit, if it touches your heart even a little bit. You must code in Julia, must read its documentation. It is a breath of fresh air after a long time.