diff --git a/koans/ConversionKoans.jl b/koans/ConversionKoans.jl index 21f28f0..5eab984 100644 --- a/koans/ConversionKoans.jl +++ b/koans/ConversionKoans.jl @@ -1,2 +1,55 @@ module ConversionKoans -end \ No newline at end of file + +#= + 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) + #= + The type of a variable is one of its most important features, + so we want to have this information at hand. + Write a function returning the type of x. + Ex: x = 3.6 return Float64. + =# +end + +function int_to_float64(int) + #= + Converting from one type to another is a common operation, + for example, converting a variable of type int to type float. + Write a function converting the variable "int" to Float64. + Ex: int = 4; return 4.0 + =# +end + +function define_YourType(int, float) + #= + Sometimes defining a custom type allow you to create representations + with some useful properties, as defining a complex number type as MyTypeComplex, + defined above. + Define a type YourType, it receives an Int64 and a Float64 + =# +end + +function convertion_vector_to_MyTypeComplex(vec) + #= + The conversion function allow you to define the conversion + behaviour between types. + Define the conversion, from vec[::Float64, ::Float64] to + MyType defined above to use the function convert a return the + conversion of vec. + Ex: convert(MyTypeComplex, vec) + =# +end + +end diff --git a/solutions/ConversionKoans.jl b/solutions/ConversionKoans.jl new file mode 100644 index 0000000..948de14 --- /dev/null +++ b/solutions/ConversionKoans.jl @@ -0,0 +1,71 @@ +module ConversionKoans + + +#= + 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 + a::Int64 + b::Float64 + end + +function type_of(x) + #= + The type of a variable is one of its most important features, + so we want to have this information at hand. + Write a function returning the type of x. + Here is the link to the documentation: + - http://www.jlhub.com/julia/manual/en/function/typeof + Ex: x = 3.6; return Float64. + =# + return typeof(x) +end + +function int_to_float64(int) + #= + Converting from one type to another is a common operation, + for example, converting a variable of type int to type float. + Write a function converting the variable "int" to Float64. + Here is the link to the function used: + - http://www.jlhub.com/julia/manual/en/function/convert + Ex: int = 4; return 4.0 + =# + return convert(Float64, int) +end + +function define_YourType(int, float) + #= + Sometimes defining a custom type allow you to create representations + with some useful properties, as defining a complex number type as MyTypeComplex, + defined above. + Define a type YourType, it receives an Int64 and a Float64 + Here is some useful information regarding defining types: + - https://docs.julialang.org/en/v1/manual/types/#Composite-Types + =# + return YourType(int, float) +end + +function convertion_vector_to_MyTypeComplex(vec) + #= + The conversion function allow you to define the conversion + behaviour between types. + Define the conversion, from vec[::Float64, ::Float64] to + MyType defined above to use the function convert + Here is the link to some useful information about custom conversion + functions in Julia: + - https://docs.julialang.org/en/v1/manual/conversion-and-promotion/#Defining-New-Conversions + Ex: convert(MyTypeComplex, vec) + =# + convert(::Type{MyTypeComplex}, x) = MyTypeComplex(x[1],x[2]) + return convert(MyTypeComplex, vec) +end + +end diff --git a/tests/solutions/tests_solutions.jl b/tests/solutions/tests_solutions.jl index c5e3001..cf5f4d8 100644 --- a/tests/solutions/tests_solutions.jl +++ b/tests/solutions/tests_solutions.jl @@ -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 diff --git a/tests/tests.jl b/tests/tests.jl index 7425083..215eedb 100644 --- a/tests/tests.jl +++ b/tests/tests.jl @@ -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