Giter Club home page Giter Club logo

ppx_static's Introduction

ppx_static - Moving Computations out of Abstractions

Synopsis

This is a simple syntax extension to lift the computation of a value out of any surrounding abstractions. This allows expressing anonymous constants close to where they are used, without the penalty of recomputing them on each function invocation or module application. A secondary use is to add state deep within a function, which persists across invocations.

Examples

module Geometry = struct
  let to_degrees x = x *. [%static 360.0 /. (8.0 *. atan 1.0)]
end

is translated to

let __ppx_static_1234 = 360.0 /. (8.0 *. atan 1.0) in

module Geometry = struct
  let to_degrees x = x *. __ppx_static_1234
end

A typical use case, which motivated this ppx, is to compile regular expressions during module initialization, while expressing them at the point they are used:

let rec unwrap s =
  (match Re.exec_opt [%static Re.Pcre.regexp {|^\((.*)\)$|}] s with
   | Some g -> `Paren (unwrap (Re.Group.get g 1))
   | None ->
      (match Re.exec_opt [%static Re.Pcre.regexp {|^\$\{([^{}]+)\}$|}] s with
       | Some g -> `Variable (Re.Group.get g 1)
       | None -> `Bare s))

Limitations

No binding from the surrounding scope is in scope for static expressions. This limitation could be lifted for bindings which are determined during module initialization, though, if needed, it may be better to introduce open%static, let%static, etc. to make bindings available for the static scope.

ppx_static's People

Contributors

paurkedal avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  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.