Equivalent of Python's 'with' in Julia? - julia

Does Julia have an equivalent of Python's with? Maybe as a macro? This is very useful, for example, to automatically close opened files.

Use a do block. Docs on do blocks are here.
And here is an example of how to do the usual with open(filename) as my_file of Python in Julia:
open("sherlock-holmes.txt") do filehandle
for line in eachline(filehandle)
println(line)
end
end
The above example is from the Julia wikibooks too.

Although the do block syntax does have certain similarities to Python's with statement, there is no exact equivalent. This is discussed in further detail in the GitHub issue "with for deterministic destruction". The issue concludes that this structure should be added to Julia, although no syntax or plan for such is established.

Related

How do I reload a module under development in Julia 1.6?

I know this question has been asked and answered before, but none of the many answers work for me as described.
What is the procedure for reloading a module that I'm working on in Julia (1.6)?
For example, I have
module MyModule
export letters
const letters = String('A':'Z')
end
and I want the be able to load the module, make changes to letters in the module's file, and then reload the module and have those changes reflected in subsequent uses of letters. This seems simple enough, but I can't get it to work.
I've tried
include("src/MyModule.jl")
using .MyModule
but if I change the definition of letters in MyModule.jl and then
include("src/MyModule.jl")
letters doesn't change, unless I fully qualify its use each time with Main.MyModule.letters: using Main.MyModule; letters refers, for example, to the old definition.
How do I reload a module under development so that I can refer to its definitions without fully qualifying them (and without having an unqualified shadow definition always lying around)?
I would just use Revise.jl and wrap everything in functions:
module MyModule
export letters
letters(char_start, char_end) = char_start:char_end |> String
end
julia> using Revise
julia> includet("src/MyModule.jl")
julia> using .MyModule
julia> letters('l', 'p')
"lmnop"
module MyModule
export letters
letters(char_start, char_end) = char_start:char_start |> String
end
julia> letters('l', 'p')
"l"
const is for defining things that you do not want to modify, so I would not expect your original version to work as expected. Revise.jl should also throw a redefinition error if you try to change it
In general though, it's usually much nicer (and easier too!) to just put everything in a package and use the usual using/import syntax. PkgTemplates.jl is great for this
If you would like to redefine consts though, I would definitely recommend checking out Pluto.jl

How to get the stack-trace in several implementations in Common Lisp?

I'm trying to make a library that I'm writing portable between the different Common Lisp implementations, I need a function like SBCL called sb-debug:list-backtrace which returns a list with stake-trace, but I did not find any of them, I'm looking for your documentation, and I'll continue, and I'll update with what I can get here, but I ask for help from anyone who has already used it and you know which function of the implementations below can return me a stack-trace list.
(defun get-stack-trace ()
#+sbcl (sb-debug:list-backtrace)
#+clisp (?)
#+cmu (?)
#+mcl (?)
#+excl (?)
#+abcl (?)
#+cll (?)
#+clasp (?)
#+ecl (?)
#+mezzano (?)
#+mkcl (?))
True that I do not need all this portability, but since it's just this function, I do not see why not try to port for as many implementations as you can get.

Strange autocompletion result in emacs + common lisp

I am using Emacs with SLIME for my development environment. When I type (write-to and then C-M-i I get the following autocompletions:
Click on a completion to select it.
In this buffer, type RET to select the completion near point.
Possible completions are:
write-to-sting
write-to-string
I know Common Lisp is powerful, but I guess write-to-sting is not in the ANSI standard. Google didn't offer a single hit for this function. Then I tried to find it in the SBCL code, but alas
(documentation 'write-to-sting 'function) returns nil so it doesn't have a documentation string.
When I try to execute the function (write-to-sting) I get The function COMMON-LISP-USER::WRITE-TO-STING is undefined.
Apropos also finds an unbound function:
(apropos 'write-to)
WRITE-TO
WRITE-TO-STING
WRITE-TO-STRING (fbound)
My question is: What is going on? Does anyone knows the story behind this function?
At some point during your interaction with the Lisp environment, you wrote write-to-sting and it was read by the Lisp reader. The symbol was interned in the COMMON-LISP-USER package. After all, maybe you intended to implement a function that sends an email to Sting, who knows?
Auto-completion works by filtering the currently known symbols in the environment.
You can safely (unintern 'write-to-sting) (or implement it).

How do you find where a macro is from in Julia?

How do you find out where a macro is from in Julia. I'm looking at someone's code and they're using an #debug("string") macro. There are no using statements in the particular file that would tell me where it's loaded from, so I assume it's loaded form somewhere else in the code.
I might guess at the debug module for Julia, but it doesn't seem like it's being used that way, it seems like it's being used more for logging, so I'm a bit unsure of how to track it down through the code.
A macro location can be obtained using the #which macro, this is a feature introduced in 0.5.
julia> #which #printf("%0.2f", 1/3)
#printf(args...) at printf.jl:1178
Similar to #which, you can use #edit to open the source file and #functionloc to get the function location programmatically.
You can go to help mode in the Julia repl by keyboard shortcut shfit+?
help?> #debug
No documentation found.
#debug is a macro.
# 1 method for macro "#debug":
#debug(msg...) at /home/guo/.julia/v0.5/Logging/src/logging_macros.jl:11
I guess the marco #debug is probably from the package Logging.jl.

Finding ? operator in AST

I'm doing some exercise in Rascal. When I try to determine the Cyclomatic complexity of a Java method getting methods from an AST. I would like to evaluate the ? operator.
As it is not determined by '/if(_, _, )', I tried to determine it using postfix(, _); (infix works fine finding || or &&)
Still no success.
Anybody who can unhide this secret to me?
Thanks in Advance
Use the force; the source for the AST definition is here https://github.com/cwi-swat/rascal/blob/master/src/org/rascalmpl/library/lang/java/m3/AST.rsc, also mentioning the conditional constructor Mark pointed out in his comment.
If you use iprintln on the AST for a small example, like iprintln(ast) or import util::ValueUI; and then text(ast) for an editor with the formatted AST it's easy to find out what ASTs look like.

Resources