each

Rails 3.1: Ruby idiom to prevent .each from throwing exception if nil?

跟風遠走 提交于 2019-12-05 11:59:25
问题 Is there a way to use .each so it does not throw an error if the object is nil or empty (without adding an additional nil/blank test? It seems that if I say phonelist.each do |phone| that if phonelist is empty, then the block should not be executed. But in my view (haml) I have - @myvar.phonelist.each do |phone| and if phonelist is empty, it throws a NoMethodError. I run into this a lot, and always workaround by adding an explicit check/branch for .blank? but it seems there should be an

IB9Y8 Asset Pricing 2019/20

混江龙づ霸主 提交于 2019-12-05 11:57:39
IB9Y8 Asset Pricing 2019/20 Group Project Assignment Page 1 of 9 IB9Y8 Asset Pricing 2019/20 Group Project This project asks students to estimate and test standard asset pricing models as discussed in the course, and to provide a qualitative evaluation of the results of this exercise in the context of the relevant theoretical framework. The objective is to allow students to demonstrate their conceptual understanding of the theory, as well as their ability to apply this knowledge to a concrete empirical investigation. The main focus is on time-series and cross-sectional tests of arbitrage

How do you find the last loop in a For Each (VB.NET)?

。_饼干妹妹 提交于 2019-12-05 11:56:11
问题 How can I determine if I'm in the final loop of a For Each statement in VB.NET? 回答1: The generally, collections on which you can perform For Each on implement the IEnumerator interface. This interface has only two methods, MoveNext and Reset and one property, Current . Basically, when you use a For Each on a collection, it calls the MoveNext function and reads the value returned. If the value returned is True , it means there is a valid element in the collection and element is returned via

usaco training <1.2 Greedy Gift Givers>

孤街浪徒 提交于 2019-12-05 11:46:28
题面 Task 'gift1': Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts of money. Each of these friends might or might not give some money to some or all of the other friends (although some might be cheap and give to no one). Likewise, each friend might or might not receive money from any or all of the other friends. Your goal is to deduce how much more money each person receives than they give. The rules for gift-giving are potentially different than you might expect. Each person goes to the bank (or any other source of money) to get a certain

How to set itemController in each as (ember 1.11 beta3)?

﹥>﹥吖頭↗ 提交于 2019-12-05 10:34:00
I want to try use: {{#each content as |product index|}} {{index}} {{/each}} But my app has the itemContoller, like this: {{#each product in content itemController='product'}} If I set this: {{#each content as |product index| itemController='product'}} It doesn't work! I found all of the ember guides and did not find the answer. Any help please. Controllers ( Object , Array and itemController ) are going away. The new way to do things is by using a component. So, instead of your item controller, you would define a component: App.MyProductComponent = Ember.Component.extend({ myIndex: function(){

1016 Phone Bills

陌路散爱 提交于 2019-12-05 09:57:48
先介绍本章(我新用上了的)新东西、最佳神器:priority_queue。妈妈再也不用担心我花时间写队列排序啦! 需求:#include<queue> 用法:priority_queue<T, vector<T>, greater<T>>(升序)/priority_queue<T, vector<T>, less<T>>(降序) vector其实不需要写,但是因为关键的升降序的说明是第三个参数故而第二个参数必须带一下。 因为其实和queue所需的函数一样(升降序本质也就只是大小于的应用),如果是queue<T>可以直接用的T那可以直接用priority_queue。 priority_queue中的函数会自动排好。例如greater的话,top()和pop()就是队列中最小的那一个。 做题逻辑……反正按人往他队列里塞时间点(把4个时间数据和online offline状况全都用一个int表示了方便排序),然后对人名排一下序。 最后输出时,tempii=top,pop,tempiii=top,判定tempii是online,iii是offline就开始处理输出,不然直接continue(已经pop掉了不要的数据了)。 然后本题测试样例坑点:有两个测试点里有“全账单完全无效的用户”,对于他们,应不输出任何东西。我一开始是先按用户输出第一行信息然后再做他的账单的判定、输出的于是就错了

1016 Phone Bills (25 分)

陌路散爱 提交于 2019-12-05 09:15:15
A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a long-distance call, the time will be recorded, and so will be the time when the customer hangs up the phone. Every calendar month, a bill is sent to the customer for each minute called (at a rate determined by the time of day). Your job is to prepare the bills for each month, given a set of phone call records. Input Specification: Each input file contains one test

CS5783: Machine Learning

ぐ巨炮叔叔 提交于 2019-12-05 04:49:19
CS5783: Machine Learning Assignment 4 1 Gaussian process regression Use the crash test dataset from assignment 3 again. In order to make numerical instability less of an issue, scale the x and t values of your dataset to values between 0 and 1, i.e. normalize each value by dividing it by the maximum value in that dimension. We will be generating Gaussian processes using two different kernels: • Squared exponential: k(x, x0) = exp{−(x−x0)22σ2 } • Exponential: k(x, x0) = exp{−|x−x0|σ} For each of these kernel families, construct your Gram matrix K and add diagonal noise to form C. In the last

jQuery: animated, continuous loop through children

与世无争的帅哥 提交于 2019-12-05 02:50:00
Close but not quite there. I'd like to have the first child div displayed for a couple seconds, slide down (via positioning) and fade out of view, then the next child slide up and fade into view. Repeat continuously, looping back after the last child is displayed. Looks like I've got the loop working though the count seems to pile the child divs on top of each other. What am I doing wrong? http://jsfiddle.net/rrbaker/Xmk2y/4/ Here's my remix: http://jsfiddle.net/ddrace/DJuV7/1/ I rearranged some things to make it more understandable and added settings for the pause and animation to keep it DRY

Golang必备技巧:接口型函数

落花浮王杯 提交于 2019-12-05 01:36:03
接口型函数,指的是用函数实现接口,这样在调用的时候就会非常简便,我称这种函数,为接口型函数,这种方式适用于只有一个函数的接口。 我们以迭代一个map为例,演示这一技巧,这种方式有点类似于groovy中Map的each方法一样,也是Gradle里each闭包。 原始接口实现 type Handler interface { Do(k, v interface {}) } func Each(m map [ interface {}] interface {}, h Handler) { if m != nil && len (m) > 0 { for k, v := range m { h.Do(k, v) } } } 首先定义一个Handler接口,只有一个Do方法,接收k,v两个参数,这就是一个接口了,我们后面会实现他,具体做什么由我们的实现决定。 然后我们定义了一个Each函数,这个函数的功能,就是迭代传递过来的map参数,然后把map的每个key和value值传递给Handler的Do方法,去做具体的事情,可以是输出,也可以是计算,具体由这个Handler的实现来决定,这也是面向接口编程。 现在我们就以新学期开学,大家自我介绍为例,演示使用我们刚刚定义的Each方法和Handler接口。这里我们假设有三个学生,分别为:张三,李四和王五,他们每个人都要介绍自己的名字和年龄。