Syntax error extra token "kMeans" after end of the expression - julia

I am using kmeans algorithms when i am using type to create a kmeans then it's giving me a error for extra token "kMeans" after end of the expression.
julia> type kMeans
kMeans
end
ERROR: syntax: extra token "kMeans" after end of expression

type is not a thing since Julia 1.0. You should use mutable struct, see https://docs.julialang.org/en/v1/manual/types/#Mutable-Composite-Types-1

Related

no method matching write(::IOStream, ::MyClass)

I am using a julia v0.6 code. But my julia version is v1.1. I don't know how to modify this piece of code
roadway_HOLO = open(io->read_dxf(io, Roadway, dist_threshold_lane_connect=2.0), joinpath(#__DIR__, "../data/ngsim_HOLO.dxf"), "r")
convert_curves_feet_to_meters!(roadway_HOLO)
open(io->write(io, roadway_HOLO), joinpath(#__DIR__, "../data/ngsim_HOLO.txt"), "w")
The error occurs at the last line of the code. Please help.
ERROR: LoadError: LoadError: MethodError: no method matching write(::IOStream, ::AutomotiveDrivingModels.Roadway)
Closest candidates are:
write(::IO, ::Any) at io.jl:498
write(::IO, ::Any, ::Any...) at io.jl:500
write(::IOStream, ::UInt8) at iostream.jl:378
...
Stacktrace:
I believe you are using this AutomotiveDrivingModels.jl. It seems to be that you need to pass MIME("text/plain") to the call to write in order to write your Roadway object to a text file.
open(io->write(io, MIME("text/plain"), roadway_HOLO), joinpath(#__DIR__, "../data/ngsim_HOLO.txt"), "w")
# or
open(io->write(io, MIME"text/plain"(), roadway_HOLO), joinpath(#__DIR__, "../data/ngsim_HOLO.txt"), "w")
Note that you can also use do syntax with methods whose first argument is a Function like open. The call above is equivalent to the following call below.
open(joinpath(#__DIR__, "../data/ngsim_HOLO.txt"), "w") do io
write(io, MIME("text/plain"), roadway_HOLO)
end
As a side note, methods(write) should normally show the write method(s) for Roadway type and also ?write should show the docstring for this write method. If you happen to see a similar error in the future, you might want to try these to find the correct method signature.
Use Serialization to store Julia objects in a file:
Consider some custom data structure and some object.
struct Some
x::String
y::Int
end
s = Some("test 123",12345)
The above structure can be serialized with the following command:
using Serialization
open("file.bin","w") do f
serialize(f,s)
end
Now let us test deserialization:
julia> open("file.bin") do f; println(deserialize(f)==s); end
true

List of keyword arguments in plot recipe definition

I have originally opened an issue in RecipesBase.jl, but I think this is just me not understanding the machinery of plot recipes. Why the code below fails when I try to pass in a list of keywords with the ... syntax?
using RecipesBase
#userplot FooPlot
#recipe function f(fp::FooPlot; a=1, b=2, kwargs...)
# SKIP
end
The error I am getting:
BoundsError: attempt to access 1-element Array{Any,1} at index [2]
I appreciate any help.

Parametric functors in Julia

Starting 0.6 it is possible to create parametric methods in Julia using where syntax. According to the release notes of 0.6 version, where syntax
can be used anywhere a type is accepted
Now consider the following contrived example:
function (rng::R)() where {R <: Range}
return first(rng)
end
which, when I attempt to compile it, gives the following error:
ERROR: function type in method definition is not a type
So my question is what is the proper way to create parametric functors in Julia 0.6+?
Ohkay, I get what you are trying to do basically. To understand functors here is a short example code.
julia> struct Student
name::String
end
julia> function (::Student)()
println("Callable of Student Type!")
end
julia> object = Student("JuliaLang")
Student("JuliaLang")
julia> object()
Callable of Student Type!
but when I try to create the parametric functors, it throws out the error similar to yours!
julia> function (::T)() where {T <: Student}
println("Callable of Student Type!")
end
ERROR: function type in method definition is not a type
This problem is actually still OPEN as a issue as #gnimuc rightly pointed out.
You mix up two things
parametric methods e.q. julia> same_type(x::T, y::T) where {T} = true
function-like objects e.g. julia> function (p::Polynomial)(x) ... end
To my knowledge there is no "parametric function-like objects"
However, the following code should be the same of what you intend.
Julia> function (rng::Range)()
return first(rng)
end
cannot add methods to an abstract type
The current docu does not mention any limitation of function-like objects to concrete type, but unfortunately Julia doesn't accept it anyway.

Julia what does a nameless value in the function header mean?

I often see something like the following in Julia:
convert(::Type{Point{Float64}}, ::Float64)
how does the (:: work? And what is the terminology for this?
Your answer can be found in the Julia documentation for defining conversions. To quote (with types switched to make it even more straightforward to read):
The type of the first argument of this method is a singleton
type, Type{Point{Float64}}, the only instance of which is
Point{Float64}. Thus, this method is only invoked when the first
argument is the type value Point{Float64}. Notice the syntax used
for the first argument: the argument name is omitted prior to the ::
symbol, and only the type is given. This is the syntax in Julia for a
function argument whose type is specified but whose value is never
used in the function body. In this example, since the type is a
singleton, there would never be any reason to use its value within the
body.
(Emphasis mine)
You will also encounter the foo(::SomeType) syntax in error messages, when trying to invoke a function with arguments of the wrong type (after all you can't show the argument names of a variant that does not exist). E.g:
julia> foo(x::Bool) = 3
foo (generic function with 1 method)
julia> foo(5)
ERROR: `foo` has no method matching foo(::Int64)

Vector{AbstractString} function parameter won't accept Vector{String} input in julia

The following code in Julia:
function foo(a::Vector{AbstractString})
end
foo(["a"])
gives the following error:
ERROR: MethodError: no method matching foo(::Array{String,1})
Closest candidates are:
foo(::Array{AbstractString,1}) at REPL[77]:2
Even though the following code runs, as expected:
function foo(a::Vector{String})
end
foo(["a"])
And further, AbstractString generally matches String as in:
function foo(::AbstractString)
end
foo("a")
How can I call a function with a Vector{AbstractString} parameter if I have String elements?
You need to write the function signature like this:
function foo{S<:AbstractString}(a::Vector{S})
# do stuff
end
On Julia 0.6 and newer, it's also possible to write instead
function foo(a::Vector{<:AbstractString})
# do stuff
end
This is a consequence of parametric type invariance in Julia. See the chapter on types in the manual for more details.

Resources