Skip to content

dashkite/generic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DashKite Generic

Generic functions

Hippocratic License HL3-CORE

Examples

Fibonacci Function

import { memoize } from "@dashkite/joy/function"

fib = Generic.make "fib", "Fibonacci sequence"
  .define [ gte 0 ], memoize ( n ) -> ( fib n - 1 ) + ( fib n - 2 )
  .define [ eq 1 ], -> 1
  .define [ eq 2 ], -> 1

assert.equal 5, fib 5
assert.throws -> fib 0

Polymorphism

import { has } from "@dashkite/joy/object"

length = Generic.make "size", "Return the size of a value"
  # based on interface
  .define [ has "length" ], ( value ) -> value.length
  # based on type
  .define [ Object ], ( object ) -> ( Object.keys object ).length
  .define [ Set ], ( set ) -> set.values().length
  
assert.equal 5, size "hello"
assert.equal 3, size [ 1..3 ]
assert.equal 2, size x: 0, y: 0
assert.equal 1, size new Set [ true ]

Method Dispatch

class Adder
  constructor: ( @total = 0 ) ->
  add: ( f = Generic.make "Adder::add" )
    .define [ Number ], ( operand ) -> 
      @total += operand
      @
  
assert.equal 12, 
  ( new Adder )
    .add 5
    .add 7
    .total

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published