Giter VIP home page Giter VIP logo

canonicaltraits.jl-a603d957-0e48-4f86-8fbd-0b7bc66df689's Introduction

CanonicalTraits

Stable Dev Build Status Codecov

Trait Definition

@trait Monoid{A} begin
    mempty :: Type{A} => A
    # a method with two arguments
    (⊕)    :: [A, A] => A
end

Note, the form @trait A{B, C, D <: Number, E <: AbstractString} is supported as well.

Instance Definition

@implement Monoid{Int} begin
  mempty(::Type{Int}) = 0
  a ⊕ b = a + b
end


32 # 5
mempty(Int) # 0

More

julia> @trait Monoid{A} begin
                  mempty :: Type{A} => A
                  # a method with two arguments
                  (⊕)    :: [A, A] => A
              end


julia> @implement Monoid{Num} where Num <: Number begin
           mempty(::Type{Num}) = zero(Num)
           (a :: Num) ⊕ (b :: Num) = a + b
       end

julia> using BenchmarkTools

julia> 32
5

julia> 3.02
ERROR: MethodError: no method matching ⊕(::Float64, ::Int64)
Closest candidates are:
  ⊕(::A, ::A) where A

julia> """"
ERROR: Not implemented trait Monoid for (String).

julia> 3.02.0
5.0

julia> mempty(Int)
0

julia> mempty(Float32)
0.0f0

julia> @btime 100200
  0.018 ns (0 allocations: 0 bytes)
300

julia> @btime 100 + 200
  0.018 ns (0 allocations: 0 bytes)
300

Limitations

* Due to the limitations of dynamic language, the type parameters occurred in trait signature should occur in the argument of each trait methods. Also, cannot define constants/singletons for traits because it's a technique for static typing.

* For Haskell users: MultiParamTypeClasses is supported. FunctionalDependencies is supported as well but need an explicit inference rule, check test/runtests.jl and search Dot for more details.

@trait Dot{F, V} where {F = vect_infer_helper(V)} begin
    dot :: [V, V] => F
    gram_schmidt :: [V, Set{V}] => V
end

Cannot list out all limitations here, if any problem, please open an issue or e-mail me.

canonicaltraits.jl-a603d957-0e48-4f86-8fbd-0b7bc66df689's People

Contributors

thautwarm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.