我想知道目前是否可以使用Helm或通过GHCJS在浏览器上运行的任何其他引擎在Haskell中编写游戏?
使用$ http我可以轻松地捕获像401这样的错误:
$http({method: 'GET', url: 'http://localhost/Blog/posts/index.json'}).
success(function(data, status, headers, config) {
$scope.posts = data;
}).
error(function(data, status, headers, config) {
if(status == 401)
{
alert('not auth.');
}
$scope.posts = {};
});
Run Code Online (Sandbox Code Playgroud)
但是,在使用服务时,我怎么能做类似的事情.这是我当前服务的外观:
myModule.factory('Post', function($resource){
return $resource('http://localhost/Blog/posts/index.json', {}, {
index: {method:'GET', params:{}, isArray:true}
});
});
Run Code Online (Sandbox Code Playgroud)
(是的,我只是学习角度).
解决方案(感谢Nitish Kumar和所有贡献者)
在Post控制器中,我正在调用这样的服务:
function PhoneListCtrl($scope, Post) {
$scope.posts = Post.query();
}
//PhoneListCtrl.$inject = ['$scope', 'Post'];
Run Code Online (Sandbox Code Playgroud)
根据所选答案的建议,现在我这样称呼它并且它有效:
function PhoneListCtrl($scope, Post) {
Post.query({},
//When it works
function(data){
$scope.posts = data;
},
//When it fails
function(error){
alert(error.status);
}); …Run Code Online (Sandbox Code Playgroud) 假设我想让所有数字成为一个实例Monoid.而不是Num像这样为每个人创建一个实例:
instance Monoid Int where
mappend = (+)
mempty = 0
instance Monoid Float where
mappend = (+)
mempty = 0.0
-- etc
Run Code Online (Sandbox Code Playgroud)
有这样的事吗?
instance Num t => Monoid t where
mappend = (+)
mempty = 0
Run Code Online (Sandbox Code Playgroud)
有些人正在回答GHC扩展并警告潜在的问题; 我发现信息丰富,但我想我会坚持Sum,Product无论如何coerce.
我正在C#中为一个学校项目做一个编译器,我不禁想知道如何在Haskell中做到这一点.
例如:
我为While循环生成的代码是:
public override void generateCode(Compiler compiler)
{
int jumpToTheBeginningInstructionIndex = compiler.getIndexOfNextActionInCurrentFunction();
MachineInstructions.JMP jumpTotheBeginning = new MachineInstructions.JMP(jumpToTheBeginningInstructionIndex);
MachineInstructions.JMPF jumpToTheEnd = new MachineInstructions.JMPF();
booleanExpression.generateCode(compiler);
//I insert the jump to the end here:
compiler.addAction(jumpToTheEnd);
foreach(IAction action in this.thenActions)
{
action.generateCode(compiler);
}
compiler.addAction(jumpTotheBeginning);
//...But is here where I know where should it jump to:
jumpToTheEnd.whereToJump = compiler.getIndexOfNextActionInCurrentFunction();
}
Run Code Online (Sandbox Code Playgroud)
您可以看到我如何在方法的中间插入jumpToTheEnd的代码,但直到我知道跳转的行的末尾.幸运的是,我保留了一个指向该跳转的指针,我可以轻松地在方法的最后设置其whereToJump属性.
你会怎么做Haskell!?任何推荐的教程?
什么是类别理论中具有此类型的箭头的名称:
a -> a
Run Code Online (Sandbox Code Playgroud)
"从类型(?)到同一类型的另一个对象"
或许他们没有特别的名字?
换句话说:是否有从任何类型a到相同类型a的所有箭头的名称?该集的箭头(函数?)示例:
\x->x+x :: Int->Int
\x-> "hello, " ++ x :: String -> String
...
Run Code Online (Sandbox Code Playgroud)
编辑
@leftaroundabout说我正在使用对象的OO定义来进行类别理论,这是错误的.因此,我真正要问的是:"在类别理论中,在一个范畴中,从某个对象O到O本身的态射的名称是什么?"
在Haskell中,是否存在(liftM.liftM),(liftM.liftM.liftM)等的别名?
所以我不必如此冗长,例如:
(liftM . liftM) (+ 1) [Just 1, Just 2] = [Just 2, Just 3]
(liftM2 . liftM2) (+) [Just 1] [Just 2] = [Just 3]
Run Code Online (Sandbox Code Playgroud) 假设我有这种类型:
type T = int option
Run Code Online (Sandbox Code Playgroud)
以及该类型的可观察量:
let o : IObservable<T> = // create the observable
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种更好的表达方式:
o.Where(function | None -> false | Some t -> true)
.Select(function | Some t -> t)
Run Code Online (Sandbox Code Playgroud)
一个只能传播
Some案例的可观察者.
有几件事我不喜欢.
给出2种类型A和B默认值d(A)&d(B).和2个主题:
let sa = new Subject<A>()
let sb = new Subject<B>()
Run Code Online (Sandbox Code Playgroud)
和f类型的功能A -> B -> C
我创建了这个可观察的:
let o = Observable.CombineLatest(sa, sb, f)
Run Code Online (Sandbox Code Playgroud)
但是,我需要o根据d(A)&生成初始值d(B).文档说,一旦两个主题产生了他们的第一个输出,CombineLatest只会产生它的第一个输出.
我有两种方法,但我不知道哪种方法可行/哪种方法最好,或者我是否遗漏了一些已经做了我需要的运算符.
我来到了第一种方法是手动调用OnNext来sa和sb到相关的订阅后的默认值o已经作出.
我的第二种方法是使用BehaviorSubjects而不是plain Subjects,用初始值创建它们,并希望CombineLatest将它用于它的第一个输出.
谢谢阅读.
附录
我已经确认第二种方法有效,但我仍然不知道在这种情况下引入一个BehaviorSubject而不是一个平原Subject是最好的.
我在一些文档和教程中看到过:
这种模式涵盖的抽象概念是什么?在Haskell中运行什么意味着什么?
附带问题,是否有一个你一直想知道的东西的标签,但是太害怕了?