function commence(args::Vector{String})
What I would like to know is, what does this line mean? Particularly, what does the argument of commence, "(args::Vector{String})" mean?
The line indicates the definition of a function named commence which takes a single argument args. The ::Vector{String} bit specifies that only vectors of strings, i.e. objects of type Vector{String}, will be accepted by the function. See the following demonstration:
julia> function commence(args::Vector{String})
#show args
nothing
end
commence (generic function with 1 method)
julia> commence([1,2,3])
ERROR: MethodError: no method matching commence(::Array{Int64,1})
Closest candidates are:
commence(::Array{String,1}) at REPL[1]:2
Stacktrace:
[1] top-level scope at REPL[2]:1
julia> commence("asd","test")
ERROR: MethodError: no method matching commence(::String, ::String)
Stacktrace:
[1] top-level scope at REPL[3]:1
julia> commence(["asd","test"]) # works, since typeof(args) == Vector{String}
args = ["asd", "test"]
I recommend you read through the manual, in particular this part, to learn more about functions in Julia.
:: means enforce the type similar to typeassert().
Also see https://docs.julialang.org/en/v1/base/punctuation/ [Julia Puntuation][1]
Related
Recently I have been trying to write a simple bot in Hy with Discord.py. In Discord.py we can write a command like this to turn the last argument into a full string that includes spaces:
#commands.command(description="", help="")
async def say(self, ctx, level, *, remains):
...
But if I write this in Hy as:
##((commands.command :description "" :help "")
(defn/a say [self ctx level * remains]
...))
It will complain about missing required argument "text." What's even weirder is that the sample code in the defn part on Hy's official website:
(defn compare [a b * keyfn [reverse False]]
(setv result (keyfn a b))
(if (not reverse)
result
(- result)))
doesn't even work under hy --spy. Did I use it wrong or there's a correct way to handle this?
(defn compare…) works for me. Sounds like the version of Hy you're running doesn't match the version of the documentation you're reading.
I'm getting the following error with the code: LogisticRegression.predict_proba(X)
TypeError Traceback (most recent call last)
in
----> 1 LogisticRegression.predict_proba(X)
TypeError: predict_proba() missing 1 required positional argument: 'X'
I don't understand why, I read the documentation and don't know what argument I am missing.
You must provide input data to the function, vector X you want to run prediction on.
I'm relatively new to Julia language, and I 've been recently trying to process some files in parallel manner. My code looks something like>
for ln in eachline (somefile)
...
proces this line
for ln2 in eachline (someotherfile)
..
..
process ln and ln2
..
..
I've been trying to speed things up a bit with #everywhere and #parallel functions, but it doesn't seem to work for eachline function.
Am I missing something?
Thanks for help.
From #parallel macro we already know that:
#parallel [reducer] for var = range
body
end
The specified range is partitioned and locally executed across all workers.
To do the above job in minimum time, #parallel gets length(range) then partitions it between nworkers().
for more details you can:
. see macro output -> macroexpand(:(#parallel for i in 1:5 i end))
or:
. check macro source -> milti.jl
EachLine is one of Julia iterables, it implements all mandatory methods of iterable interface, but length() is not one of those. (check this discussion), so EachLine is not a range and #parallel fails to do it's task because lack of length() function.
But there are at list two solutions to parallelize the process part:
use lis=readlines() to collect a range of lines, the #parallel for li in lis
use pmap()
Julia’s pmap() (page 483) is designed for the case where each function
call does a large amount of work. In contrast, #parallel for can
handle situations where each iteration is tiny, perhaps merely summing
two numbers.
a sample code:
len=function(s::AbstractString)
string(length(s)) * " " * string(myid());
end
function test()
open("eula.1028.txt") do io
pmap(len,eachline(io))
end
end
I'm trying to send midi messages from some Julia code I've written, but I'm having trouble with the midiOutOpen function. I'm following this tutorial here, but the output I'm getting from the function doesn't make sense.
This is my Julia code:
const CALLBACK_NULL = uint32(0x00000001)
function openoutputdevice(id::Uint32)
handle = uint32(0)
err = ccall((:midiOutOpen, :Winmm), stdcall,
Uint32,
(Ptr{Uint32}, Uint32, Ptr{Uint32}, Ptr{Uint32}, Uint32),
&handle, id, C_NULL, C_NULL, CALLBACK_NULL)
println(hex(err))
handle
end
The handle is always 0, and the error that's being returned is "10". I've grepped through the Windows header files, and this doesn't seem to match up with any of the errors that can be expected from the function (see here), so I'm more inclined to think that I'm mapping the wrong Julia data types in the ccall. It's been a long time since I've done anything C-related, so I'm hoping there's something obviously wrong with this. The only odd thing I've seen is that CALLBACK_NULL is defined in mmsyscom.h as 0x000000001 - a 9 digit hex number, even though the function doc specifies a DWORD for the final parameter to midiOutOpen.
Any ideas?
The error is MMSYSERR_INVALFLAG because CALLBACK_NULL is defined as:
#define CALLBACK_NULL 0x00000000l
That is a lowercase-letter-"L" at the end, not the number 1 (one). The call succeeds when this value is corrected.
I am trying to write an interface between RSPEC (ruby flavoured BDD) and a Windows application. The application itself is written in an obscure language, but it has a C API to provide access. I've gone with Ruby/DL but am having difficulties getting even the most basic call to a DLL method to work. Here is what I have so far, in a file called gt4r.rb:
require 'dl/import'
module Gt4r
extend DL::Importable
dlload 'c:\\gtdev\\r321\\bin\\gtvapi'
# GTD initialization/termination functions
extern 'int GTD_init(char *[], char *, char *)'
extern 'int GTD_initialize(char *, char *, char *)'
extern 'int GTD_done(void)'
extern 'int GTD_get_error_message(int, char **)'
end
My reading so far suggests that this is all I need to get going, so I wrote up a RSPEC example:
require 'gt4r'
##test_environment = "INCLUDE=C:\\graphtalk\\env\\aiadev\\config\\aiadev.ini"
##normal_user = "BMCHARGUE"
describe Gt4r do
it 'initializes' do
rv = Gt4r.gTD_initialize ##normal_user, ##normal_user, ##test_environment
rv.should == 0
end
end
And when run...
C:\code\GraphTalk>spec -fs -rgt4r gt4r_spec.rb
Gt4r
- initializes (FAILED - 1)
1)
'Gt4r initializes' FAILED
expected: 0,
got: 13 (using ==)
./gt4r_spec.rb:9:
Finished in 0.031 seconds
1 example, 1 failure
The return value (13) is an actual return code, meaning an error, but when I try to add the gTD_get_error_message call to my RSPEC, I can't get the parameters to work.
Am I heading in the right direction and can anyone point to the next thing I can try?
Thanks,
Brett
A follow up to this question, showing the part that fails when I try to get the error message from my target library:
require 'gt4r'
##test_environment = "INCLUDE=C:\\graphtalk\\env\\aiadev\\config\\aiadev.ini"
##normal_user = "BMCHARGUE"
describe Gt4r do
it 'initializes' do
rv = Gt4r.gTD_initialize ##normal_user, ##normal_user, ##test_environment
Gt4r.gTD_get_error_message rv, #msg
#msg.should == ""
rv.should == 0
end
end
I expect the error message to be returned in #msg, but when run I get the following:
Gt4r
(eval):5: [BUG] Segmentation fault
ruby 1.8.6 (2008-08-11) [i386-mswin32]
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
And this if I use a symbol (:msg) instead:
C:\code\GraphTalk\gt4r_dl>spec -fs -rgt4r gt4r_spec.rb
Gt4r
- initializes (ERROR - 1)
1)
NoMethodError in 'Gt4r initializes'
undefined method `to_ptr' for :msg:Symbol
(eval):5:in `call'
(eval):5:in `gTD_get_error_message'
./gt4r_spec.rb:9:
Finished in 0.046 seconds
1 example, 1 failure
Clearly I am missing something about passing parameters between ruby and C, but what?
The general consensus is you want to avoid DL as much as possible. The (english) documentation is quite sketchy and the interface is difficult to use for anything but trivial examples.
Ruby native C interface is MUCH easier to program against. Or you could use FFI, which fills a similiar niche to DL, originally comes from the rubinius project and has recently been ported to "normal" ruby. It has a nicer interface and is much less painful to use:
http://blog.headius.com/2008/10/ffi-for-ruby-now-available.html
The return value (13) is an actual
return code, meaning an error, but
when I try to add the
gTD_get_error_message call to my
RSPEC, I can't get the parameters to
work.
It might help posting the error instead of the code that worked :)
Basically, once you start having to deal with pointers as in (int, char **), things get ugly.
You need to allocate the data pointer for msg to be written to, since otherise C will have nowhere to write the error messages. Use DL.mallo.