Julia request user input from script
The easiest thing to do is readline(stdin). Is that what you’re looking for?
The easiest thing to do is readline(stdin). Is that what you’re looking for?
Julia has the isfile() function to test for a regular file: julia> isfile(“foo.txt”) false shell> touch foo.txt julia> isfile(“foo.txt”) true As the documentation: Returns true if path (the parameter) is a regular file, false otherwise.
In Julia, the equivalent to list.files() is readdir([path]) There is no built-in directory search that I know of, but it is a one-liner: searchdir(path,key) = filter(x->contains(x,key), readdir(path)) UPDATE: Since at least Julia v0.7, contains() has been deprecated for occursin(substring, string). So the above filter would now be: searchdir(path,key) = filter(x->occursin(key,x), readdir(path))
Iterators.flatten(x) creates a generator that iterates over each element of x. It can handle some of the cases you describe, eg julia> collect(Iterators.flatten([(1,2,3),[4,5],6])) 6-element Array{Any,1}: 1 2 3 4 5 6 If you have arrays of arrays of arrays and tuples, you should probably reconsider your data structure because it doesn’t sound type stable. However, … Read more
You should use: b = string(a) or b = repr(a) string function can be used to create string from any value using print and repr uses showall. In case of Int64 this is equivalent. And actually this is probably the reason why convert does not work – as there are many ways to convert integer … Read more
For v0.7+ Use fieldnames(x), where x is a DataType. For example, use fieldnames(Date), instead of fieldnames(today()), or else use fieldnames(typeof(today())). This returns Vector{Symbol} listing the field names in order. If a field name is myfield, then to retrieve the values in that field use either getfield(x, :myfield), or the shortcut syntax x.myfield. Another useful and … Read more
Assuming your Python and Julia are installed you need to take the following steps. Run Julia and install PyCall using Pkg pkg”add PyCall” Put your code into a Julia package using Pkg Pkg.generate(“MyPackage”) In the folder src you will find MyPackage.jl, edit it to look like this: module MyPackage f(x,y) = 2x.+y export f end … Read more
Reading a file into memory all at once as an array of lines is just a call to the readlines function: julia> words = readlines(“/usr/share/dict/words”) 235886-element Array{String,1}: “A” “a” “aa” ⋮ “zythum” “Zyzomys” “Zyzzogeton” By default this discards the newlines but if you want to keep them, you can pass the keyword argument keep=true: julia> … Read more
@ChrisRackauckas’s answer is accurate as far as it goes – i.e. for mutable objects. There’s a bit more to the issue than that, however, so I’ll elaborate a bit here. The === operator (an alias for the is function) implements Henry Baker’s EGAL predicate [1, 2]: x === y is true when two objects are programmatically … Read more
According to the documentation under ?@async, “@async wraps an expression in a Task.” What this means is that for whatever falls within its scope, Julia will start this task running but then proceed to whatever comes next in the script without waiting for the task to complete. Thus, for instance, without the macro you will … Read more