我正在通过在Apache Tomcat/6.0.18上运行的Spring MVC控制器传输大型文档
因为它很大,并且(最终)会动态生成,所以我决定使用分块传输编码.
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.httpclient.ChunkedOutputStream;
import org.apache.commons.net.io.CopyStreamException;
import org.apache.commons.net.io.Util;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class QueryController {
@Inject
QueryService queryService;
@RequestMapping(value = "/stream")
public void hellostreamer(HttpServletResponse response) throws CopyStreamException, IOException {
response.setHeader("Transfer-Encoding", "chunked");
response.setHeader("Content-type", "text/xml");
InputStream filestream = new FileInputStream("/lotsrecs.xml");
ChunkedOutputStream chunkStream = new ChunkedOutputStream(response.getOutputStream());
Util.copyStream(filestream,chunkStream);
chunkStream.close();
chunkStream.finish();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我在firefox中打开它时,我得到了这个:
XML Parsing Error: syntax error
Location: http://localhost:8082/streaming-mockup-1.0-SNAPSHOT/stream
Line Number 1, Column 1:
800
^
Run Code Online (Sandbox Code Playgroud)
而不是将块大小作为关于流的元数据读取,而是将它们作为流的一部分读取!
使用Live HTTP标头,我可以看到正在接收Transfer-Encoding标头: …
我已经意识到,当我有嵌套数据结构时,我一直在手动编写代码来深入研究它们.像这样:
--one level
Prelude> map (*2) [1,2,3]
[2,4,6]
--nested two levels
Prelude> let t2 = map $ map (*2)
Prelude> t2 [[1,2,3],[4,5,6]]
[[2,4,6],[8,10,12]]
--nested three levels
Prelude> let t3 = map $ map $ map (*2)
Prelude> t3 [[ [1,2,3],[4,5,6] ],[ [1,2,3],[4,5,6] ]]
[[[2,4,6],[8,10,12]],[[2,4,6],[8,10,12]]]
Run Code Online (Sandbox Code Playgroud)
所以我发现我应该能够使用更高阶函数自动构造一个函数来钻研我的嵌套数据结构:
Prelude> let t f n = (iterate map f) !! n
<interactive>:35:22:
Occurs check: cannot construct the infinite type: b0 = [b0]
Expected type: (a0 -> b0) -> a0 -> b0
Actual type: (a0 -> …Run Code Online (Sandbox Code Playgroud) 我想在图像中写入一些像素,然后将图像写入磁盘.我一直在遵循我从很多Haskellers那里听到的关于类型签名的建议,并且基本上玩"类型俄罗斯方块"直到我到达我要去的地方.它主要为我工作,但我遇到了一些麻烦.
要写一个像素,有一个功能:
writePixel :: PrimMonad m => MutableImage (PrimState m) a -> Int -> Int -> a -> m ()
Run Code Online (Sandbox Code Playgroud)
我可以通过阅读签名来告诉我需要传递一个MutableImage,所以我寻找一个带有MutableImage的函数:
createMutableImage :: (PrimMonad m, Pixel px) => Int -> Int -> px -> m (MutableImage (PrimState m) px)
Run Code Online (Sandbox Code Playgroud)
这似乎在某种状态monad中运行.
writeIt = runST $ createMutableImage 100 100 (100::Pixel8) >>=
freezeImage
Run Code Online (Sandbox Code Playgroud)
这工作,并返回我可以写入磁盘的漂亮的灰色图像.但我不知道如何获取MutableImage,所以我可以写像素!简单地插入writePixel调用会给我一个错误,我无法理解:
writeIt = runST $ createMutableImage 100 100 (100::Pixel8) >>=
writePixel 100 100 255 >>=
freezeImage
Run Code Online (Sandbox Code Playgroud)
结果是:
Couldn't match type `MutableImage (PrimState (GHC.ST.ST s)) px0'
with `()'
Expected type: () …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单/愚蠢的问题。
我有一个从 Spark 数据帧返回的变量,如下所示:
Any = WrappedArray(101, 11, 119, 141, 238, 64, 45, 268, 31, 63, 86, 23, 24, 420, ...
Run Code Online (Sandbox Code Playgroud)
我想做的就是将其转换为普通数组,以便我可以迭代整数。即使我只能将其获取到WrappedArray类型,我也可以从那里获取数组。
运行"cabal install sdl-mpeg"
$ ls -l /usr/include/smpeg/smpeg.h
-rw-r--r-- 1 root root 7503 2008-11-05 18:07 /usr/include/smpeg/smpeg.h
$ cabal install sdl-mpeg
Resolving dependencies...
Configuring SDL-mpeg-0.0.1...
cabal: Missing dependency on a foreign library:
* Missing header file: smpeg.h
This problem can usually be solved by installing the system package that
provides this library (you may need the "-dev" version). If the library is
already installed but in a non-standard location then you can use the flags
--extra-include-dirs= and --extra-lib-dirs= to specify where it …Run Code Online (Sandbox Code Playgroud) 好的,所以我有一个控制器,它的模型设置如下:
App.MyModel = DS.Model.extend({
myOtherProperty: attr(),
mySortKey: attr(),
mySubCollection: function () {
return this.store.filter('MyOtherModel', function (myOtherModel) {
//Some basic filtering logic (it's actually more complex than this)
return myOtherModel.get('someProperty') == this.get('myOtherProperty');
});
}.property('myOtherProperty')
})
Run Code Online (Sandbox Code Playgroud)
实际的过滤函数并不重要 - 重要的是'mySubCollection'计算属性返回DS.PromiseArray对象.上述模型的控制器类型为'Ember.ObjectController',因为它只显示单个对象.
显示此属性的模板使用{{each}}帮助程序执行此操作:
MyTemplate的:
{{#each mySubCollection}}
{{view App.MyView}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)
现在 - 我需要按'mySortKey'属性按降序排序这个promise数组.我怎样才能做到这一点?文档说DS.PromiseArray从Ember.Array扩展,但在'mySubCollection'的计算属性末尾添加'sortBy('mySortKey:desc')'导致它破坏而根本不起作用 - 这是有道理的,因为我会在一个承诺而不是数组上调用'sortBy'.
我看了一堆不同的想法,不断出现的是切换到使用ArrayController.我可以做到这一点,但它并不理想,因为它在一个非常简单的设置之上添加了一堆复杂性,它已经很好用了(除了排序).
有什么想法吗?
我试图了解如何使用 Traversable 迭代我的树数据结构。我有一棵丑陋的树(实际上是森林),看起来像这样:
data ParseTree a = Leaf a | Node a (ParseTree a) (ParseTree a) | MultiNode a [ParseTree a]
deriving (Show, Functor, F.Foldable, T.Traversable)
t = Node S
(Node NP (Leaf DP) (Leaf NP))
(MultiNode VP
[Node VP (Node VP (Leaf VP) (Leaf PP)) (Node NP (Leaf DP) (Leaf NP)),
Node VP (Leaf VP) (Node PP (Leaf PP) (Node NP (Leaf DP) (Leaf NP)))]
)
Run Code Online (Sandbox Code Playgroud)
我想找到多节点,以便我可以替换构建新树,多节点中的每个项目一个。
对我来说编写这样的函数很容易
findmulti :: ParseTree a -> [ParseTree a]
findmulti (Leaf a) = …Run Code Online (Sandbox Code Playgroud) 我写了一些使用repa 计算距离矩阵的代码:
distance :: Int -> Int -> Mat -> Double
distance aindx bindx arr = let a = slice arr (Any :. aindx :. All)
b = slice arr (Any :. bindx :. All)-
sqdiff = R.map (\x -> x*x) $ R.zipWith (-) a b
in sqrt $ sumAllS sqdiff
buildDistanceMatrix :: Mat -> Mat
buildDistanceMatrix m = let (Z :. height :. width) = R.extent m
cords = fromListUnboxed (Z :. (height * height) ) [ (x,y) …Run Code Online (Sandbox Code Playgroud) haskell ×5
apache-spark ×1
ember-data ×1
ember.js ×1
javascript ×1
juicy-pixels ×1
lifting ×1
mpeg ×1
quickcheck ×1
repa ×1
scala ×1
sdl ×1
spring ×1
state-monad ×1
streaming ×1