Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion koans/ConversionKoans.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,35 @@
module ConversionKoans
end

#=
Conversion allows to apply methods defined for the converted type.
Here is the basic documentation for conversion and promotion in Julia:
-https://docs.julialang.org/en/v1/manual/conversion-and-promotion/
=#

struct MyTypeComplex
real::Float64
imaginary::Float64
end

struct YourType
end

function type_of(x)
end

function int_to_float64(int)
end

function define_YourType(int, float)
#=
define a type YourType above, it receives an Int64 and a Float64
=#
end

function convertion_vector_to_MyTypeComplex(vec)
#=
define the conversion, from vec[::Float64, ::Float64] to MyType defined above and use the function convert
=#
end

end
42 changes: 42 additions & 0 deletions solutions/ConversionKoans.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module ConversionKoans

#=
Links:
- https://docs.julialang.org/en/v1/manual/conversion-and-promotion/
=#

struct MyTypeComplex
real::Float64
imaginary::Float64
end

struct YourType
a::Int64
b::Float64
end

function type_of(x)
return typeof(x)
end

function int_to_float64(int)
return convert(Float64, int)
end

function define_YourType(int, float)
#=
define a type YouType above, it receive an Int64 and a Float64
=#
return YourType(int, float)
end

function convertion_vector_to_MyTypeComplex(vec)
#=
define the conversion, from vec[::Float64, ::Float64] to MyType defined above to use the function convert
=#

convert(::Type{MyTypeComplex}, x) = MyTypeComplex(x[1],x[2])
return convert(MyTypeComplex, vec)
end

end
15 changes: 12 additions & 3 deletions tests/solutions/tests_solutions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,18 @@ end
# @testset "Strings" begin
# end

# using ConversionKoans
# @testset "Type Conversions" begin
# end
using ConversionKoans
@testset "Conversion operations" begin
@test ConversionKoans.type_of(4) == Int64
@test ConversionKoans.type_of([3, 4]) == Array{Int64,1}
@test ConversionKoans.type_of(3.5 + im) == Complex{Float64}
@test ConversionKoans.int_to_float64(3) == 3.0
@test ConversionKoans.int_to_float64(20) == 20.0
@test typeof(ConversionKoans.convertion_vector_to_MyTypeComplex([5,6])) == ConversionKoans.MyTypeComplex
@test typeof(ConversionKoans.convertion_vector_to_MyTypeComplex([-10.1,5.4])) == ConversionKoans.MyTypeComplex
@test typeof(ConversionKoans.define_YourType(1, 2.2)) == ConversionKoans.YourType
@test typeof(ConversionKoans.define_YourType(1, -3.0)) == ConversionKoans.YourType
end

# using ArrayKoans
# @testset "Arrays" begin
Expand Down
11 changes: 10 additions & 1 deletion tests/tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ using StringKoans
end

using ConversionKoans
@testset "Type Conversions" begin
@testset "Conversion operations" begin
@test ConversionKoans.type_of(4) == Int64
@test ConversionKoans.type_of([3, 4]) == Array{Int64,1}
@test ConversionKoans.type_of(3.5 + im) == Complex{Float64}
@test ConversionKoans.int_to_float64(3) == 3.0
@test ConversionKoans.int_to_float64(20) == 20.0
@test typeof(ConversionKoans.convertion_vector_to_MyTypeComplex([5,6])) == ConversionKoans.MyTypeComplex
@test typeof(ConversionKoans.convertion_vector_to_MyTypeComplex([-10.1,5.4])) == ConversionKoans.MyTypeComplex
@test typeof(ConversionKoans.define_YourType(1, 2.2)) == ConversionKoans.YourType
@test typeof(ConversionKoans.define_YourType(1, -3.0)) == ConversionKoans.YourType
end

using ArrayKoans
Expand Down