例如:我想有Add方法ICollection的自定义集合类来实现方法链接和流利的语言,所以我能做到这一点的:
randomObject.Add("I").Add("Can").Add("Chain").Add("This").
Run Code Online (Sandbox Code Playgroud)
我可以想到一些选项,但它们很混乱,并涉及将ICollection包装在另一个界面中等等.
我有以下代码,
<?php
class Templater
{
static $params = array();
public static function assign($name, $value)
{
self::$params[] = array($name => $value);
}
public static function draw()
{
self::$params;
}
}
$test = Templater::assign('key', 'value');
$test = Templater::draw();
print_r($test);
Run Code Online (Sandbox Code Playgroud)
如何更改此脚本以便我可以使用它?
$test = Templater::assign('key', 'value')->assign('key2', 'value2')->draw();
print_r($test);
Run Code Online (Sandbox Code Playgroud) 我有这个
if(noDelay){
$(element).find("." + options.class).remove();
} else {
$(element).find("." + options.class).fadeOut().remove();
}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以避免重复这个句子,只在fadeOut()满足给定条件时添加?
我不能移动fadeOut()直到链的末端,这可能会使事情变得更容易.
我在想类似的东西
$(element).find("." + options.class).(if(noDelay) fadeOut()).remove();
Run Code Online (Sandbox Code Playgroud)
提前谢谢,Leo
我想在Ruby中链接我自己的方法.而不是像这样编写ruby方法并使用它们:
def percentage_to_i(percentage)
percentage.chomp('%')
percentage.to_i
end
percentage = "75%"
percentage_to_i(percentage)
=> 75
Run Code Online (Sandbox Code Playgroud)
我想像这样使用它:
percentage = "75%"
percentage.percentage_to_i
=> 75
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我寻求建议是否使用./*这与 - >/this,即C++(*this).chained().methods()与this-> chained() - > methods().
顺便说一句,目前我见过的大多数页面都推荐[[C++(*this).chained().methods()]].
我只是想知道,因为你做不到
My_Class object.chained().methods();
(顺便说一句,我没有在第一部分中测试过这些例子.我在第二部分提供了测试的例子.)
你必须这样做
My_Class对象;
object.chained()方法();
这是一个恼人的额外线
或者你可以做到
Run Code Online (Sandbox Code Playgroud)My_Class object = My_Class().object.chained().methods();
这需要一个值复制 - 如果构造函数有副作用,这是不可接受的,比如注册对象实例 - 就像许多Knobs库一样
或者你可以做到
Run Code Online (Sandbox Code Playgroud)My_Class* object_ptr = *(new My_Class).object.chained().methods();
哪个有效,但需要烦人的*(ptr)
或者你可以做到
Run Code Online (Sandbox Code Playgroud)My_Class* object_ptr = (new My_Class)->object.chained()->methods();
这是一个更好的青少年.
我想你可以做到
My_Class&object_ref(My_Class().chained().methods());
而且我不确定我对此的看法.
顺便说一句,我不需要调试帮助.
我一直在编写这样的东西我只是为了清楚起见我提供的例子.
我正在寻求样式建议,因为有几种方法可以对它进行编码,而且我使用了不同的库来以相反的方式进行编码.
混合它们很难看:
My_Object_with_Setters* object_ptr2 = &((new My_Object_with_Setters)->set_R1(1).set_P1(2)->set_R1(3))
My_Object().method_returning_ptr()->method_returning_ref();
Run Code Online (Sandbox Code Playgroud)
也许它不是那么糟糕......但它肯定会令人困惑.
当我遇到使用混合.chained() - > methods()的两个不同库的代码时,我有时希望能够使用postfix地址和解除引用运算符
My_Object*mptr = My_Object().somethod_returning_ptr() - > method_returning_ref - >&
我经常使用这个习惯用于setter函数
class My_Object_with_Setters {
public:
static int count;
int value;
public: …Run Code Online (Sandbox Code Playgroud) 我想构建一个具有类似rails活动记录的接口的API客户端.我希望消费者能够链接方法,并且在最后一个方法被链接之后,客户端基于所调用的方法请求URL.所以它的方法是链接一些懒惰的评估.我查看了Active Record,但这非常复杂(产生过程等).
这是我正在谈论的那种事情的玩具示例.你可以在调用'get'之前将任意数量的'bar'方法链接在一起,如下所示:
puts Foo.bar.bar.get # => 'bar,bar'
puts Foo.bar.bar.bar.get # => 'bar,bar,bar'
Run Code Online (Sandbox Code Playgroud)
我已经成功实现了这个,但我宁愿不需要调用'get'方法.所以我想要的是:
puts Foo.bar.bar # => 'bar,bar'
Run Code Online (Sandbox Code Playgroud)
但我目前的实现是这样做的:
puts Foo.bar.bar #=> [:bar, :bar]
Run Code Online (Sandbox Code Playgroud)
我曾想过重写数组方法each,to_s但我相信有更好的解决方案.
我如何链接方法并知道哪个是最后一个,所以我可以返回类似get方法中返回的字符串?
这是我目前的实施:
#!/usr/bin/env ruby
class Bar
def get(args)
# does a request to an API and returns things but this will do for now.
args.join(',')
end
end
class Foo < Array
def self.bar
@q = new
@q << :bar
@q
end
def bar
self << :bar …Run Code Online (Sandbox Code Playgroud) 使用方法链接,我希望重复激活一个函数,但只在函数完成后.几乎就像在函数完全运行之前不执行.预期结果示例:
var myfunc = {
copy: function(message){
window.setTimeout(function(){
console.log(message);
},1000);
return this;
}
};
myfunc.copy('hello').copy('world');
// wait a second then log:
// hello
// wait until first function completion (1000ms), wait a second then log:
// world
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏!
我正在尝试使用链式方法更新 Rust 结构。我找到了一种方法来做到这一点,但我不确定我下面的代码是否是惯用的 Rust,而不仅仅是一种解决方法。
特别是,我.to_owned()在链式方法的末尾使用了返回借用的结构体。代码编译并运行得很好。这是最小的例子。
//struct.rs
#[derive(Debug, Default, Clone, PartialEq)]
pub struct ModelDataCapture {
run: i32,
year: i32,
}
impl ModelDataCapture {
pub fn new() -> Self {
ModelDataCapture::default()
}
pub fn set_run(&mut self, run: i32) -> &mut ModelDataCapture {
self.run = run;
self
}
pub fn set_year(&mut self, year: i32) -> &mut ModelDataCapture {
self.year = year;
self
}
}
//main.rs
let data_capture = ModelDataCapture::new()
.set_run(0)
.set_year(1)
.to_owned(); // <<< QUESTION
println!("here is the data capture {:?}", …Run Code Online (Sandbox Code Playgroud) 我编写的 API 有几个不返回值的异步方法,但仍应按调用顺序执行。我想从最终用户中抽象出正在等待的解决方案,以便他们可以链接方法调用,并期望每个承诺在前一个承诺解决后执行,如下所示:
api = new Api();
api.doAsync().doAnotherAsync().doAThirdAsync();
Run Code Online (Sandbox Code Playgroud)
我们从这些方法中获取值并不重要,重要的是它们按顺序执行。我尝试过使用链接结构,但它并不可靠。
class Api {
resolvingMethodChain = false;
constructor() {
this._methodChain = {
next: null,
promise: Promise.resolve(),
}
}
_chain(p) {
this._methodChain.next = {
promise: p,
next: null,
};
// if we are not finished resolving the method chain, just append to the current chain
if (!this.resolvingMethodChain) this._resolveMethodChain(this._methodChain);
this._methodChain = this._methodChain.next;
return this
}
async _resolveMethodChain(chain) {
if (!this.resolvingPromiseChain) {
this.resolvingPromiseChain = true;
}
// base case
if (chain === null) {
this.resolvingPromiseChain …Run Code Online (Sandbox Code Playgroud) 我有一个spring security配置方法.我希望antMatchers("/**/**").permitAll()仅在条件匹配时才链接特定方法.这样的事情{dev == true ? .antMatchers("/**/**").permitAll(): ()->{}} .当然,这不是一个有效的语法,最重要的做法是什么.寻找menimum编码.
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.cors().disable()
.authorizeRequests()
{dev == true ? .antMatchers("/**/**").permitAll(): ()->{}} //dev only. NEVER enable on prod
.antMatchers("/", "/signup", "/static/**", "/api/sigin", "/api/signup", "**/favicon.ico").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/")
.loginProcessingUrl("/api/signin")
.successHandler(authSuccessHandler())
.failureHandler(authFailureHandler())
.permitAll()
.and()
.logout()
.permitAll();
}
Run Code Online (Sandbox Code Playgroud) method-chaining ×10
javascript ×3
chaining ×2
ruby ×2
asynchronous ×1
c# ×1
c++ ×1
chain ×1
fluent ×1
icollection ×1
if-statement ×1
java ×1
java-8 ×1
jquery ×1
oop ×1
php ×1
rust ×1
spring-boot ×1
struct ×1
this ×1