Random.Generate Int的情况

时间:2019-06-21 09:42:37

标签: elm

我是榆树新手。榆木版本为0.19。我正在尝试使用Ranndom.int选择飞船类型,但是不知道如何做。 我应该如何使用numToShip函数?更改类型?

这是我的代码...

class CustomerAPITestAdmin(APITestCase):
    fixtures = ['new-fixtures.json']

    def tokenSetUp(self,user):
        self.token = Token.objects.get(user__username=user)
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)

    def testSaveMeasure(self):
        self.tokenSetUp('jpmichel')
        self.executeTest()

    def executeTest(self):

        data = {
            "patient": 34,
            "measure_cat": 52,
            "measure_type": "1",
            "date": "2019-06-11",
            "value": '',
            "unit": '',
            "nnu_value": "3"
        }

        print(json.dumps(data))

        headers = {'Authorization': 'Token '+ self.token.key, 'content_type':'application/json', 'Accept-Language' : 'en'}
        response = requests.post('http://127.0.0.1:8000/saveMeasure/', data=json.dumps(data), headers=headers)

        self.assertEqual(response.status_code, 201)

错误消息是:

type ShipType
    = Battleship
    | Cruiser   
    | Destroyer 
    | Submarine 

numToShip : Int -> ShipType
numToShip num =
   case num of
     0 -> Destroyer
     1 -> Battleship
     2 -> Cruiser
     _ -> Submarine

shipName : ShipType -> String
shipName shipType =
  case shipType of
    Destroyer ->
        "Destroyer"
    Battleship ->
        "Battleship"
    Cruiser ->
        "Cruiser"
    Submarine ->
        "Submarine"

randomShip : String
randomShip =
  shipName (numToShip (Random.int 0 3) )

这些永远无法比拟!模式是问题吗?还是表达?

1 个答案:

答案 0 :(得分:2)

Random.int不会返回Int,而是返回Generator Int。然后,您将调用Random.generateGenerator转换为Cmd,然后将使用生成的值调用update函数。

Elm的特征之一是所有功能都是 pure ,这意味着相同的输入始终会导致相同的输出。由于每次调用该函数时都要求一个不同的值,因此需要将该命令交给运行时,该运行时可以处理请求中的各种杂项(当您想与外部通过HTTP或JavaScript)。有关更多详细信息,请参见the Random example in the Elm Guide

或者,如果您愿意提供用于计算随机值的种子,则可以立即从Generator获取值。您可以使用Random.step,它接受​​一个Generator和一个Seed,并生成一个值下一个种子,您可以将其反馈到{{1 }}(如果您需要多个值)。如果能够“重播”您的随机值很有用,那么您可能只想这样做,因为将step保持在一定位置是很痛苦的。否则,只需坚持使用Seed来创建generate