background preloader


Facebook Twitter

Lihaoyi.github. Val number: P[Int] = P( CharIn('0'to'9').rep(1).!.


Map(_.toInt) ) val parens: P[Int] = P( "(" ~/ addSub ~ ")" ) val factor: P[Int] = P( number | parens ) val divMul: P[Int] = P( factor ~ (CharIn("*/").! Erik's Ponderings: Scala partial functions (without a PhD) If you have done some Scala for a while, you know about pattern matching and match/case.

Erik's Ponderings: Scala partial functions (without a PhD)

Things like: value match { case Some(value) ⇒ … case None ⇒ … } But there is another use of the case keyword, without match, as in: map foreach { case (k, v) ⇒ println(k + " → " + v) } The first time I saw this kind of things I was a bit puzzled: in which situations could case be used without match? There is nothing new with anonymous functions of course, and Scala has a very compact notation for those that doesn’t involve case. But there is more. As expected this crashes, because the pattern match doesn’t know what to do when the string “cat” is passed to it. On the other hand, this example doesn’t crash: So what’s the difference? Now you might wonder, coming from a “normal” programming language background, what it means, for a function to be “partial”. But even though it comes from math it’s actually simple.

An Introduction To Scala Parser Combinators - Part 1: Parser Basics. External DSLs made easy with Scala Parser Combinators. External DSLs are hard since implementing them involves reinventing most of the mechanisms found in a general purpose language.

External DSLs made easy with Scala Parser Combinators

Designing internal DSLs are equally hard, more so in a statically typed language. Dynamically typed languages like Ruby offer strong meta-programming facilities, which help in implementing internal DSLs. But metaprogramming in Ruby is still considered elitist by many, and is not an art mastered by programmers at large. Parser combinators offer a unique value here. They allow programmers to write executable grammars, in the sense that designing and implementing a DSL is almost equivalent to writing the EBNF notations in the syntax of the native language.

The basic idea is to view the operators of BNF (or regular expressions for that matter) as methods that operate on objects representing productions of a grammar. Object Equality. Chapter 28 of Programming in Scala, First EditionObject Equalityby Martin Odersky, Lex Spoon, and Bill VennersDecember 10, 2008 Comparing two values for equality is ubiquitous in programming.

Object Equality

It is also more tricky than it looks at first glance. This chapter looks at object equality in detail and gives some recommendations to consider when you design your own equality tests. 28.1 Equality in Scala [link] As mentioned in Section 11.2, the definition of equality is different in Scala and Java. Scala also has an equality method signifying object identity, but it is not used much. Final def == (that: Any): Boolean = if (null eq this) {null eq that} else {this equals that} 28.2 Writing an equality method [link] How should the equals method be defined? This is problematic, because equality is at the basis of many other things. Var hashSet: Set[C] = new collection.immutable.HashSet hashSet += elem1 hashSet contains elem2 Pitfall #1: Defining equals with the wrong signature.

What went wrong? Simpler way to use JavaFX from Scala. Scala Quick Guide. Scala, short for Scalable Language, is a hybrid functional programming language.

Scala Quick Guide

It was created by Martin Odersky and it was first released in 2003. Scala smoothly integrates features of object-oriented and functional languages and Scala is compiled to run on the Java Virtual Machine. Many existing companies, who depend on Java for business critical applications, are turning to Scala to boost their development productivity, applications scalability and overall reliability. Here is the important list of features, which make Scala a first choice of the application developers.

Scala Regular Expressions. Scala supports regular expressions through Regex class available in the scala.util.matching package.

Scala Regular Expressions

Let us check an example where we will try to find out word Scala from a statement: When the above code is compiled and executed, it produces the following result: Phdoerfler / sbt-fxml. Bitbucket is a code hosting site with unlimited public and private repositories.

phdoerfler / sbt-fxml

We're also free for small teams! Sign up for freeClose sbt-fxml is the missing link for your JavaFX FXML files and your codebase. If you have used the Android IDE or playframework before, you will feel right at home. This is beta software with a not yet stabilized API. Integrating Scala components in a Java application « akquinet-blog. Scala is starting to be really popular, and there are many reasons why you might like to use it in your current projects.

Integrating Scala components in a Java application « akquinet-blog

At akquinet we’re now using Scala inside Java applications to reduce the amount of written code and to benefit from Scala’s flexibility. However, integrating Java and Scala in the same application requires some tricks. Using Java classes in Scala is pretty straightforward; however, using Scala classes in Java is not. Scala has several language features which cannot be directly mapped to Java, for example function types and traits. Here we will describe how these language features are compiled to Java byte code and how to access them from Java afterwards. Unit and Any. Interop Between Java and Scala. 9 Feb 2009 Sometimes, the simplest things are the most difficult to explain.

Interop Between Java and Scala

Scala’s interoperability with Java is completely unparalleled, even including languages like Groovy which tout their tight integration with the JVM’s venerable standard-bearer. What's New in Scala 2.8: Package Objects. ScalazineWhat's New in Scala 2.8: Package Objectsby Martin OderskySeptember 14, 2010 Summary The third installment of a series of articles on the latest Scala release, Scala 2.8, Martin Odersky explains package objects.

What's New in Scala 2.8: Package Objects

Until 2.8, the only things you could put in a package were classes, traits, and standalone objects. These are by far the most common definitions that are placed at the top level of a package, but Scala 2.8 doesn't limit you to just those. Any kind of definition that you can put inside a class, you can also put at the top level of a package. To do so, you put the definitions in a package object. An example is shown in the following listings. Now assume you want to place a variable, planted, and a method, showFruit, directly into package gardening. Scala Standard Library - _root_ Scala Documentation - Scala Documentation. Scala 2.8 Collections API. Programming in Scala, First Edition. Programming in Scala, First Editionby Martin Odersky, Lex Spoon, and Bill VennersDecember 10, 2008 Martin Odersky made a huge impact on the Java world with his design of the Pizza language. Although Pizza itself never became popular, it demonstrated that object-oriented and functional language features, when combined with skill and taste, form a natural and powerful combination.

Pizza's design became the basis for generics in Java, and Martin's GJ (Generic Java) compiler was Sun Microsystem's standard compiler starting in 1.3 (though with generics disabled). I had the pleasure of maintaining this compiler for a number of years, so I can report from first-hand experience that Martin's skill in language design extends to language implementation. Since that time, we at Sun tried to simplify program development by extending the language with piecemeal solutions to particular problems, like the for-each loop, enums, and autoboxing. Scala IDE for Eclipse Space. The exquisite flavor of Scala development.