什么是流利语法Fluent Syntax

,例如https://github.com/Medium/dynamite :

// getting an item from a tableclient.getItem(‘user-table’).setHashKey(‘userId’, ‘userA’).setRangeKey(‘column’, ‘@’).execute().then(function(data) {// data.result: the resulting object})

// example from // define the classvar Kitten = function() { this.name = ‘Garfield’; this.color = ‘brown’; this.gender = ‘male’;};Kitten.prototype.setName = function(name) { this.name = name; return this;};Kitten.prototype.setColor = function(color) { this.color = color; return this;};Kitten.prototype.setGender = function(gender) { this.gender = gender; return this;};Kitten.prototype.save = function() { console.log(‘saving ‘ + this.name + ‘, the ‘ +this.color + ‘ ‘ + this.gender + ‘ kitten…’ );// save to database here…return this;};// use itnew Kitten() .setName(‘Bob’) .setColor(‘black’) .setGender(‘male’) .save();

.

var mkChained = function(spec) { return function(init) {var s = spec[0] ? spec[0](init) : 0;var i = function(opt) {return spec[1] ? spec[1](s, opt) : s;}Object.keys(spec).forEach(function(name){// skip `entry` and `exit` functionsif(/^\d+$/.test(name))return;// transition ‘name : (s, opt) -> s’i[name] = function(opt) {s = spec[name](s, opt);return i;};});return i; }};var API = mkChained({ 0: function(opt) {return ;/* create initial state */}, then: function(s, opt) {return s; /* new state */}, whut: function(s, opt) {return s; /* new state */}, 1: function(s, opt) {return ;/* compute final value */}});// We create an instance of our newly crafted API,var call = API() // entry.whut()// transition.then()// transition.whut();// transition// And call itvar result0 = call() // exit , result1 = call() // exit

SQL

Author a = AUTHOR.as("a");create.selectFrom(a).where(exists(selectOne().from(BOOK).where(BOOK.STATUS.eq(BOOK_STATUS.SOLD_OUT)).and(BOOK.AUTHOR_ID.eq(a.ID))));

String[] datesStr = new String[] {"12-10-1492", "06-12-1978"};…List<Calendar> dates =Op.on(datesStr).toList().map(FnString.toCalendar("dd-MM-yyyy")).get();

时必须表示为表达式目录树在运行时,使它能够即创建类型安全连贯接口,而不是窗体中的对象:

Customer obj = … obj.property("name").eq("John")一种学法:method<Customer>(customer -> customer.getName() == "John")fluent interface包装程序的示例:

// Basic definition class GlutApp { private:int w_, h_, x_, y_, argc_, display_mode_;char **argv_;char *title_; public:GlutApp(int argc, char** argv) {argc_ = argc;argv_ = argv;}void setDisplayMode(int mode) {display_mode_ = mode;}int getDisplayMode() {return display_mode_;}void setWindowSize(int w, int h) {w_ = w;h_ = h;}void setWindowPosition(int x, int y) {x_ = x;y_ = y;}void setTitle(const char *title) {title_ = title;}void create(){;} }; // Basic usage int main(int argc, char **argv) {GlutApp app(argc, argv);app.setDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_ALPHA|GLUT_DEPTH); // Set framebuffer paramsapp.setWindowSize(500, 500); // Set window paramsapp.setWindowPosition(200, 200);app.setTitle("My OpenGL/GLUT App");app.create(); }// Fluent wrapper class FluentGlutApp : private GlutApp { public:FluentGlutApp(int argc, char **argv) : GlutApp(argc, argv) {} // Inherit parent constructorFluentGlutApp &withDoubleBuffer() {setDisplayMode(getDisplayMode() | GLUT_DOUBLE);return *this;}FluentGlutApp &withRGBA() {setDisplayMode(getDisplayMode() | GLUT_RGBA);return *this;}FluentGlutApp &withAlpha() {setDisplayMode(getDisplayMode() | GLUT_ALPHA);return *this;}FluentGlutApp &withDepth() {setDisplayMode(getDisplayMode() | GLUT_DEPTH);return *this;}FluentGlutApp &across(int w, int h) {setWindowSize(w, h);return *this;}FluentGlutApp &at(int x, int y) {setWindowPosition(x, y);return *this;}FluentGlutApp &named(const char *title) {setTitle(title);return *this;}// It doesn’t make sense to chain after create(), so don’t return *thisvoid create() {GlutApp::create();} }; // Fluent usage int main(int argc, char **argv) {FluentGlutApp(argc, argv).withDoubleBuffer().withRGBA().withAlpha().withDepth().at(200, 200).across(500, 500).named("My OpenGL/GLUT App").create(); }

有一种旅行,叫单车旅行。它没有奢侈准备,

什么是流利语法Fluent Syntax

相关文章:

你感兴趣的文章:

标签云: