Category Archives: Julia

Disabling auto-indentation of code in Julia REPL

By: Bogumił Kamiński

Re-posted from: https://juliasnippets.blogspot.com/2018/11/disabling-auto-indentation-of-code-in.html

With the recent release of Julia 1.0.2 there is still a small annoyance in the Julia REPL on Windows. If you copy-paste a code like this:

function f()
    for i in 1:10
        if i > 5
            println(i)
        end
    end
end

from your editor to your Julia REPL, you get the following result:

julia> function f()
           for i in 1:10
                   if i > 5
                               println(i)
                                       end
                                           end
                                           end
f (generic function with 1 method)

Notice, that Julia automatically indents the code which is pasted, but the code is already indented so the result does not look nice. This gets really bad when you paste 50 lines of highly nested code.

There is an open PR to fix this issue here, but since it did not get into Julia 1.0.2 I thought that I would post the hack I use to disable auto-indentation. Run the following lines in your Julia REPL:

import REPL
REPL.GlobalOptions.auto_indent = false
REPL.LineEdit.options(s::REPL.LineEdit.PromptState) = REPL.GlobalOptions

and now if you copy-paste to Julia REPL the code we have discussed above you get:

julia> function f()
           for i in 1:10
               if i > 5
                   println(i)
               end
           end
       end
f (generic function with 1 method)

and all is formatted as expected.

The solution overwrites REPL.LineEdit.options method to make sure that we always use REPL.GlobalOptions with auto-indentation disabled. It is not ideal, but I find it good enough till the issue is resolved.

If you would want to use this solution by default you can put the proposed code in your ~/.julia/config/startup.jl file.

Disabling auto-indentation of code in Julia REPL

By: Unknown

Re-posted from: https://juliasnippets.blogspot.com/2018/11/disabling-auto-indentation-of-code-in.html

With the recent release of Julia 1.0.2 there is still a small annoyance in the Julia REPL on Windows. If you copy-paste a code like this:

function f()
    for i in 1:10
        if i > 5
            println(i)
        end
    end
end

from your editor to your Julia REPL, you get the following result:

julia> function f()
           for i in 1:10
                   if i > 5
                               println(i)
                                       end
                                           end
                                           end
f (generic function with 1 method)

Notice, that Julia automatically indents the code which is pasted, but the code is already indented so the result does not look nice. This gets really bad when you paste 50 lines of highly nested code.

There is an open PR to fix this issue here, but since it did not get into Julia 1.0.2 I thought that I would post the hack I use to disable auto-indentation. Run the following lines in your Julia REPL:

import REPL
REPL.GlobalOptions.auto_indent = false
REPL.LineEdit.options(s::REPL.LineEdit.PromptState) = REPL.GlobalOptions

and now if you copy-paste to Julia REPL the code we have discussed above you get:

julia> function f()
           for i in 1:10
               if i > 5
                   println(i)
               end
           end
       end
f (generic function with 1 method)

and all is formatted as expected.

The solution overwrites REPL.LineEdit.options method to make sure that we always use REPL.GlobalOptions with auto-indentation disabled. It is not ideal, but I find it good enough till the issue is resolved.

If you would want to use this solution by default you can put the proposed code in your ~/.julia/config/startup.jl file.

Creating Interactive Online Flux.jl Demos

Neethu Mariya Joy is pursuing a BE(Hons.) in Computer Engineering at Birla Institute of Technology and Science in Pilani.

Neethu explains how she became interested in Julia and how she took the initiative to participate in the Google Summer of Code program:

“My introduction to programming was at school. I remember coming early to school with friends to figure out how some code written in BASIC works by trying out all sorts of variations at our computer lab. Back then, it was just something that was fun to play with.

“I was excited about getting a chance to study Computer Engineering at BITS Pilani. I joined a few technical clubs which helped me accelerate my learning process. I heard about Julia from a member of one of those clubs. When I first decided to give it a try, I tried converting some old node.js code into Julia. The code ran faster, but even more important, the code was much shorter, easier to read and easier to debug.

“I heard about the Google Summer of Code program from my seniors at college. When I learned that Julia was participating through NumFOCUS, I reached out to Julia Computing’s Mike Innes about my interest in contributing. Mike told me about the FluxJS demos project and I was really excited to work on it because it seemed fun and creative. Also, I got a chance to learn about the various models that could be implemented.

“My project was to create interactive demos for Flux.jl on the web. The challenging part was to get all the details of the demos right, especially while making an AlphaGo demo. I added features to FluxJS.jl as well. Currently, fluxml.ai contains five demos which were developed during the summer.

“Converting Flux.jl models with custom layers into tensorflow.js code can be done with a few primitives since julia lets you create a graph of the function calls used while executing a function by examining its source. For example, different layers that use the same basic function calls, say add and multiply inside them, can be converted to javascript using a set of core primitives instead of seperate primitives for each layer.

“These demos can be used to teach new users some of the potential applications of Flux. Each demo accompanies a link to the code. Having an interactive demo that can run on any browser can help new users understand the underlying models well.

“Going forward, I will continue working on more demos and create better ways to learn Flux.”