Skip to content

Instantly share code, notes, and snippets.

@artpej
Last active August 29, 2015 14:05
Show Gist options
  • Save artpej/f588c9c58d678bf25ddb to your computer and use it in GitHub Desktop.
Save artpej/f588c9c58d678bf25ddb to your computer and use it in GitHub Desktop.
module typetestdeco

function  zip = |a,b, zipper| {
  require(a: length() == b: length(), "Arrays must have the same size")
  for ( var i = 0, i < a: length(), i = i + 1) {
        zipper(a :get(i), b :get(i))
  }
}

function checkparams = |checkers...| {
  let _deco_ = |fun| {
    let _wrapped_ = |args...|{
      zip( checkers, args, |f,v| -> require(f(v), v+" is not of required type!") )
      return fun: invokeWithArguments(args)
    }
    return _wrapped_
  }
  return _deco_
}

# Type checking function factory
function checktype = |t| -> |v| -> v oftype t

@checkparams(checktype(Integer.class), checktype(String.class), checktype(Double.class))
function foo = |a, b, c|{
    println(":D")
}

function main = |args| {
  foo(42, "foo", 13.37)
  foo(13.37, 42, "foo")
}
@yloiseau
Copy link

let isInteger = checktype(Integer.class)

@checkparams(isInteger)
function foo = | a | {...}

:)

@jponge
Copy link

jponge commented Aug 20, 2014

👍 @yloiseau

@yloiseau
Copy link

@jponge :-P
Remind me... Is there a fold or reduce function available (native or in lib)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment