ben

OMÜ , Bilgisayar Mühendisliği, 13'

20 Eylül 2016 Salı

julia beginner 2

immutable

kendi değişken sınıfımızı yazmamızı sağlar

julia> immutabe starfish
               num_of_legs::Int
          end
julia> s=starfish(6)
starfish(6)
julia> names(s)
:num_of_legs
julia> s.num_of_legs
6

Type Tanımlamada Miraslama

julia> abstract Cat
julia> type Lion<:Cat
          roar::String
           color

         end
julia> type Tiger<:Cat
          has_stripes::Bool
          roar::String
       end
julia> subtypes(Cat)
 2-element Array{Any,1}:
Lion
Tiger

Fonksiyonlarda Type

julia> type Koala
            sound::String
           end
julia> function GurultuYap(k::Koala)
           k.sound
           end
GurultuYap (generic function with 1 method)

julia> function GurultuYap(l::Lion)
         l.roar
       end
GurultuYap (generic function with 2 methods)

julia> function GurultuYap(t::Tiger)
         t.roar
       end
GurultuYap (generic function with 3 methods)

julia> l=Lion("RAWRRR","green")
julia> t=Tiger(true, "Ben Tigerım")
julia> k=Koala("hımmmmm")










@which

fonksiyonun açıklamasını verir

julia> function fight(k::Koala, c::Cat)
       println("coala ve cat")
       end
fight (generic function with 1 method)

julia> k=Koala("hımmmmm" )
julia> c=HouseCat("aaa")
julia> @which fight(k,c)
fight(k::Koala, c::Cat) at none:2


@elapsed, @time,@show










Makro

julia> macro foo(e)
       quote
         println("hello")
         $e
         println("world")
       end
       end






Sleep
julia>sleep(1)
julia> median([@elapsed sleep(1) for x=1:10])
1.0020625845

julia> macro bench(f)
       quote
       median([@elapsed $f for x=1:10])
       end
       end
julia> @bench sleep(0.5)
0.501552907


makro içinde fonksiyon çağırma

julia> macro bench2(args...)
        bench2(args...)
       end

julia> function bench2(e)
       quote
       median([@elapsed sleep(1) for x=1:10])
       end
       end
bench2 (generic function with 1 method)

julia> @bench2 sleep(0.1)
1.002052691

Hiç yorum yok: