method-chaining

What is the correct way to chain methods in .Net

北慕城南 提交于 2019-12-06 03:43:46
In .Net, you can chain methods returning a value or by using a void. Is one of them the "right way"? So you could say 1) Foo myFoo = new Foo(); myfoo.Bars = myBars.DoSomethingCool(x) .DoSomethingElse(y) .AndSomethingElse(z); public static IList<IBar> DoSomethingCool(this IList<IBar> source, object x) { IList<IBar> result = //some fn(source) return result; } In this case, all 3 extension methods need to return IList (the type for myFoo.Bars) or it could also be written as 2) myBars.DoSomethingCool(x) .DoSomethingElse(y) .AndSomethingElse(z); public static void DoSomethingCool(this IList<IBar>

Check if call is method chaining

杀马特。学长 韩版系。学妹 提交于 2019-12-06 01:58:14
Is it possible to know if a method call is from a method chaining? For example, I have the bellow class : class Test{ protected $string = '123'; public function a($string){ $this->string .= $string; if(method chain){ return $this; }else{ return $this->string; } } public function b($string){ $this->string .= $string; if(method chain){ return $this; }else{ return $this->string; } } } Result: $test = new Test(); echo $test->a('000'); // 123000 echo $test->a('000')->b('www'); // 123000www UPDATE I ended up creating an exec() method to tell no more methods were going to be called. public function

Ruby Challenge - Method chaining and Lazy Evaluation

喜你入骨 提交于 2019-12-06 01:35:26
问题 After reading the article http://jeffkreeftmeijer.com/2011/method-chaining-and-lazy-evaluation-in-ruby/, I started looking for a better solution for method chaining and lazy evaluation. I think I've encapsulated the core problem with the five specs below; can anyone get them all passing? Anything goes: subclassing, delegation, meta-programming, but discouraged for the latter. It would be favourable to keep dependencies to a minimum: require 'rspec' class Foo # Epic code here end describe Foo

What determines the execution order of methods in jQuery chains?

你。 提交于 2019-12-05 17:15:41
问题 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

Method chaining with R

你。 提交于 2019-12-05 17:02: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

How to attach an event to onSubmit event of form with chaining earlier attached methods as well?

萝らか妹 提交于 2019-12-05 11:20:22
Actaully my application is having hundreds of pages. Now i have to attach an event 'disablePage' on onSubmit of form. I don't want to go to each and every page and write: <form name="frmname" onSubmit="disablePage();"> What i am doing right now is:- excerpt from common.js file; [ included in all pages ] /* we have to attach 'attachFormSubmit' method on onLoad event, otherwise forms[0] will always be null. If there is any alternative, then please suggest one */ if (window.addEventListener){ window.addEventListener('load', attachFormSubmit, false); } else if (window.attachEvent){ window

How to chain call functions by using a string containing that chain in PHP

你。 提交于 2019-12-05 07:16:42
I have a chain call like so: $object->getUser()->getName(); I know that I can use a string to call a function on an object: $functionName = 'getUser'; $object->$functionName() or call_user_func(array($object, functionName)) I was wondering if it was possible to do the same for a chain call? I tried to do: $functionName = 'getUser()->getName'; $object->functionName(); But I get an error Method name must be a string I guess this is because the () and -> cannot be interpreted since they are part of a string? Is there any way I can achieve this without having to do: $function1 = getUser;

Java method call chaining in static context

血红的双手。 提交于 2019-12-05 05:42:29
In StringBuilder class I can do like this: StringBuilder sb = new StringBuilder(); sb.append( "asd").append(34); method append returns StringBuilder instance, and I can continuosly call that. My question is it possible to do so in static method context? without class instance Yes. Like this (untested). public class Static { private final static Static INSTANCE = new Static(); public static Static doStuff(...) { ...; return INSTANCE; } public static Static doOtherStuff() { .... return INSTANCE; } } You can now have code like. Static.doStuff(...).doOtherStuff(...).doStuff(...); I would recommend

Create a new jquery chained method

回眸只為那壹抹淺笑 提交于 2019-12-04 19:42:05
How do you write new chained methods in jQuery? I have a very procedural style in my jQuery: $("#SaveButton").click(function () { Foo($("#SubTotal")); Foo($("#TaxTotal")); Foo($("#Total")); Bar($("#SubTotal")); Bar($("#TaxTotal")); Bar($("#Total")); }); How do I create a .foo() method in jQuery so that I can then write: $("#SaveButton").click(function () { $("#SubTotal,#TaxTotal,#Total").foo().bar(); }); And in a related point - is there an easy way (in Visual Studio, or Notepad++ or something else) to find and replace all Foo($("#selector")); with $("#selector").foo(); ? You can define custom

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

时光总嘲笑我的痴心妄想 提交于 2019-12-04 08:56:50
问题 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