下面的代码表达了我的问题:(它是自包含的,你可以创建一个带有空模板的Xcode项目,替换main.m文件的内容,删除AppDelegate.h/.m文件并构建它)
//
// main.m
// CollectionViewProblem
//
#import <UIKit/UIKit.h>
@interface Cell : UICollectionViewCell
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UILabel *label;
@end
@implementation Cell
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
self.label = [[UILabel alloc] initWithFrame:self.bounds];
self.label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.label.backgroundColor = [UIColor greenColor];
self.label.textAlignment = NSTextAlignmentCenter;
self.button = [UIButton buttonWithType:UIButtonTypeInfoLight];
self.button.frame = CGRectMake(-frame.size.width/4, -frame.size.width/4, frame.size.width/2, frame.size.width/2);
self.button.backgroundColor = [UIColor redColor];
[self.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.label];
[self.contentView addSubview:self.button];
}
return self;
}
// Overriding …Run Code Online (Sandbox Code Playgroud) 我正在阅读在线LYAH书(该链接将直接带您到我的问题所涉及的部分).
作者定义了二叉树数据类型,并展示了如何通过实现foldMap函数将其作为Foldable类型的实例(在Data.Foldable中定义):
import Data.Monoid
import qualified Data.Foldable as F
data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)
instance F.Foldable Tree where
foldMap f Empty = mempty
foldMap f (Node x l r) = F.foldMap f l `mappend`
f x `mappend`
F.foldMap f r
Run Code Online (Sandbox Code Playgroud)
foldMap的类型声明如下:
F.foldMap :: (Monoid m, F.Foldable t) => (a -> m) -> t a -> m
Run Code Online (Sandbox Code Playgroud)
所以它需要一个函数,它接受一个类型为"a"的实例并返回一个monoid.
现在作为示例,作者创建一个Tree实例
testTree = Node 5
(Node 3
(Node 1 Empty …Run Code Online (Sandbox Code Playgroud) 我正在使用一个简单的命令行应用程序来处理内存(de)分配内容,该应用程序适用于使用启用了ARC的Xcode版本4.2.1构建的Mac OSX 10.7以及默认的构建设置.我无法解释我从ARC的理解中得到的行为,所以我希望有人可以解释这里发生了什么.
首先,在下面的代码中我得到了我期望的行为(请注意NLog()输出在相应语句后的注释中给出)
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSObject *objPtr1 = [[NSObject alloc] init];
NSObject *objPtr2 = objPtr1;
__weak NSObject *weakRef = objPtr1;
NSLog(@"%@", [objPtr1 description]); // <NSObject: 0x1001107d0>
objPtr1 = nil;
NSLog(@"%@", [objPtr2 description]); // <NSObject: 0x1001107d0>
objPtr2 = nil;
NSLog(@"%@", [weakRef description]); // (null)
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以在上面,在分配了weakRef之后,NSObject实例有两个强指针,因此保留计数为2.在将objPtr1归零后,还有一个保留指向实例的指针,因此它仍然在内存中并响应说明消息.在nir-ing objPtr2之后,没有指向该对象的强指针并且它被解除分配(我假设它是,因为weakRef已被置零).到现在为止还挺好.
现在,相同的代码有一个小的变化:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSObject *objPtr1 = [[NSObject alloc] init];
NSObject *objPtr2 …Run Code Online (Sandbox Code Playgroud) 我一直试图通过在线书籍LYAH "向我学习一个Haskell" .
作者描述了Applicative类型的Functors的行为,它具有从一个仿函数中提取函数并将其映射到第二个仿函数的能力; 这是通过为Applicative类类声明的<*>函数:
class (Functor f) => Applicative f where
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
Run Code Online (Sandbox Code Playgroud)
举个简单的例子,在下面的实现中,Maybe类型是Applicative的一个实例:
instance Applicative Maybe where
pure = Just
Nothing <*> _ = Nothing
(Just f) <*> something = fmap f something
Run Code Online (Sandbox Code Playgroud)
前面提到的行为的一个例子:
ghci> Just (*2) <*> Just 10 -- evaluates to Just 20
Run Code Online (Sandbox Code Playgroud)
所以<*>运算符从第一个操作数"提取"(*2)函数并将其映射到第二个操作数.
现在在Applicative类型中,<*>的两个操作数都属于同一类型,所以我认为为什么不尝试实现这种行为的泛化,其中两个操作数是不同类型的Functors,所以我可以评估这样的东西:
Just (2*) <*:*> [1,2,3,4] -- should evaluate to [2,4,6,8]
Run Code Online (Sandbox Code Playgroud)
所以这就是我提出的:
import Control.Applicative
class (Applicative …Run Code Online (Sandbox Code Playgroud) 我在玩Haskell交互式提示符(ghci)时遇到了一些我很好奇的东西.以下代码在ghci 7.0.4下运行
[minBound..1]
Run Code Online (Sandbox Code Playgroud)
抛出以下异常:
<interactive>:1:12:
Ambiguous type variable `t0' in the constraints:
(Num t0) arising from the literal `1' at <interactive>:1:12
(Enum t0) arising from the arithmetic sequence `minBound .. 1'
at <interactive>:1:1-13
(Bounded t0) arising from a use of `minBound'
at <interactive>:1:2-9
Probable fix: add a type signature that fixes these type variable(s)
In the expression: 1
In the expression: [minBound .. 1]
In an equation for `it': it = [minBound .. 1]
Run Code Online (Sandbox Code Playgroud)
我知道将上面的内容写成[minBound..1 :: Int]会清楚地表明'1'在这里是一个Int,但我的问题是,歧义在哪里?'1'可以解释为Int …
我是一个新的(约1天)R用户.我试图产生六个骰子三次投掷的所有216个结果.关键是然后将一些函数应用于每个三元组(例如,最大面值).这就是我想出来的:
mat <- matrix(numeric(0), ncol=3)
for (i in 1:6) {
for (j in 1:6) {
for (k in 1:6) {
mat <- rbind(mat, c(i, j, k))
}
}
}
# find maximum of each outcome
apply(mat, 1, max)
Run Code Online (Sandbox Code Playgroud)
用R做一个更好,更简洁的方法吗?我会喜欢用outer这种方式:
outer(1:6, outer(1:6, 1:6, max), max)
Run Code Online (Sandbox Code Playgroud)
但它失败了,错误
外部错误(1:6,1:6,max):dims [product 36]与object [1]的长度不匹配
在 Stroustrup 的《Programming: Principles and Practices of Programming Using C++(第二版)》一书中,作者创建了struct如下:
const int not_a_reading = –7777;
struct Day {
vector<double> hour {vector<double>(24,not_a_reading)};
};
// As the author says: "That is, a Day has 24 hours,
// each initialized to not_a_reading."
Run Code Online (Sandbox Code Playgroud)
我知道vector<double> hour{24, not_a_reading} 不会这样做,因为它初始化了两个元素的向量,24 和 -7777,这不是所需的对象。
但是有什么理由说明作者的初始化技巧比仅仅做的要好:
vector<double> hour(24, not_a_reading)
Run Code Online (Sandbox Code Playgroud)
(?)
这个问题是关于PL如何工作的好奇心,而不是其他任何问题.(实际上,我看到的是SML,它与Haskell的不同之处在于前者使用call-by-value - 但我的问题是关于Haskell.)
Haskell(据我所知)有"按需调用"语义.这是否意味着如果我定义一个函数如下:
cond True thenExp elseExp = thenExp
cond _ thenExp elseExp = elseExp
Run Code Online (Sandbox Code Playgroud)
这将总是表现得像if-then-else表达式?或者,在另一种意义上,if-then-else可以被视为可以被定义为函数的东西的语法糖?
编辑:
只是为了对比Haskell与Standard ML的行为,定义(在SML中)
cond p t f = if p then t else f;
然后是阶乘函数
fun factc n = cond (n=0) 1 (n * factc (n-1));
评估factc 1(说)永远不会完成,因为最后一个参数中的递归cond永远不会终止.
但是,定义
fun fact n = if n=0 then 1 else fact (n-1);
按预期工作,因为then分支仅根据需要进行评估.
也许有一些聪明的方法可以推迟SML中的参数评估(不知道我还不熟悉它)但重点是,在一个按值调用的类型语言中,if-then-else通常表现不同.我的问题是这是否(按需调用与按价值调用)是这种差异背后的主要原因(并且共识似乎是"是").
haskell ×4
c++11 ×1
constructor ×1
functor ×1
ios ×1
objective-c ×1
r ×1
types ×1
uiresponder ×1
vector ×1