Giter Site home page Giter Site logo

keddelzz / hidden-args Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 0.0 37 KB

Hide your accumulators from the public API

License: MIT License

Scala 100.00%
scala hidden-arguments tail-recursive-functions functional-programming macros metaprogramming macro-paradise annotations

hidden-args's People

Contributors

keddelzz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

hidden-args's Issues

Change organization-name

Change it from com.github.keddelzz.hidden-args to com.github.keddelzz.

This change will also affect future releases of hidden-args. (Because we should also change the organization-name of the project on jcenter to be consistent with the changes in repo.)

Make name of nested function configurable

@hiddenargs("loop")
def sum(xs: List[Int], @hidden acc: Int = 0): Int = 
  xs match {
    case Nil => acc
    case hd :: tl => sum(tl, acc + hd)
  }

The annotation @hiddenargs could get an optional parameter, which represents the name of the inner function, thus the code snipped above would expand to

def sum(xs: List[Int], @hidden acc: Int = 0): Int = {
  def loop(xs: List[Int], acc: Int): Int = 
    xs match {
      case Nil => acc
      case hd :: tl => sum(tl, acc + hd)
    }

  loop(xs, 0)
}

.. instead of using the (synthetic) name sum_impl.

Move 'tailrec' annotations to inner function

If a function is defined to be tailrec, the inner function should receive the tailrec annotation instead of the outer one, because after macro expansion the wrapper function isn't tailrec anymore.

For example:

@hiddenarg
@tailrec
def factorial(n: Int, @hidden acc: BigInt = BigInt(1)): BigInt = 
  if (n <= 0) acc
  else factorial(n - 1, BigInt(n) * acc)

Support for multiple parameter lists and implicit arguments

I want to be able to write something like:

@hiddenargs
  def sum2[T: Numeric](xs: List[T], @hidden acc: T = implicitly[Numeric[T]].zero): T =
    xs match {
      case Nil      => acc
      case hd :: tl => sum(tl, implicitly[Numeric[T]].plus(acc, hd))
    }

Or maybe use the implicit parameter explicitly. For example:

@hiddenargs
  def sum[T](xs: List[T], @hidden acc: T = ev.zero)(implicit ev: Numeric[T]): T =
    xs match {
      case Nil      => acc
      case hd :: tl => sum(tl, ev.plus(hd, acc))
    }

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.