method-chaining

Combining promises and chaining

蹲街弑〆低调 提交于 2019-12-04 05:43:57
问题 Is there a good design pattern for utilizing promises, while still supporting chaining? For example, let's say that we have something like: function Foobar() { this.foo = function() { console.log('foo'); return this; }; this.bar = function () { console.log('bar'); return this; }; } var foobar = new Foobar(); foobar.foo().bar(); If instead we change the methods to use promises, e.g. function Foobar() { this.foo = function() { var self = this; return new Promise(function (resolve, reject) {

Method chaining with R

泄露秘密 提交于 2019-12-04 02:12:44
Is it possible to chain functions in R? Sample data: m <- matrix(c(1:10, 11:20), nrow = 10, ncol = 2) For example, I would like to replace the following statements below: step1 <- mean(m) step2 <- sum(step1) res <- step2 Or, res <- sum(mean(m)) With something like this : res <- m@mean()@sum() In some cases, that would clarify my code considerably. EDIT1 This is a dummy example. I randomly picked 'sum' and 'mean'. Ben has given a first piece of answer using %@% however, it prevents from using extra arguments within functions : m %@% function1(arg1, arg2) %@% function2(arg1, arg2) How can I work

What determines the execution order of methods in jQuery chains?

柔情痞子 提交于 2019-12-04 02:04:27
HTML Code <div id="foo"> <h1>foo</h1> <p>Pellentesque habitant morbi tristique.</p> </div> <div id="bar"> <h1>bar</h1> </div> jQuery Code $('#bar').click(function () { $('#foo p').hide('slow').appendTo('#bar').show('slow'); }) Expected Result When #bar is clicked hide the p element in #foo append p to #bar show p which is now a child of #bar Actual Result append p to #bar hide the p element in #foo show p which is now a child of #bar Questions What determines the execution order of methods in jQuery chains? How can I ensure that each event completes before the next starts? If you want to wait

Chain functions in different way

心已入冬 提交于 2019-12-03 14:15:32
Scala functions has following methods for chaining: fn1.andThen(fn2) fn1.compose(fn2) But how can be written this case: I have function cleanUp() which has to be called always as last step. And I have a bunch of other functions, like that: class Helper { private[this] val umsHelper = new UmsHelper() private[this] val user = umsHelper.createUser() def cleanUp = ... // delete user/ and other entities def prepareModel(model: TestModel) = { // create model on behalf of the user } def commitModel() = { // commit model on behalf of the user } } And some external code can use code something like this

In java, can one create a fluent extensible class hierarchy with methods that can be invoked in any order?

我与影子孤独终老i 提交于 2019-12-03 14:04:42
问题 Can one create an extensible class hierarchy in java whose methods are fluent and can be invoked in any order? (YES! see answer below), even for existing classes when you don't have access to the source, provided the methods are fluant! I'm retrofitting an existing hierarchy and hope to use a factory or at least a generic constructor and (eventually) immutable builder patterns (JB P.14). The methods that set fields return void - it would be better for them to return a generic T instead - that

What is the accepted/recommended syntax for Scala code with lots of method-chaining?

烈酒焚心 提交于 2019-12-03 10:57:50
问题 In Scala I tend to favour writing large chained expressions over many smaller expressions with val assignments. At my company we've sort of evolved a style for this type of code. Here's a totally contrived example (idea is to show an expression with lots of chained calls): import scala.util.Random val table = (1 to 10) map { (Random.nextInt(100), _) } toMap def foo: List[Int] = (1 to 100) .view .map { _ + 3 } .filter { _ > 10 } .flatMap { table.get } .take(3) .toList Daniel Spiewak's Scala

How can I properly chain custom methods in Ruby?

家住魔仙堡 提交于 2019-12-03 07:17:54
I am trying to do a chaining method for the following two methods. After running this code, I kept getting the following output: #<SimpleMath:0x007fc85898ab70>% My question is: what is the proper way of chaining methods in Ruby ? Here is my codes: class SimpleMath def add(a,b=0) a + b return self end def subtract(a,b=0) a - b return self end end newNumber = SimpleMath.new() print newNumber.add(2,3).add(2) Are you trying to do something like this? class SimpleMath def initialize @result = 0 end #1 add function def add(val) @result += val self end #2 Subtract function def subtract(val) @result -

In java, can one create a fluent extensible class hierarchy with methods that can be invoked in any order?

☆樱花仙子☆ 提交于 2019-12-03 05:04:28
Can one create an extensible class hierarchy in java whose methods are fluent and can be invoked in any order? (YES! see answer below), even for existing classes when you don't have access to the source, provided the methods are fluant! I'm retrofitting an existing hierarchy and hope to use a factory or at least a generic constructor and (eventually) immutable builder patterns (JB P.14). The methods that set fields return void - it would be better for them to return a generic T instead - that way we will gain the ability to do method chaining (they all call super now). Goals: 1. Avoid having

What is the accepted/recommended syntax for Scala code with lots of method-chaining?

纵饮孤独 提交于 2019-12-03 02:32:17
In Scala I tend to favour writing large chained expressions over many smaller expressions with val assignments. At my company we've sort of evolved a style for this type of code. Here's a totally contrived example (idea is to show an expression with lots of chained calls): import scala.util.Random val table = (1 to 10) map { (Random.nextInt(100), _) } toMap def foo: List[Int] = (1 to 100) .view .map { _ + 3 } .filter { _ > 10 } .flatMap { table.get } .take(3) .toList Daniel Spiewak's Scala Style Guide (pdf), which I generally like, suggests the leading dot notation in the chained method calls

Can a C# method chain be “too long”?

◇◆丶佛笑我妖孽 提交于 2019-12-03 01:23:53
Not in terms of readability, naturally, since you can always arrange the separate methods into separate lines. Rather, is it dangerous, for any reason, to chain an excessively large number of methods together? I use method chaining primarily to save space on declaring individual one-use variables, and traditionally using return methods instead of methods that modify the caller. Except for string methods, those I kinda chain mercilessly. In any case, I worry sometimes about the impact of using exceptionally long method chains all in one line. Let's say I need to update the value of one item