关于LearnYouaHaskell的这段代码:
newtype Pair b a = Pair {getPair :: (a,b)}
instance Functor (Pair c) where
fmap f (Pair (x, y)) = Pair (f x, y)
Run Code Online (Sandbox Code Playgroud)
我知道第一行是从元组创建一个新的数据类型.然而,为什么参数的顺序切换Pair b a到getPair :: (a,b)?如果我切换其中一个订单,那么由于定义中的模式匹配而出现错误fmap.
我想从 PySpark 上的数据框中进行分层抽样。有一个sampleBy(col, fractions, seed=None)功能,但它似乎只使用一列作为层。有没有办法使用多个列作为层?
我有一系列数字.我想对它们进行排序并删除重复项.这个答案建议使用set和sort进行那种操作.操作顺序不应改变结果,因此我测量了计算的时间.
from numpy.random import randint
from time import clock
n = 1000000
A=randint(0,n,n)
t1=clock()
B=sorted(set(A))
t2=clock()
C=set(sorted(A))
t3=clock()
print t2-t1, t3-t2
>>> 0.48011 1.339263
Run Code Online (Sandbox Code Playgroud)
sorted(set(A))大约比set(排序(A))快三倍.
是什么让一个比另一个更快?此外,还有更快的方法吗?
python ×2
algorithm ×1
apache-spark ×1
haskell ×1
newtype ×1
pyspark ×1
python-2.7 ×1
set ×1
sorting ×1