首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这能写得更简洁,并与函数式反应式编程范例相一致吗?

这能写得更简洁,并与函数式反应式编程范例相一致吗?
EN

Code Review用户
提问于 2013-08-15 20:56:59
回答 1查看 528关注 0票数 7

试着学习玻璃钢和榆树。从玻璃钢和榆树的角度看下面的节目"ok“吗?还有什么可以改进的?这个程序可以在这里执行:http://elm-lang.org/try

代码语言:javascript
复制
import Random
import Mouse
data Fruit = Apple | Orange | Banana | Melon | Guava
rand = Random.range 0 4 (Mouse.clicks)
fruit n = if | n == 0 -> Apple
             | n == 1 -> Orange
             | n == 2 -> Banana
             | n == 3 -> Melon
             | otherwise -> Guava
fruitText = toForm <~ (asText <~ (fruit <~ rand))
time = lift (inSeconds . fst) (timestamp (fps 40))
scene dy form = collage 300 300 [move (0, 100 * cos dy) form]
main = lift2 scene time fruitText
EN

回答 1

Code Review用户

回答已采纳

发布于 2013-10-10 09:27:38

迟了两个月,但我还是会尽力回答的。;-)

由于您的rand已经确定了数字将是有效的索引,您的水果,您不必使用这么多,如果情况。其余的在我看来还不错,但我在源代码中做了一些注释。

代码语言:javascript
复制
import Random
import Mouse
-- Sometimes empty lines can improve readability.

data Fruit = Apple | Orange | Banana | Melon | Guava

-- For module global declarations, it is often helpful to
-- explicitly annotate the types.
fruits : [Fruit]
fruits = [Apple, Orange, Banana, Melon, Guava]

-- especially for functions :)
fruit : Int -> Fruit
fruit n = head <| drop n fruits

rand : Signal Int
rand = Random.range 0 ((length fruits) - 1) Mouse.clicks

-- exercise: Try to figure and annotate the remaining types. ;-)
fruitText = toForm <~ (asText <~ (fruit <~ rand))

time = lift (inSeconds . fst) (timestamp (fps 40))

scene dy form = collage 300 300 [move (0, 100 * cos dy) form]

main = lift2 scene time fruitText

btw:较让某人复制粘贴到elm-lang.org/try,向某人显示elm代码要容易一些。

请参阅:http://share-elm.com/sprout/52567327e4b0d6a98b1531c7

票数 6
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/29823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档