小编Neu*_*onQ的帖子

Django 中的域语言

有没有什么脑残简单的方法来根据域选择 Django 网站上页面的特定语言版本?(除了语言代码之外,两种语言的 URL 都是相同的,所以我希望与(默认使用“en”完成)mysite.com/teachers/manage相同,并且与- 不,URL 本身就像“教师/管理” ' 永远不会被翻译,所以我不想为两种语言配置 2 组 url)。mysite.com/en/teachers/managemesito.es/teachersmesito.es/es/teachers

注意:我发现的所有其他解决方案都意味着我也想翻译 URL,但我知道我永远不会想翻译这个特定网站的 URL 和 manae 2 组。

注2(忘记添加):大多数网站实际上是基于django-cms的,因此django-cms特定的解决方案将有助于...


好吧,我通过重定向视图通过这个丑陋的黑客得到了我想要的东西(但是,没有一个正确且简单的方法来做到这一点吗?...):

## mysite.urls:

urlpatterns = patterns('',
    (r'^$', 'mysite.views.language_router')


## mysite.views:

def language_router(request):
    if request.META['HTTP_HOST'].find('myspanishdomain.com') != -1 \
            and request.META['PATH_INFO'] == '/' :
        return HttpResponseRedirect('http://www.myspanishdomain.com/es/')
    return cms.views.details(request, '')
Run Code Online (Sandbox Code Playgroud)

(myenglishdomain.com 站点的默认语言为英语)

django localization internationalization

3
推荐指数
1
解决办法
2488
查看次数

Haskell:将`type`转换为`data`语句

什么是data此Haskell type语句的等效语句:

type CWIO a = CWorld -> (a, CWorld)
Run Code Online (Sandbox Code Playgroud)

......这会让我写:

instance Monad CWIO where
    (action1 >>= action2) w0 =
        let (x1, w1) = action1 w0
            (x2, w2) = action2 x1 w1
        in  (x2, w2)
    return a = \world -> (a, world)
Run Code Online (Sandbox Code Playgroud)

CWorld:

data CWorld = CWorld {
    cin :: String,
    cout :: String
}
Run Code Online (Sandbox Code Playgroud)

我正在通过实际构建一个真正有效的"虚拟IO monad"(在其"虚拟世界"中)来寻求"真正理解"monad.对于monad的概念有某种"通过构建/工程理解/解释",因为我是那种需要自己从头开始构建东西才能真正理解"某事"的人,所以我要去这里路线相同.整个代码上下文在这里https://gist.github.com/NeuronQ/11119444/adbf0a9d6d17d4231d7ec68f565203f8dd75f702,但对于任何有经验的Haskell程序员来说,它可能看起来毫无意义和"残酷".

monads haskell types algebraic-data-types io-monad

2
推荐指数
1
解决办法
118
查看次数

J语言的熵公式

我正在使用 J 编程语言,我尝试创建一个动词,用于从概率列表中计算熵(事件的结果,公式在 python/pesudocode: 中是这样的-sum([p*log(p,2) for p in ps]))。

我尝试使用组合 ( @:)的版本有效,但基于hook & fork 的版本似乎在做其他事情,我关心它为什么这样做。我正在尝试使用hook和fork 进行工作,这个案例确实证明我的直觉是错误的。

这是代码:

   probs =: 0.75 0.25    NB. probabilties
   entropy =: +/ @: (- * 2&^.)
   entropyWrong =: +/ (- * 2&^.)
   entropy probs         NB. this is correct
0.811278
   entropyWrong probs    NB. this is wrong!
 1.06128 1.25
0.561278 0.75
   NB. shouldn't the following be the same as above (wrong)?
   +/ (- * 2&^.) probs
0.811278 …
Run Code Online (Sandbox Code Playgroud)

hook fork entropy j

2
推荐指数
1
解决办法
147
查看次数

Haskell:如何在编写自己的"绑定"时更好地理解类型?

我试图通过实际尝试手动实现一些使用过的运算符(如>>=(bind))来绕过Haskell的monad .我这样做是为了尝试更好地理解事物,这种努力没有实际用途.但是我从一开始就进入了一堵砖墙,因为在monads的解释中看起来像"代码示例"的代码实际上并不是实际可编译的代码......

当我尝试定义自己的时候,bind我得到了一个类型错误.这段代码:

bind :: IO a -> (a -> IO b) -> IO b
(bind action1 action2) world0 =
    let (a, world1) = action1 world0
        (b, world2) = action2 a world1
    in
        (b, world2)
Run Code Online (Sandbox Code Playgroud)

...给我这些(9是上面代码的第一行):

iom.hs:10:1:
    Couldn't match expected type `t0 -> (t1, t2)'
                with actual type `IO b'
    The equation(s) for `bind' have three arguments,
    but its type `IO a -> (a -> IO b) -> IO b' has only two

iom.hs:11:23:
    Couldn't match …
Run Code Online (Sandbox Code Playgroud)

monads haskell types bind

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