Scala Functional programming

Source: Internet
Author: User

Learned some of Scala's basic grammar http://twitter.github.io/scala_school/zh_cn/index.html,

Try Scala's functional programming.

How to implement with functional programming: SUM (f, a, b) =

s = 0

For i in [A, b]

S+=f (i)

return s

This is the pseudo-code for imperative programming, C/s + +.

1. Implementation 1.

1 def sum (f:int=>int, A:int, b:int): Int = {2     if(a > B) 0 else F (a) + SUM (a+1 , B) 3 }

SUM (x=>x, 1, 5)

SUM (x=>x*x*x, 1, 5)

2. Implementation 2

Here are three parameters, F, a, B. Is it possible to abstract sum into an input of f, and the output is a function of G: (int, int) =>int? And then use the generated G effect to a, B?

1 def sum (f:int=>int):(int, int) =>int = {2   def func (A:int, b:int): int = { 3       if (A > B) 0 else F (a) +func (a+1, b)          4  }    5 }

SUM (x=>x) (1,5)

SUM (x=>x*x*x) (1,5)

The f is abstracted out.

Scala Functional programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.