小编Bry*_*yan的帖子

这是ContT的合适用途吗?

我正在做一个需要我写一个小翻译的项目.指令具有简单的树结构,其中一个命令具有停止执行的效果.因此,在下面的示例中,"baz"永远不会打印出来.

import Control.Monad.Cont

data Instruction = Print String | Halt | Block [Instruction]
    deriving (Eq, Show)

instructions =
  [ Print "foo"
  , Block
    [ Print "bar"
    , Halt
    ]
  , Print "baz"
  ]

main :: IO ()
main = runContT (callCC $ interpret instructions)
                (const $ pure ())

interpret []     k = pure ()
interpret (a:as) k = case a of
    Print str -> liftIO (putStrLn str) >> interpret as k
    Block ins -> interpret ins k       >> interpret as …
Run Code Online (Sandbox Code Playgroud)

monads continuations haskell

7
推荐指数
1
解决办法
1069
查看次数

如何编写汇总外汇蜡烛数据的查询?

我有一个包含以下架构的财务数据表:

         Table "public.candles"
   Column   |      Type      | Modifiers 
------------+----------------+-----------
 posix_time | bigint         | not null
 low        | numeric(8,2)   | not null
 high       | numeric(8,2)   | not null
 open       | numeric(8,2)   | not null
 close      | numeric(8,2)   | not null
 volume     | numeric(23,16) | not null
Indexes:
    "candles_pkey" PRIMARY KEY, btree (posix_time)
Run Code Online (Sandbox Code Playgroud)

每支蜡烛间隔一分钟.我想将数据汇总成蜡烛,间隔时间为5分钟,1小时,1天等.

我可以聚合posix_time,high,low,并volume与超过五分钟的时间间隔

SELECT posix_time/(60*5)*(60*5) AS new_posix_time,
       max(high)                AS new_high,
       min(low)                 AS new_low,
       sum(volume)              AS new_volume
FROM candles
GROUP BY new_posix_time
Run Code Online (Sandbox Code Playgroud)

并用适当的变量计算新的 …

sql postgresql

5
推荐指数
1
解决办法
530
查看次数

如何编写返回相邻对列表的 Clojure 函数?

我正在尝试编写一个函数adjacents,该函数返回序列相邻对的向量。所以(adjacents [1 2 3])会回来[[1 2] [2 3]]

(defn adjacents [s]
  (loop [[a b :as remaining] s
          acc []]
    (if (empty? b)
      acc
      (recur (rest remaining) (conj acc (vector a b))))))
Run Code Online (Sandbox Code Playgroud)

我当前的实现适用于字符串序列,但对于整数或字符,REPL 会输出此错误:

IllegalArgumentException Don't know how to create ISeq from: java.lang.Long  clojure.lang.RT.seqFrom (RT.java:494)
Run Code Online (Sandbox Code Playgroud)

clojure

4
推荐指数
1
解决办法
661
查看次数

标签 统计

clojure ×1

continuations ×1

haskell ×1

monads ×1

postgresql ×1

sql ×1