Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion koans #22

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
55 changes: 54 additions & 1 deletion koans/ConversionKoans.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,55 @@
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)
#=
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
71 changes: 71 additions & 0 deletions solutions/ConversionKoans.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module ConversionKoans


#=
igaray marked this conversation as resolved.
Show resolved Hide resolved
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
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