如何开始使用 Scala

发布于 2024-10-25 00:38:33 字数 1539 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

心房敞 2024-11-01 00:38:33

您可能会通过访问 Simply Scala 获得第一印象,那里有在线解释器。

绝对经典的是 Scala for Java Refugees,它最初是为来自以下国家的人编写的: Java,但考虑到 C#/Java 的基础知识非常相似,这对您很有帮助。

您不需要先学习 Java,但需要安装并运行 Java 运行时/开发工具包。

然后转到 http://www.scala-lang.org/downloads 并下载适合您的操作系统的软件包(我总是更喜欢 Scala 的夜间构建,它们比最新的稳定版本有更多的错误修复)。

之后,运行 Scala REPL,这基本上是“Simply Scala离线”(Simply Scala也在幕后使用Scala REPL)。甚至许多 Java 程序员首先使用 Scala REPL 来构建原型。

如果您更喜欢学习书籍,我可以推荐 Martin Odersky 的Programming in Scala(第 2 版)(如果您从一门语言开始)设计观点和想要的“参考书”)。还有其他一些书,比如《Scala 编程》,可以说更适合初学者,但我个人觉得《Scala 编程》非常好,并且仅通过那本书就学会了 Scala。

启动 Scala 的一个好方法是使用集合类。 .NET 最近通过 LINQ 和扩展方法添加了类似的内容,因此您可以轻松上手。

一个帮助您入门的小例子:

//Define a class with some properties
case class Person(name: String, var age: Int, spokenLanguages: String*)

//Create some persons
val joe = Person("Joe", 42, "English","French","Danish")
val doe = Person("Doe", 23, "English","German")
val don = Person("Don", 11, "Italian","French","Polish")
val bob = Person("Bob", 17, "German")

//Access a property
joe.name

//Don had his 12th birthday!
don.age = 12

//Put the persons into a list
val persons = List(joe, doe, don, bob)

//Divide the list into minors and adults
val (minors, adults) = persons.partition(_.age < 18)
//Get the total age of all persons
val personsTotalAge  = persons.map(_.age).sum
//Return a list with only those speaking English
val englishSpeakers  = persons.filter(_.spokenLanguages.contains("English"))
//Same as the example above.
val englishSpeakers2 = 
  for{ person   <- persons
       language <- person.spokenLanguages 
       if language == "English"
  } yield person

我不太精通 C#,但我相信很多事情可能与您相似。

Scala XML 支持的一些示例:

//The shoppingCart for breakfast
val shoppingCart = <list>
                     <item><name>Tomatoes</name><price>0.30</price><amount>4</amount></item>
                     <item><name>Eggs</name><price>0.15</price><amount>10</amount></item>
                     <item><name>Bread</name><price>2.20</price><amount>1</amount></item>
                   </list>

//How much does it cost?
val total = (shoppingCart \ "item").map(i => (i \ "price").text.toDouble * (i \ "amount").text.toDouble).sum

//This is a Symbol
val sym = 'SomeSymbol

//I'm too lazy to use Strings for XML! (Example for implicits)
implicit def symbol2string(symbol: Symbol) = symbol.name

//Now I can use Symbols too!
val total = (shoppingCart \ 'item).map(i => (i \ 'price).text.toDouble * (i \ 'amount).text.toDouble).sum

You might gain a first impression by visiting Simply Scala where you have an online interpreter available.

An absolute classic is Scala for Java Refugees which was originally written for people coming from Java, but will be quite helpful for you, considering how similar the basics of C#/Java are.

You don't need to learn Java first , but you need to have the Java runtime/development kit installed and working.

Then go to http://www.scala-lang.org/downloads and download the appropriate package for your operating system (I always prefer the nightly builds of Scala, they have more bug-fixes than the latest stable one).

After that, run the Scala REPL which is basically "Simply Scala offline" (Simply Scala uses the Scala REPL behind the covers, too). Even many Java programmers use the Scala REPL to prototype things first.

If you prefer books to learn I can recommend Programming in Scala (2nd edition) by Martin Odersky (if you start from a language design point of view and want the "reference book"). There are others like "Programming Scala" which are more targeted at beginners so to speak, but personally I found "Programming in Scala" excellent and have learned Scala with just that book.

A nice way to start Scala is working with the collection classes. .NET has added something similar lately with LINQ and extension methods, so it will be easy to pick up for you.

A small example to get you started:

//Define a class with some properties
case class Person(name: String, var age: Int, spokenLanguages: String*)

//Create some persons
val joe = Person("Joe", 42, "English","French","Danish")
val doe = Person("Doe", 23, "English","German")
val don = Person("Don", 11, "Italian","French","Polish")
val bob = Person("Bob", 17, "German")

//Access a property
joe.name

//Don had his 12th birthday!
don.age = 12

//Put the persons into a list
val persons = List(joe, doe, don, bob)

//Divide the list into minors and adults
val (minors, adults) = persons.partition(_.age < 18)
//Get the total age of all persons
val personsTotalAge  = persons.map(_.age).sum
//Return a list with only those speaking English
val englishSpeakers  = persons.filter(_.spokenLanguages.contains("English"))
//Same as the example above.
val englishSpeakers2 = 
  for{ person   <- persons
       language <- person.spokenLanguages 
       if language == "English"
  } yield person

I'm not that fluent in C#, but I believe many things might look similar to you.

Some examples of Scala's XML support:

//The shoppingCart for breakfast
val shoppingCart = <list>
                     <item><name>Tomatoes</name><price>0.30</price><amount>4</amount></item>
                     <item><name>Eggs</name><price>0.15</price><amount>10</amount></item>
                     <item><name>Bread</name><price>2.20</price><amount>1</amount></item>
                   </list>

//How much does it cost?
val total = (shoppingCart \ "item").map(i => (i \ "price").text.toDouble * (i \ "amount").text.toDouble).sum

//This is a Symbol
val sym = 'SomeSymbol

//I'm too lazy to use Strings for XML! (Example for implicits)
implicit def symbol2string(symbol: Symbol) = symbol.name

//Now I can use Symbols too!
val total = (shoppingCart \ 'item).map(i => (i \ 'price).text.toDouble * (i \ 'amount).text.toDouble).sum
萌面超妹 2024-11-01 00:38:33

您不需要先学习 Java。您熟悉函数式编程吗?如果是的话,您应该能够快速投入。不管怎样,这里有一些关于如何学习 Scala 的想法:

  • 获取一本好的参考书。我推荐 Odersky、Spoon 和 Venners 编写的《Scala 编程》。我发现它是最全面的 Scala 书籍之一。

  • 与学习任何新语言一样,尝试使用 Scala 编写几个小型应用程序。如果您不是函数式程序员,您可能会使用不同的范例进行编程,但现在没关系。尝试编写程序时不使用“var,(使用 val 代替)”,不使用循环,并最大限度地减少整体状态变化。

  • 使用 sbt 构建您的程序。我有点犹豫是否推荐这个,因为你必须学习一种新工具来编写你的程序。但我发现用它来编写 Scala 应用程序也很棒。看起来很多 Scala 项目都使用 sbt。

  • 另请查看此评论和该线程,以帮助您过渡到 Scala。 与 Java 迁移时形成的习惯作斗争到 Scala

You don't need to learn Java first. Are you familiar with functional programming? If you are, you should be able to jump in quite fast. Anyway, here are some thoughts on how you can learn Scala:

  • Get a good reference book. I recommend Programming In Scala by Odersky, Spoon, and Venners. I find it as one of the most comprehensive Scala books.

  • As with learning any new language, try writing several small application using Scala. If you're not a functional programmer, you might program it in a different paradigm, but that's okay for now. Try writing your program without using "var, (use val instead)" not using loops, and minimizing state change overall.

  • Use sbt to build your program. I'm kinda hesitant to recommend this since you have to learn a new tool to write your program. But I find it a great too to write Scala apps with. And many Scala projects use sbt it seems like.

  • Also check out this comment and that thread overall to help you transition to Scala. Struggle against habits formed by Java when migrating to Scala

月下凄凉 2024-11-01 00:38:33

Java 作为一种语言不需要从 scala 开始(无论如何 java 本身与 c# 非常相似,或者实际上是相反的......)。
然而,一旦你开始使用 scala 做富有成效的事情,你将与许多 java 库进行交互,并了解到 java 世界是一个比 .net 世界更广阔的星系,其中有很多或多或少的标准库,其中有很多您需要的东西直接位于标准 .NET 库中。您可以边学习边学习它们,但如果没有 Java 背景,这种体验可能会让人感到不知所措。不过,如果你开始学习 java,情况会是一样的......
您可能需要学习的其他 Java 特定内容包括泛型在 JVM 中的功能要弱得多,以及 scala 如何尝试解决这个问题。

至于 Scala 本身作为一种语言,来自 .NET,阅读一些有关函数式编程的内容可能比学习 Java 更能让你受益匪浅。函数范式是我在最初接触 scala 的过程中最无知的部分,这给我在理解 scala 网站上的资源中的示例代码时带来了最大的麻烦。

建立基础

  • 对于函数式编程,我建议阅读 SICP(在线且免费)。
  • 当你学习 Java 时,尝试看看 F#。这是一门很好的语言,有详细的文档记录,并且“生活”在 .NET 生态系统中

学习 scala

Java as a language will not be necessary to start with scala (and anyway java itself is very similar to c#, or actually it's the other way around...).
Once you start doing productive things with scala, though, you will be interacting with a lot of java libraries and learn that java-world is a much broader galaxy of more-or-less standard libraries than .net-world where a lots of the things you need are directly in the standard .NET libraries. You can learn them as you go, but not coming from a java background, the experience might feel overwhelming. It would be the same thing had you started learning java, though....
Other java-specific things you may have to learn are about generics being much less powerful in the JVM and how scala tries to work around this.

As for scala itself as a language, coming from .NET, you may benefit more from reading a few things on functional programming than from learning java. The functional paradigm is the part where I was the most ignorant in my initial approach to scala and that caused me the most trouble in understanding example code from the resources you can find on the scala website.

Building a foundation

  • For functional programming I would recommend reading SICP (it's online and free).
  • While you learn java try to take a look at F#. It's a good language, it's well documented and "lives" in the .NET ecosystem

learning scala

山有枢 2024-11-01 00:38:33

如果您想学习 Scala,那么通过学习 Scala 并随手学习您需要的 Java,您会得到更好的服务。您无需学习 Java 即可开始使用 Scala。

一个好的起点是阅读这个问题,其中列出了当前可用的大多数 Scala 书籍。

https://stackoverflow.com/questions/3359852/scala-programming-book/3360308#3360308< /a>

If you want to learn Scala, you'll be much better served by learning Scala and picking up the Java you need as you go. You don't need to learn Java to be able to start using Scala.

A good place to start would be reading through this question, which lists most of the Scala books currently available.

https://stackoverflow.com/questions/3359852/scala-programming-book/3360308#3360308

﹏雨一样淡蓝的深情 2024-11-01 00:38:33

我认为您不需要了解 Java 即可开始使用 Scala。不过,了解大致情况还是很有帮助的,因为我读过的大多数 Scala 文档都提到了 Java 功能或错误。

关于开源项目,您可以查看 Scala 项目的 Github。不过,大多数开源 Scala 项目往往都是框架。

关于书籍,我发现 Artima 的《Scala 编程》和《实用程序员》的《Scala 编程》都非常好。

I don't think you need to know Java to get started on Scala. It's helpful to know the broad strokes though, because most Scala documentation I have read refers back to Java features or bugs.

Regarding Open Source projects, you cna have a look on Github for scala projects. Most open source Scala projects tend to be frameworks, though.

Regarding books, I found both the Artima book, Programming in Scala, and the Pragmatic Programmers, Programming Scala, very good.

弃爱 2024-11-01 00:38:33

除此之外:写一段时间简单程序,完全忽略Scala的高级功能。只有当您很好地掌握了基础知识、功能范例和类型系统时,才可以迁移到那里。

In addition to the others: Write simple programs for a while and completely ignore advanced features of Scala. Only move there when you have a good grasp of the basics, the functional paradigm und the type system.

绿萝 2024-11-01 00:38:33

我建议您参加 Martin Odersky 所著的 Coursera 中的 Scala 函数式编程原理。有视频讲座、作业,甚至期末考试。

I advise you to take part in Coursera Functional Programming Principles in Scala by Martin Odersky. There are video lectures, assignments and even final exam.

樱花细雨 2024-11-01 00:38:33

Twitter(Scala 爱好者之一)最近推出了他们自己的 scala 完整教程 - Scala School。这是一本很棒的指南,我推荐给所有 Scala 初学者。

Scala 学校是作为 Twitter 上的一系列讲座开始的,旨在为
经验丰富的工程师成为高效的 Scala 程序员。 ... 我们
认为 Scala 教学最有意义,而不是像
它是一种改进的 Java,但作为一种新语言。没有 Java 经验
预期的。重点将围绕口译员和
对象函数风格以及我们的编程风格
这里。重点将放在可维护性、清晰性上
表达式,并利用类型系统。

大多数课程除了 Scala REPL 之外不需要其他软件。这
鼓励读者跟随,并走得更远!使用这些
课程作为探索该语言的起点。

Twitter (one of the scala lovers) recently unveiled their own scala complete tutorial -- Scala School. It is awesome guide which I recommend to all scala beginners.

Scala school was started as a series of lectures at Twitter to prepare
experienced engineers to be productive Scala programmers. ... We
think it makes the most sense to approach teaching Scala not as if
it's an improved Java but as a new language. Experience in Java is not
expected. Focus will be around the interpreter and the
object-functional style as well as the style of programming we do
here. An emphasis will be placed on maintainability, clarity of
expression, and leveraging the type system.

Most of the lessons require no software other than a Scala REPL. The
reader is encouraged to follow along, and go further! Use these
lessons as a starting point to explore the language.

吻泪 2024-11-01 00:38:33

我目前正在开发一种不需要事先编程知识的产品。它将展示函数式编程和命令式编程相结合的优势,并且非常简单易懂。此外,这些帖子将涵盖最佳实践并使用 Scala 解决日益困难的问题。

http://vigtig.it/blog/

你应该尝试一下,第 2 部分即将完成:)

I'm currently developing one which doesn't require prior programming knowledge. It will show the strength of combining functional and imperative programming, and it is very simple to follow. Furthermore, the posts will cover best practice and solve increasingly difficult problems using Scala.

http://vigtig.it/blog/

You should give it a try, part 2 is almost finished :)

烟雨扶苏 2024-11-01 00:38:33

下载并安装 Apache Maven。使用它创建一个空白的 Scala 项目并编写经典的 Scala hello world 应用程序。这将要求您在 Maven 创建的项目目录中配置 pom.xml 文件。

然后一遍又一遍地运行mvncompile,纠正错误直到编译。

然后运行 ​​mvn package 直到通过所有单元测试。

最后,运行 mvn scala:run

一旦你构建了一个真正的项目,scala:run 就会有一个选项来输出从 shell 脚本或批处理文件运行它所需的完整 Java 命令。

Download and install Apache Maven. Use it to create a blank Scala project and write the classic Scala hello world application. This will require you to configure the pom.xml file in the project directory that Maven creates.

Then run mvn compile over and over, correcting errors until it compiles.

Then run mvn package until you pass all the unittests.

And finally, run mvn scala:run

Once you build a real project scala:run has an option to spit out the full Java command needed to run it from a shell script or batch file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文