不是一个特别强大的Javascript人,我在尝试更新DateMongo中的很多对象时遇到了一些麻烦.
似乎$inc尚未实现Date对象.所以,为了尝试在一天之内碰到一堆日期,我从bash通过mongo myScript.js以下方式调用(类似)这个脚本:
conn = new Mongo();
db = conn.getDB('myDatabase');
var incrementDates = function() {
db.blah.find(myQuery).forEach(function(doc) {
db.blah.update(
{ _id : doc._id
, my_date : { $exists : true }
}
, { $set : { my_date : new Date(doc.my_date.getTime() + 86400000) }}
);
});
}
incrementDates();
Run Code Online (Sandbox Code Playgroud)
基本思想似乎在mongoDB shell中运行良好:
> var doc = db.blah.findOne(myQuery)
> doc.my_date
ISODate("1962-11-02T23:00:00Z")
> new Date(doc.my_date.getTime() + 86400000);
ISODate("1962-11-03T23:00:00Z")
Run Code Online (Sandbox Code Playgroud)
但在脚本中不太好:
TypeError: doc.my_date has no properties
Run Code Online (Sandbox Code Playgroud)
所以我认为我试图getTime …
拿一个最小的语言(显然叫做Hutton's Razor):
{-# OPTIONS_GHC -fno-warn-missing-methods #-}
data Expr =
Lit Int
| Add Expr Expr
deriving (Eq, Show)
instance Num Expr where
fromInteger = Lit . fromInteger
(+) = Add
tree 0 = 1
tree n =
let shared = tree (n - 1)
in shared + shared
Run Code Online (Sandbox Code Playgroud)
如果我们只是tree 30在GHCi中运行,那么类型将默认为,Integer并且我们将立即得到答案,因为tree共享的递归计算.但是如果我们运行tree 30 :: Expr,我们会得到一个巨大的语法树,因为Haskell提供的共享let不会转移到嵌入式语言中的表达式.
该data-reify库可用于观察表达式中可能存在的任何隐式共享.我们可以添加一些机器来实现:
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE TypeFamilies #-}
import Control.Applicative
import Data.Reify …Run Code Online (Sandbox Code Playgroud) 我在编译二进制文件时观察两台机器之间的不同链接行为.
每个都有相同的GHC(7.8.3),相同的代码,相同的标志(-Wall -O2),相同的libgmp(每个都由Homebrew安装):
machine-one$ otool -L my-binary
my-binary:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)
/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
machine-two$ otool -L my-binary
my-binary:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)
/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
/usr/local/lib/libgmp.10.dylib (compatibility version 13.0.0, current version 13.0.0)
Run Code Online (Sandbox Code Playgroud)
我不能为我的生活弄清楚为什么libgmp在第二台机器上动态链接.
就差异而言,我已经能够认识到:GHC已经通过第一台机器上的OS X的二进制分发版和第二台机器上的Homebrew进行了安装.对于C编译器,我们有:
machine-one$ cc --version
Apple LLVM version 6.0 (clang-600.0.51) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
machine-two$ cc --version
Apple …Run Code Online (Sandbox Code Playgroud) 使用这个简单的基础仿函数和其他机器来获得具有约束条件的免费monad:
{-# LANGUAGE DeriveFunctor #-}
import Control.Monad.Free
data ProgF r =
FooF (Double -> r)
| BarF Double (Int -> r)
| EndF
deriving Functor
type Program = Free ProgF
foo = liftF (FooF id)
bar a = liftF (BarF a id)
Run Code Online (Sandbox Code Playgroud)
这是一个简单的程序
prog :: Program Int
prog = do
a <- foo
bar a
Run Code Online (Sandbox Code Playgroud)
它有以下(手工制作)AST:
prog =
Free (FooF (\p0 ->
Free (BarF p0 (\p1 ->
Pure p1))
Run Code Online (Sandbox Code Playgroud)
我希望能够做的是以下列方式推理绑定术语:
PureAST 中的术语如果没有进行某种配对,直接通过cofree comonad注释一个免费的monad …
这是一个简单的函数。它接受一个输入Int并返回一个(可能为空)对列表(Int, Int),其中输入Int是任何对的立方元素的总和。
cubeDecomposition :: Int -> [(Int, Int)]
cubeDecomposition n = [(x, y) | x <- [1..m], y <- [x..m], x^3 + y^3 == n]
where m = truncate $ fromIntegral n ** (1/3)
-- cubeDecomposition 1729
-- [(1,12),(9,10)]
Run Code Online (Sandbox Code Playgroud)
我想测试上述属性是否正确;如果我对每个元素进行立方并对任何返回元组求和,那么我会得到我的输入:
import Control.Arrow
cubedElementsSumToN :: Int -> Bool
cubedElementsSumToN n = all (== n) d
where d = map (uncurry (+) . ((^3) *** (^3))) (cubeDecomposition n)
Run Code Online (Sandbox Code Playgroud)
出于运行时考虑,我想Int在使用 QuickCheck 进行测试时将输入限制为一定大小。我可以定义适当的类型和Arbitrary实例:
{-# …Run Code Online (Sandbox Code Playgroud) 我在查看Statemonad中运行的模拟中如何减少内存使用和GC时间时遇到了一些麻烦.目前我必须运行已编译的代码+RTS -K100M以避免堆栈空间溢出,并且GC统计数据非常可怕(见下文).
以下是代码的相关摘要.完整的,有效的(GHC 7.4.1)代码可以在http://hpaste.org/68527找到.
-- Lone algebraic data type holding the simulation configuration.
data SimConfig = SimConfig {
numDimensions :: !Int -- strict
, numWalkers :: !Int -- strict
, simArray :: IntMap [Double] -- strict spine
, logP :: Seq Double -- strict spine
, logL :: Seq Double -- strict spine
, pairStream :: [(Int, Int)] -- lazy (infinite) list of random vals
, doubleStream :: [Double] -- lazy (infinite) list of …Run Code Online (Sandbox Code Playgroud) 我几乎没有成功地绕着包装中涉及的类型的基本管道ad包裹.例如,以下工作完美:
import Numeric.AD
ex :: Num a => [a] -> a
ex [x, y] = x + 2*y
> grad ex [1.0, 1.0]
[1.0, 2.0]
Run Code Online (Sandbox Code Playgroud)
哪里grad有类型:
grad
:: (Num a, Traversable f) =>
(forall (s :: * -> *). Mode s => f (AD s a) -> AD s a)
-> f a -> f a
Run Code Online (Sandbox Code Playgroud)
如果我改变的类型签名ex来[Double] -> Double,并尝试同样的事情,我得到
Couldn't match expected type `AD s a0' with actual type `Double'
Expected type: …Run Code Online (Sandbox Code Playgroud) 我正在读某个地方的咖喱功能,听起来很混乱.这个例子让我更加困惑.可以说我有一个功能:
power :: (Int, Float) -> Float -- computes the nth power of b
power (n, b) =
if n == 0 then 1.0 else b * power (n-1, b)
Run Code Online (Sandbox Code Playgroud)
现在,我定义另一个函数powerc:: Int -> Float -> Float,使得
powerc n b =
if n == 0 then 1.0 else b * powerc (n-1) b
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释一下这个功能是如何powerc发布的power.
请考虑以下优秀博客文章中的以下缩写代码:
import System.Random (Random, randomRIO)
newtype Stream m a = Stream { runStream :: m (Maybe (NonEmptyStream m a)) }
type NonEmptyStream m a = (a, Stream m a)
empty :: (Monad m) => Stream m a
empty = Stream $ return Nothing
cons :: (Monad m) => a -> Stream m a -> Stream m a
cons a s = Stream $ return (Just (a, s))
fromList :: (Monad m) => [a] -> NonEmptyStream m a
fromList …Run Code Online (Sandbox Code Playgroud) haskell ×8
ghc ×2
random ×2
data-reify ×1
free-monad ×1
graph ×1
javascript ×1
macos ×1
mongodb ×1
quickcheck ×1
recursion ×1
simulation ×1
types ×1