我从他们更喜欢列表理解许多Pythonists听说过,因为他们可以做的一切,你可以使用高阶功能,如过滤器和减少,做和更多.所以这个问题解决了这些问题:你可以用它们做些什么的可靠例子,这对HOF来说很棘手?
python haskell functional-programming list-comprehension clojure
JSON.stringify显然不是很节省空间.例如,[123456789,123456789]在需要大约5时占用20多个字节.在发送到流之前,websocket会压缩其JSON吗?
记住这个计划:
class Convert a b where
convert :: a -> b
data A = A deriving Show
data B = B deriving Show
data C = C deriving Show
data D = DA A | DB B | DC C deriving Show
instance Convert A A where convert A = A
instance Convert A B where convert A = B
instance Convert A C where convert A = C
instance Convert B A where convert B = A
instance Convert …Run Code Online (Sandbox Code Playgroud) 这是我之前的一个问题的后续问题,我在其中询问为什么流融合不是在某个程序中踢.结果证明问题在于某些函数没有内联,并且一个INLINE标志通过about改进了性能17x(这表明了内联的重要性!).
现在,请注意,在最初的问题上,我立刻硬编码64了incAll.现在,假设我创建了一个nTimes函数,它重复调用一个函数:
module Main where
import qualified Data.Vector.Unboxed as V
{-# INLINE incAll #-}
incAll :: V.Vector Int -> V.Vector Int
incAll = V.map (+ 1)
{-# INLINE nTimes #-}
nTimes :: Int -> (a -> a) -> a -> a
nTimes 0 f x = x
nTimes n f x = f (nTimes (n-1) f x)
main :: IO ()
main = do
let size …Run Code Online (Sandbox Code Playgroud) 假设您必须编写一个程序来测试所有程序以搜索完成特定任务的程序.例如,考虑这个JavaScript函数:
function find_truth(){
for(n=0;;++n){
try {
var fn = Function(string(n));
if (fn() == 42)
return fn;
} catch() {
continue;
}
}
}
Run Code Online (Sandbox Code Playgroud)
只要string(n)返回第n个字符串("a","b","c",......"aa","ab"......),该程序最终将输出一个求值的函数42.此方法的问题在于它枚举的字符串可能是也可能不是有效的程序.我的问题是:是否可以自己枚举程序?怎么样?
在Haskell中,你可以得到Functor,Foldable并Traversable自动使用deriving.Applicative但是,没有办法得出.考虑到有一种明显的方法来定义一个Applicative实例(相当于一个压缩的应用程序),有没有办法启用deriving Applicative?
为了测试教会编码列表如何针对用户定义列表和本机列表执行,我准备了3个基准测试:
data List a = Cons a (List a) | Nil deriving Show
lenumTil n = go n Nil where
go 0 result = result
go n result = go (n-1) (Cons (n-1) result)
lsum Nil = 0
lsum (Cons h t) = h + (lsum t)
main = print (lsum (lenumTil (100000000 :: Int)))
Run Code Online (Sandbox Code Playgroud)
main = print $ sum ([0..100000000-1] :: [Int])
Run Code Online (Sandbox Code Playgroud)
fsum = (\ a -> (a (+) 0))
fenumTil n cons nil = go …Run Code Online (Sandbox Code Playgroud) 有没有办法使用JavaScript或jQuery设置全局类的CSS?也就是说,附加.my_class { foo:bar }到<style/>页面的标签?
例如,给出以下树数据类型:
data Tree a = Node [Tree a] | Leaf a deriving Show
type Sexp = Tree String
Run Code Online (Sandbox Code Playgroud)
如何使用高阶组合器表达"漂亮"功能,该组合打印出适当缩进的树?例如:
sexp =
Node [
Leaf "aaa",
Leaf "bbb",
Node [
Leaf "ccc",
Leaf "ddd",
Node [
Leaf "eee",
Leaf "fff"],
Leaf "ggg",
Leaf "hhh"],
Leaf "jjj",
Leaf "kkk"]
pretty = ????
main = print $ pretty sexp
Run Code Online (Sandbox Code Playgroud)
我希望该程序的结果是:
(aaa
bbb
(ccc
ddd
(eee
fff)
ggg
hhh)
jjj
kkk)
Run Code Online (Sandbox Code Playgroud)
这是一个不完整的解决方案,使用"fold"作为组合器,不实现缩进:
fold f g (Node children) = f (map (fold f g) children) …Run Code Online (Sandbox Code Playgroud) 在Haskell中,我创建了一个1000000 IntMaps的Vector.然后我使用Gloss以一种访问该向量的随机位图的方式呈现图片.
也就是说,我把每一个都留在了记忆中.渲染函数本身非常轻量级,因此性能应该很好.
然而,该计划以4fps的速度运行.在分析时,我注意到95%的时间花在了GC上.足够公平:
GC疯狂地扫描我的矢量,即使它永远不会改变.
有没有办法告诉GHC "这个大的价值是必要的,不会改变 - 不要试图在其中收集任何东西".
编辑:下面的程序足以复制问题.
import qualified Data.IntMap as Map
import qualified Data.Vector as Vec
import Graphics.Gloss
import Graphics.Gloss.Interface.IO.Animate
import System.Random
main = do
let size = 10000000
let gen i = Map.fromList $ zip [mod i 10..0] [0..mod i 10]
let vec = Vec.fromList $ map gen [0..size]
let draw t = do
rnd <- randomIO :: IO Int
let empty = Map.null $ vec Vec.! mod rnd size
let rad = if …Run Code Online (Sandbox Code Playgroud) haskell ×7
javascript ×3
algorithm ×2
applicative ×1
clojure ×1
combinators ×1
css ×1
fold ×1
ghc ×1
html5 ×1
jquery ×1
json ×1
list ×1
optimization ×1
performance ×1
python ×1
recursion ×1
socket.io ×1
styles ×1
types ×1
websocket ×1