scala中的递归是非常必要的吗? [英] Is recursion in scala very necessary?

查看:108
本文介绍了scala中的递归是非常必要的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在coursera scala教程中,大多数示例都使用自上而下的迭代。部分地,正如我所看到的,迭代用于避免for / while循环。我来自C ++,对此感到有些困惑。



迭代选择了/ while循环吗?它在生产中是否实用?任何风险的stackoverflow?效率如何?如何自下而上的动态规划(特别是当他们不是尾巴回收)?

另外,我应该使用更少的if条件,而是使用更多的case和子类?

解决方案

真正高质量的Scala将使用非常少的迭代并且只有更多的递归。循环使用较低级的命令式语言通常最好使用高阶combinator,特别是map和flatmap,而且还要过滤,zip,fold,foreach,reduce,collect,partition,scan,groupBy和a好几个人。迭代最好只在性能关键部分完成,而递归只在高阶组合器不太适合的情况下(通常不是尾递归,fwiw)进行。在生产系统的三年编码Scala中,我使用迭代一次,递归两次,每天映射五次。

In the coursera scala tutorial, most examples are using top-down iterations. Partially, as I can see, iterations are used to avoid for/while loops. I'm from C++ and feel a little confused about this.

Is iteration chosen over for/while loops? Is it practical in production? Any risk of stackoverflow? How about efficiency? How about bottom up Dynamic Programming (especially when they are not tail-recusions)?

Also, should I use less "if" conditions, instead use more "case" and subclasses?

解决方案

Truly high-quality Scala will use very little iteration and only slightly more recursion. What would be done with looping in lower-level imperative languages is usually best done with higher-order combinators, map and flatmap most especially, but also filter, zip, fold, foreach, reduce, collect, partition, scan, groupBy, and a good few others. Iteration is best done only in performance critical sections, and recursion done only in a some deep edge cases where the higher-order combinators don't quite fit (which usually aren't tail recursive, fwiw). In three years of coding Scala in production systems, I used iteration once, recursion twice, and map about five times per day.

这篇关于scala中的递归是非常必要的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆