Planet Scala

Scala blogs aggregated

June 14, 2013

Functional Jobs

Web Developer at Atlassian (Full-time)

The Atlassian Marketplace launched a year ago and has helped hundreds of third-party developers build thriving businesses on the Atlassian platform. We're one of the newest and fastest growing products in the Atlassian family, and our development team in SF is responsible for building that platform -- from the add-on framework to the purchasing process. We're looking for a multi-lingual web developer who's an expert at JavaScript, but also has the technical chops to master new languages and navigate the entire stack. Today, you have the proficiency to kick out a beautiful single-page web app backed by a Roy Fielding-approved REST API with the tools of your choice.

Our team embraces functional programming, and our service backend is written in Scala. You don't need to know Scala, but you should be functionally-curious and excited to learn.

What you'll do

  • Deliver new features to a rapidly evolving service, with daily deployments and real-time feedback from users.
  • Work with a team of talented developers and designers to build simple, beautiful and consistent user experiences for our application.
  • Drive the implementation of new technologies to improve our ability to build a great product.

What we'd like to see

  • Experience building and maintaining front-end code for dynamic, modern web applications.
  • Hands-on experience with current web technologies.
  • Excellent understanding of JavaScript.
  • Familiarity with JavaScript libraries and frameworks like jQuery,
  • RequireJS, Raphaël, Backbone.js, bacon.js, node, and underscore.js
  • Solid software design skills and a love for automated testing.
  • Passion for usability, simplicity and consistency in user interface design.
  • Excellent communication and collaboration skills.
  • A burning desire to read and understand at least one monad tutorial.
  • Above all, intellectual curiosity and willingness to learn new languages, new paradigms and new approaches to building web applications.

Get information on how to apply for this position.

June 14, 2013 08:17 PM

June 08, 2013

Eric Torreborre

Specs2 2.0 - Interpolated - RC2

<status class="ok">

This is a quick update to present the main differences with specs2 2.0-RC1. I have been fixing a few bugs but more importantly I have:

  • made the Tags trait part of the standard specification
  • removed some arguments for reporting and made the formatting of specifications more granular

This all started with an issue on Github...

Formatting

Creating reports for specifications is a bit tricky. On one hand you hand different possible "styles" for the specifications: "old" acceptance style (with the ^ operator), "new" acceptance style (with interpolated strings), "unit" style... Then, on the other hand, you want to report the results in the console, where information is logged on a line-by-line base and in HTML files, where newlines, whitespace and indentation all needs great care.

I don't think I got it quite right yet, especially for HTML, but working on issue #162 forced me to make specs2 implementation and API a bit more flexible. In particular, in specs2 < 2.0, you could set some arguments to control the display of the specification in the console and/or HTML. For example noindent is a Specification argument saying that you don't want the automatic indentation of text and examples. And markdown = false means that you don't want text to be parsed as Markdown before being rendered to HTML.

However issue 162 shows that setting formatting properties at the level of the whole specification doesn't play well with other features like specification inclusion. I decided to fix this issue by using an existing specs2 feature: tags.

Tags and Specification

Tags in specs2 are different from tags you can find in other testing libraries. Not only you can tag single examples but you can also mark a full section of a specification with some tags. We can use this capability to select specific parts of a specification for execution but we can also use it to direct the formatting of the specification text. For example you can now write:

</status><status class="ok">

class MySpec extends Specification { def is = s2""" ${formatSection(verbatim = false)}
This text uses Markdown when printed to html, however if some text is indented with 4 spaces
it should *not* be rendered as a code block because `verbatim` is false.

"""
}

Given the versatile use of tags now, I decided to include the Tags trait, by default, in the Specification class. I resisted doing that in the past because I didn't want to encumber too much the Specification namespace with something that was rarely used by some users. Which leads me to the following tip on how to use the Specification class:

  • when starting a new project or prototyping some code, use the Specification class directly with all inherited features

  • when making your project more robust and production-like, create your own Spec trait, generally inheriting from the BaseSpecification class for basic features, and mix in only the traits you think you will generally use

This should give you more flexibility and choice over which specs2 feature you want to use with a minimal cost in terms of namespace footprint and compile times (because each new implicit you bring in might have an impact in terms of performances)

API changes

The consequence of this evolution is yet another API break:

  • the Text and Example classes now use a FormattedString class containing the necessary parameters to display that string as HTML or in the console
  • for implementation reasons I have actually changed the constructor parameters of all Fragment classes to avoid storing state as private variables
  • the noindent, markdown arguments are now gone (you need to replace them with ${formatSection(flow=true)} and ${formatSection(markdown=true)}, see below)
  • the Tags trait is mixed in the Specification class so if you had methods like def tag you might get conflicts

And there are now 2 methods formatSection(flow: Boolean, markdown: Boolean, verbatim: Boolean) and formatTag(flow: Boolean, markdown: Boolean, verbatim: Boolean) to tag specification fragments with the following parameters:

  • flow: the fragment (Text or Example) shouldn't be reported with automatic indenting (default = false, set automatically to true when using s2 interpolated strings)
  • markdown: the fragment is using Markdown (default = true)
  • verbatim: indented text with more than 4 spaces must be rendered as a code block (default = true but can be set to false to solve #162)

HTML reports

I'm currently thinking that I should try out a brand new way of translating an executed specification with interpolated text into HTML. My first attempts were not completely successful and I find it hard to preserve the original layout of the specification text, especially with the Markdown translation in the middle. Yet, I must say a word on the Markdown library I'm using, Pegdown. I found this library extremely easy to adapt for my current needs (to implement the verbatim = false option) and I send my kudos to Mathias for such a great job.


This is it. Download RC2, use it and provide feedback as usual, thanks!

</status>

by Eric (noreply@blogger.com) at June 08, 2013 12:15 AM

June 07, 2013

Coderspiel

Why The NSA Collecting Your Phone Records Is A Problem | Cato @ Liberty

Why The NSA Collecting Your Phone Records Is A Problem | Cato @ Liberty:

Some stress that what is being collected is “just metadata”—a phrase I’m confident you’ll never see a computer scientist or data analyst use.

Yeah no I would just call it “data”.

Metadata is a fine concept when we are talking about the structure of data. It is meaningless jargon when we are talking about an ongoing, secret, general data transfer from private corporations to government.

Let’s say I follow you to work for a week and record your routes on a map. Am I a stalker recording your habits, or a harmless guy recording “just metadata” about your footsteps? I mean, I didn’t take a stencil of each footprint and reproduce the street scene in my underground lair. What’s the big deal?

Everybody knows I could follow you to work and record your route. What’s the difference if I actually do?

The data about the endpoints of communications — the identifiers, time stamps, and locations — just happens to be the most useful and practical data for the NSA to have collected whenever this started — another detail that is too secret for the funding public to know about.

But the trajectory that the agency is on, in particular their/our $2 billion investment in the Utah data center, suggests that an “upgrade” of this program to store the entirety of our communications is just a matter of time.

Unless?

June 07, 2013 02:09 PM

June 06, 2013

scala-lang.org

Scala 2.10.2 is now available!

We are very happy to announce the final release of Scala 2.10.2!

The Scala team and contributors fixed 95 issues since 2.10.1!

In total, 164 RC1 pull requests and 7 RC2 pull requests were opened on GitHub, of which 140 were merged after having been tested and reviewed.

by moors at June 06, 2013 09:22 PM

June 03, 2013

Ruminations of a Programmer

Endo is the new fluent API

I tweeted this over the weekend .. <script async="async" charset="utf-8" src="http://platform.twitter.com/widgets.js"></script> My last two blog posts have been about endomorphisms and how it combines with the other functional structures to help you write expressive and composable code. In A DSL with an Endo - monoids for free, endos play with Writer monad and implement a DSL for a sequence of activities through monoidal composition. And in An exercise in Refactoring - Playing around with Monoids and Endomorphisms, I discuss a refactoring exercise that exploits the monoid of an endo to make composition easier. Endomorphisms help you lift your computation into a data type that gives you an instance of a monoid. And the mappend operation of the monoid is the function composition. Hence once you have the Endo for your type defined, you get a nice declarative syntax for the operations that you want to compose, resulting in a fluent API. Just a quick recap .. endomorphisms are functions that map a type on to itself and offer composition over monoids. Given an endomorphism we can define an implicit monoid instance ..
implicit def endoInstance[A]: Monoid[Endo[A]] = new Monoid[Endo[A]] {
def append(f1: Endo[A], f2: => Endo[A]) = f1 compose f2
def zero = Endo.idEndo
}
I am not going into the details of this, which I discussed at length in my earlier posts. In this article I will sum up with yet another use case for making fluent APIs using the monoid instance of an Endo. Consider an example from the domain of securities trading, where a security trade goes through a sequence of transformations in its lifecycle through the trading process .. Here's a typical Trade model (very very trivialified for demonstration) ..
sealed trait Instrument
case class Security(isin: String, name: String) extends Instrument

case class Trade(refNo: String, tradeDate: Date, valueDate: Option[Date] = None,
ins: Instrument, principal: BigDecimal, net: Option[BigDecimal] = None,
status: TradeStatus = CREATED)
Modeling a typical lifecycle of a trade is complex. But for illustration, let's consider these simple ones which need to executed on a trade in sequence ..
  1. Validate the trade
  2. Assign value date to the trade, which will ideally be the settlement date
  3. Enrich the trade with tax/fees and net trade value
  4. Journalize the trade in books
Each of the functions take a Trade and return a copy of the Trade with some attributes modified. A naive way of doing that will be as follows ..
def validate(t: Trade): Trade = //..

def addValueDate(t: Trade): Trade = //..

def enrich(t: Trade): Trade = //..

def journalize(t: Trade): Trade = //..
and invoke these methods in sequence while modeling the lifecycle. Instead we try to make it more composable and lift the function Trade => Trade within the Endo ..
type TradeLifecycle = Endo[Trade]
and here's the implementation ..
// validate the trade: business logic elided
def validate: TradeLifecycle =
((t: Trade) => t.copy(status = VALIDATED)).endo

// add value date to the trade (for settlement)
def addValueDate: TradeLifecycle =
((t: Trade) => t.copy(valueDate = Some(t.tradeDate), status = VALUE_DATE_ADDED)).endo

// enrich the trade: add taxes and compute net value: business logic elided
def enrich: TradeLifecycle =
((t: Trade) => t.copy(net = Some(t.principal + 100), status = ENRICHED)).endo

// journalize the trade into book: business logic elided
def journalize: TradeLifecycle =
((t: Trade) => t.copy(status = FINALIZED)).endo
Now endo has an instance of Monoid defined by scalaz and the mappend of Endo is function composition .. Hence here's our lifecycle model using the holy monoid of endo ..
def doTrade(t: Trade) =
(journalize |+| enrich |+| addValueDate |+| validate).apply(t)
It's almost the specification that we listed above in numbered bullets. Note the inside out sequence that's required for the composition to take place in proper order.

Why not plain old composition ?


A valid question. The reason - abstraction. Abstracting the composition within types helps you compose the result with other types, as we saw in my earlier blog posts. In one of them we built larger abstractions using the Writer monad with Endo and in the other we used the mzero of the monoid as a fallback during composition thereby avoiding any special case branch statements.

One size doesn't fit all ..


The endo and its monoid compose beautifully and gives us a domain friendly syntax that expresses the business functionality ina nice succinct way. But it's not a pattern which you can apply everywhere where you need to compose a bunch of domain behaviors. Like every idiom, it has its shortcomings and you need different sets of solutions in your repertoire. For example the above solution doesn't handle any of the domain exceptions - what if the validation fails ? With the above strategy the only way you can handle this situation is to throw exceptions from validate function. But exceptions are side-effects and in functional programming there are more cleaner ways to tame the evil. And for that you need different patterns in practice. More on that in subsequent posts ..

by Debasish Ghosh (noreply@blogger.com) at June 03, 2013 06:40 AM

June 02, 2013

Kevin Albrecht

Value Attributes Don't Always Mean What You Think They Mean

I recently discovered the hard way that the "value" attributes of different HTML elements can have subtle and frustratingly different meanings.

Take a look at this simple example:
<input type="button" value="one" id="buttonX">
<input type="text" value="one" id="textX">

<script>
var buttonX = document.getElementById("buttonX");
var textX = document.getElementById("textX");

buttonX.value = "two";
textX.value = "two";

buttonX.setAttribute("value","three");
textX.setAttribute("value","three");
</script>
(View this as a JSFiddle: http://jsfiddle.net/sEpMz/)

After running this code, you would probably guess that the text visible in the two widgets would both be "three", but in fact, the button will display "three" while the text box will display "two"!

The reason behind this is that for certain input element types, the "value" attribute defines the initial text of the widget, while for other element types, the "value" attribute defines the visible text of the widget!

The important distinction here is between the "value" attribute (the value="something" that appears in the HTML), and the "value" property (elementX.value = 'something' in JavaScript). The property will always reflect what is displayed in the widget on the page, while the attribute has different behavior depending on the type of the input element.

by Kevin Albrecht (noreply@blogger.com) at June 02, 2013 08:56 AM

May 31, 2013

scala-lang.org

Scala 2.10.2-RC2 is now available!

We are very happy to announce the RC2 release of Scala 2.10.2! If no serious blocking issues are found this will become the final 2.10.2 version.

The Scala team and contributors fixed 95 issues since 2.10.1!

In total, 164 RC1 pull requests and 7 RC2 pull requests were opened on GitHub, of which 140 were merged after having been tested and reviewed.

by moors at May 31, 2013 04:51 PM

May 29, 2013

scala-lang.org

Scala 2.11.0-M3 is now available!

We are pleased to announce the next milestone release of Scala 2.11.0, which is now available for download!

This is a pre-release software. You can see our plans for upcoming Scala releases on our Roadmap. For production use, we recommend the latest stable release, 2.10.1.

The Scala team and contributors fixed 108 issues, in addition to those fixed in the upcoming 2.10.2, which are also included in this release.

Please give 2.11.0-M3 a spin! This release is not binary compatible with the 2.10.x series, so you will need to obtain builds of your dependencies. Once we start the release candidates, we will coordinate with the open source community to release these simultaneously, but for these milestones we are not asking library authors to go to that trouble.

by moors at May 29, 2013 04:29 PM

Paul Chiusano

The future of software, the end of apps, and why UX designers should care about type theory

The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination. Few media of creation are so flexible, so easy to polish and rework, so readily capable of realizing grand conceptual structures... Yet the program construct, unlike the poet's words, is real in the sense that it moves and works, producing visible outputs separate from the construct itself. […] The magic of myth and legend has come true in our time. One types the correct incantation on a keyboard, and a display screen comes to life, showing things that never were nor could be. 
Fred Brooks
Have you noticed how applications accrete feature after feature but never seem quite capable of doing everything we want? Software is a profound technology with enormous potential, and we stifle this potential with an antiquated metaphor. That metaphor is the machine. Software is now organized into static machines called applications. These applications ("appliances" is a better word) come equipped with a fixed vocabulary of actions, speak no common language, and cannot be extended, composed, or combined with other applications except with enormous friction. By analogy, what we have is a railway system where the tracks in each region are of differing widths, forcing trains and their cargo to be totally disassembled and then reassembled to transport anything across the country. As ridiculous as this sounds, this is roughly what we do at application boundaries: write explicit serialization and parsing code and lots of tedious (not to mention inefficient) code to deconstruct and reconstruct application data and functions.

This essay is a call to cast aside the broken machine metaphor and ultimately end the tyranny of applications. Applications can and ultimately should be replaced by programming environments, explicitly recognized as such, in which the user interactively creates, executes, inspects and composes programs. In this model, interaction with the computer is fundamentally an act of creation, the creative act of programming, of assembling language to express ideas, access information, and automate tasks. And software presents an opportunity to help humanity harness and channel "our vast imaginations, humming away, charged with creative energy".

Though the machine metaphor is wrong for software, it's also understandable why it's persisted. Before the discovery of software, arguably in the 1930s with Alan Turing's invention of the universal Turing machine, human technology had produced only physical artifacts like cash registers, engines, and light bulbs, built for some particular purpose and equipped with a largely fixed vocabulary of actions. With software came the idea that behavior and functionality could be specified as pure information, independent of the machine which interprets them. This raised novel possibilities. As pure information, a program is infinitely copyable at near zero cost, and in the internet age, capable of being transported anywhere on the planet almost instantaneously. A programmer can now miraculously turn thoughts to reality and deploy them around the globe by typing on a keyboard and clicking a few buttons. Though our mindset hasn't caught up yet, software relegated the machine (which once held primacy for the artifacts and technology produced by civilization) to an implementation detail, a substrate for the real technology--the specification of behavior in the form of a program.

We artificially limit the potential of this incredible technology by reserving a tiny, select group of people (programmers) to use its power build applications with largely fixed sets of actions (and we now put these machines on the internet too and call them "web applications"), and whose behaviors are not composable with other programs. Software let us escape the tyranny of the machine, yet we keep using it to build more prisons for our data and functionality!
Bob: Now, wait a minute. Applications usually have an API too, you know. If you really want programmability, why not just use the API? 
Alice: I wouldn't say 'usually', but okay, in theory let's suppose that's true. In practice, even though I'm a programmer and could in principle customize the applications I use, I don't because of the friction of doing so. Each application is a universe unto its own, with its own language and idiosyncratic modes of interaction. The situation hasn't improved with web applications, which have somewhat converged on ad hoc JSON+REST protocols as the lingua franca of application programmability. 
Bob: What's wrong with that? There are JSON parsers for every programming language under the sun! I even wrote a really fast, push-based, nonblocking parser in 5,000 lines of Java! It's pretty awesome. Check out how I optimized the parsing by hand-coding a switch-statement-based state machine for the parse table to reduce allocation rates and improve cache loc-- 
Alice: You're missing my point! Compare the overhead of calling a function in the 'native' language of your program vs calling a function exposed via JSON+REST. And no I don't mean the computational overhead, though that is a problem too. Within the language of your program, if I want to call a function returning a list of (employee, date) pairs, I simply invoke the function and get back the result. With JSON+REST, I get back a blob of text, which I then have to parse into a syntax tree and then convert back to some meaningful business objects. If I had that overhead and tedium for every function call I made I'd have quit programming long ago. 
Bob: Are you just saying you want more built in types for JSON, then? That's easy, I hear there's even a proposal to add a date type to JSON. 
Alice: And maybe in another fifteen years JSON will grow a standard for sending algebraic data types (they've been around for like 40 years, you know) and other sorts of values, like you know, functions
Bob: Functions?? Are you serious? You aren't talking about sending functions across the internet and just executing them, that's a huge security liability! 
Alice: Nevermind that for now. My point is-- 
Bob: --now wait a minute! You know, I was humoring you earlier by saying if you wanted programmability you could just use the application's API. Okay, for the sake of argument I'll grant that this can be rather inconvenient. But so what? You and I both know that 99.9% of users don't want to program or customize; they are perfectly happy with applications that do one thing, and do it well. 
Alice: I wouldn't say 'perfectly happy', I'd say that users are resigned to the notion that applications are machines with a fixed set of actions, and any limitations of these machines must simply be worked around via whatever tedium is required. But of course they would think that--we've never shown our users software that didn't work just like a machine, so how could we expect them to know about the wonderful, customizable universe of possibilities that we programmers get to play in every day? This isn't a good state of affairs, it's sad, and we ought to start doing something about it! It isn't hopeless--in fact, I find that if you get users in the right mindset they are positively incessant about wanting to customize their user experience and the actions supported by an application. It's human nature, our inner spirit of creativity and invention that can never be truly squelched! When we are shown something of use or interest to us, some piece of functionality or data, we begin thinking up possible variations and combinations that also interest us or seem useful. 
Bob: Okay, but let's be realistic. Do you really expect your users to be booting up text editors, running compilers, interpreting syntax and type errors and so forth just to get something accomplished? 
Alice: Of course not--no user should have to put up with the arcane programming environments that we professional programmers have to endure on a daily basis. Then again, we shouldn't have to either! Which is why the goal of software should not be to build machines, but to build pleasing, accessible programming environments that delight and inspire our users to creation while facilitating the sharing and reuse of programming ideas! Yes, we can and should optimize these environments for programming in various domains, which could include graphical views and so forth, but we should still place these environments in a unified framework rather than in walled gardens of functionality like the current batch of (web) appliances... er, 'applications'. 
Bob: So what are you saying? Get rid of Microsoft Word, Outlook, Gmail, Twitter, Facebook, and all the rest? 
Alice: Yes! Or rather, I would deconstruct these applications into libraries and grant users access to the functions and data types of these libraries within a grand unified programming environment. 
Bob: I want to talk more about that... but in any case, these applications you deride aren't just libraries, they are providing an intuitive interface to functionality that people find valuable, and we are going to need some sort of interface to this functionality that's better than a text editor and the command line. Providing this better interface is what applications do
Alice: If the interfaces provided by these applications are so intuitive, why are there rows and rows of 'Missing Manual' and 'For Dummies' books covering just about every application under the sun? Applications are failing at even their stated goal, but they do worse than that. Yes, an application is an (often terrible) interface to some library of functions, but it also traps this wonderful collection of potential building blocks in a mess of bureaucratic red tape. Any creator wishing to build atop or extend the functionality of an application faces a mountain of idiosyncratic protocols and data representations and some of the most tedious sort of programming imaginable: parsing, serializing, converting between different data representations, and error handling due to the inherent problem of having to pass through a dynamically typed and insufficiently expressive communication channel! And that's if an application even exposes any significant portion of its functionality through an actual API, which they often don't. We can do so much better! 
Bob: All right, I'll bite. Let's hear your story for how to organize the computing world without applications. 
Alice: I'm glad you asked...

The world without applications

The 'software as machine' view is so ingrained in people's thinking that it's hard to imagine organizing computing without some notion of applications. But let's return to first principles. Why do people use computers? People use computers in order to do and express things, to communicate with each other, to create, and to experience and interact with what others have created. People write essays, create illustrations, organize and edit photographs, send messages to friends, play card games, watch movies, comment on news articles, and they do serious work too--analyze portfolios, create budgets and track expenses, find plane flights and hotels, automate tasks, and so on. But what is important, what truly matters to people is simply being able to perform these actions. That each of these actions presently take place in the context of some 'application' is not in any way essential. In fact, I hope you can start to see how unnatural it is that such stark boundaries exist between applications, and how lovely it would be if the functionality of our current applications could be seamlessly accessed and combined with other functions in whatever ways we imagine. This sort of activity could be a part of the normal interaction that people have with computers, not something reserved only for 'programmers', and not something that requires navigating a tedious mess of ad hoc protocols, dealing with parsing and serialization, and all the other mumbo-jumbo that has nothing to do with the idea the user (programmer) is trying to express. The computing environment could be a programmable playground, a canvas in which to automate whatever tasks or activities the user wished.

Let me give an example of the problems with the current application-oriented model, and show what possibilities are put out of reach by our current framing of software. Please don't get bogged down in the details, I'm just trying to be illustrative here.

Suppose Carol and Dave are a young, conscientious couple intent on being disciplined about saving for retirement. But, they want to enjoy their time together as well, and so as part of their budget, which they manage using Mint.com, they allocate $200 per month to a virtual 'vacation' fund which accumulates from month to month. They also keep a shared Google doc in which they both jot down ideas for places they'd like to go and things they might like to do. Periodically, they take a vacation, drawing ideas from this doc. They make sure to keep the total cost of the trip under the amount that has accumulated into their vacation fund, and then attribute the cost of the trip to their vacation budget so it is deducted by Mint.com.

So far so good, but Carol, who is the planner in the relationship, notices that whenever she plans a vacation for the two of them she's doing a similar sort of thing. First, she opens up the Mint.com application to see how much money has accumulated in their vacation fund. Next, she opens up the Google doc to remind herself of the possible locations for trips they could take. Then, she goes to Kayak.com and searches for plane flights under the budget price, taking care to reserve enough leftover money for booking a hotel (say, on Hotels.com) and whatever other expenses are to be expected on the trip. It's a complex process, with lots of information and factors to keep straight, and it must be repeated each and every time they wish to plan a trip. Carol wonders if it would be possible to automate this process somehow, at least partially. She'd like a program that extracts a list of locations from their shared Google doc, then gets a list of possible flights to these locations and a list of possible hotels, then filters out any flight+hotel combinations that exceed the budget, then gives her the opportunity to interactively filter and browse through possible results, perhaps even allowing for interactive adjustments to certain base assumptions like the daily cost of miscellaneous expenses while on vacation, the dates of the trip, etc. This would save a lot of time and make the planning process more fun and creative.

Unfortunately, this sort of thing just isn't possible today. Kayak.com and Mint.com both lack APIs! Mint lets users download their transaction history, but this history doesn't indicate how much money has accumulated in each budget category. Kayak is even worse--it lacks a search API entirely.

So it seems Carol and Dave would be reduced to screen scraping if they want to programmatically build on Kayak and Mint. Google docs at least comes equipped with an API, but it's an ad hoc XML over REST API and there's friction associated with its use due to having to parse XML and so on. Overall, the friction and overhead to implementing this automation idea is way too high to justify it, so Carol doesn't bother and just does everything manually, or worse, gives up on a dream vacation!

Now let's imagine how things could be. Kayak, Mint, and Google docs would be, first and foremost, libraries rather than applications. Each might come equipped with custom views or editing environments for writing and executing certain 'shapes' of programs, but these views would not be their primary (or only) mode of interaction, as they are now. Instead, the collection of functions and data types in these libraries would be primary, and accessible within the unified programming environment of the user's desktop. This programming environment, moreover, would allow for transparent access to remote functionality, so users could write programs that call functions exposed via cloud services as well as functions defined locally.

If that example seems contrived, here's a more 'serious' one: a widget-making business has a customer relationship management (CRM) application that's used by the sales team. For each potential client, they make notes about what widget features clients are most interested in. The company also uses some project management software that lets them track features, improvements, and fixes to the product, and group these into releases. Whenever the company rolls out a new version of the widget product, the sales team would like to cross reference the list of changes that can be extracted from the project management software with the list of all the clients or leads that would be interested in these changes. Moreover, it would be nice to be able to take this list of potential clients who might be interested in newly released features and perhaps even assemble a form email calling out the particular features or improvements in the new version that that particular client was interested in. The sales team can of course add any personal touches to the emails before sending them to the potential clients.

Today, this process might end up being done manually, which doesn't scale very well if a business has hundreds or thousands of 'live' sales leads and a large number of features that they roll out with each release. Even if both the CRM and the project management app come with APIs, there is quite a bit of friction involved in writing a program that 'speaks' both APIs and handles all the boring concerns like parsing, deserialization, error handling, and so on.

I just made up these use cases, and I could come up with hundreds of others. No one piece of software 'does it all', and so individuals and businesses looking to automate or partially automate various tasks are often put in the position of having to integrate functionality across multiple applications, which is often painful or flat out impossible. The amount of lost productivity (or lost leisure time) on a global scale, both for individuals and business, is absolutely staggering.
Bob: All right, I think I finally see what you're getting at. These are very old ideas, you know. Haven't you ever heard of the Unix Philosophy? In fact, I could probably implement most of your use cases with 'a very small shell script'. 
Alice: You make it sound like Thompson and Ritchie invented the idea of composition. Mathematicians have been composing functions for hundreds (or even thousands) of years before that without making such a fuss about it or waving any sort of philosophical flag. But anyway, I would love to see you try to implement those tasks with a shell script, as you say. Have you ever tried reading a shell script written by someone else that's longer than 10 lines or so? I'm a professional programmer, well-trained in navigating all the arcane nonsense that's common in software, and a small part of me dies every time I have to write or read a bash script. I appreciate the spirit of the Unix Philosophy, but the implementation, of writing programs in a terrible language that read and write 'vaguely parseable text' leaves a lot to be desired. And JSON and XML aren't much better, either. 
Bob: So you really think that Carol and some sales guys are going to be writing programs, even if it is some theoretical future souped-up graphical programming environment? 
Alice: Why does that seem so unlikely to you? 
Bob: Because writing software is complicated! I know because I'm a professional programmer. We can't expect the masses to be writing the sort of complex program that we professional programmers produce. 
Alice: 'Complex programs'? You mean like Instagram? A website where you can post photos of kittens and subscribe to a feed of photos produced by other people? Or Twitter? Or any one of the 95% of applications which are just a CRUD interface to some data store? The truth is, if you strip applications of all their incidental complexity (largely caused by the artificial barriers at application boundaries), they are often extremely simple. But in all seriousness, why can't more people write programs? Millions of people use spreadsheets, an even more impoverished and arcane programming environment than what we could build. 
Bob: Maybe so, but I still don't think that a programming environment can ever be accessible to the majority of people. Spreadsheets are a good example--they are a rather accessible (if limited) form of programming, and not everyone uses the programmability of spreadsheets or even wants to! 
Alice: And two thousand years ago, most of the population was illiterate and arithmetic was considered too difficult for the average person, yet now we teach kids these things in elementary school. The truth is, we don't really know how many people might program if given a learnable programming environment and programming were reduced to its exhilarating, creative essence. I worry we have raised generations of programmers who are simply very good at tolerating bullshit and, paraphrasing Paul Lockhart, the most talented programmer of our time may be a waitress in Tulsa, Oklahoma who considers herself bad at computers. The spreadsheet brought programming (in a limited fashion) to millions of people, and a more accessible environment could bring it to millions or billions more. Who are you, with your limited imagination, to place a ceiling on how accessible programming could be? Well, the world is what we make of it, and I want to make a world in which applications die off, programming is no longer the awkward, arcane and tedious process it often is today, and where the internet is used to transparently share, use, and compose functionality across the internet. Which brings me to my next point...

What's wrong with the internet

The internet contains vast pools of data and functionality largely trapped within noncomposable applications all competing to be the center of the universe.

The economy of the internet is deeply broken. Have you ever wondered why the internet market is dominated by a few huge businesses like Google, Facebook, Twitter, etc? High transaction costs imposed by application boundaries have distorted the software economy, making it artificially expensive to integrate functionality from third-parties. This selects for larger businesses with the resources to develop and integrate functionality internally, which they do using composable libraries within their own application boundaries. From here, network effects due mostly to high switching costs (again, because of application boundary friction) sustain the positions of these larger market players. We essentially have a situation in which these larger market players own a significant portion of the network effects on the web. It would be preferable if ownership of these network effects were transferred to the public domain and businesses were forced to compete on their ideas and cleverness in describing these ideas in software, rather than competing as they do now on how well they can coax users into entering various walled gardens and keep them there with lock-in and high switching costs. With a unified programming environment spanning the web (I'll say more about this in another post), we could see these transaction costs and switching costs drop to nearly zero and a radical democratization of the internet market as ownership of these network effects is transferred to the public domain.

Unlike the production of many physical goods and services, software does not have any natural economies of scale. Arguably, there are diseconomies of scale with software--per unit of functionality, software becomes harder to write with the addition of more people, resources and code, because of the complexity of managing a large codebase and coordinating concurrent development. Large businesses with significant codebases fight a constant (losing) battle against entropy and employ armies of developers to maintain and make rudimentary additions to functionality. The 'economies of scale' with software are almost entirely due to artificially high transactions costs caused by the application-centered world view and the lack of a unified computational framework owned by the public. As a civilization, we would be better off if software could be developed by small, unrelated groups, with an open standard that allowed for these groups to trivially combine functionality produced anywhere on the network.

What I am proposing is a radical shift that could mean the end of huge internet businesses like Google and Facebook. Or rather, it means that Google and Facebook would be forced to compete on functionality with programmers all over the world, any of whom could write similar functionality that could be substituted for Google/Facebook functionality with literally zero switching costs. Oh, I might use Google as 'cloud provider', a place to stick my data and my computations, but this would be using Google as a commodity, an implementation detail, much the way I use the physical computer on which I type this right now. At any point, I could choose to transfer my data and personal functionality to another cloud provider, again with zero switching costs. And while we're at it, perhaps we could dispense with cloud providers entirely and replace them with a peer-to-peer network in which individuals share compute time and local storage!
Bob: I wouldn't knock Google, Twitter, and Instagram... they are serving literally millions of concurrent users. That's a serious technical challenge, you know. 
Alice: A serious technical challenge that has been created artificially! In the world I envision, the (limited) functionality of sites like Twitter could be written as a library and then used in a decentralized way by anyone connected to the internet. Writing such a library would require no servers, no capital, and could be completed by a programmer (or user) in a weekend! Think about it--if I write quicksort as a library function, is there any 'serious technical challenge' in making it possible for my function to be used by millions of users? No, of course not, because my function is pure information and can be transported all over the world and run by a billion people simultaneously, without my having to do anything other than put the code somewhere connected to the internet. But for some strange reason, if I write a function that operates on the follows-graph maintained in an (unnecessarily) centralized way by Twitter, I need to deal with all sorts of complexity if I want this function to be used by more than a few hundred people concurrently? Twitter (and Facebook, and Instagram, and Google) are solving problems created by the 'application as center of the universe' viewpoint that is so common today. 
Bob: Even so, I think you are vastly underestimating the complexity of the software that these companies produce. These companies are coordinating the activities of fleets of computers, doing error handling and recovery, and wrapping up often complex functionality in nice, usable interfaces (which by the way have seen many man months worth of tuning and testing) that you do nothing but complain about! We have it so easy! 
Alice: And yet, I still can't get Gmail to do even simple tasks like schedule an email to be sent later or batch up all incoming emails containing a certain phrase into a weekly digest! By the way, I just thought up those use cases on the spot, I could think of dozens more that aren't supported. The problem is, I don't want a machine, I want a toolkit, and Google keeps trying to sell me machines. Perhaps these machines are exquisitely crafted, with extensive tuning and so forth, but a machine with a fixed set of actions can never do all the things that I can imagine might be useful, and I don't want to wait around for Google to implement the functionality I desire as another awkward one-off 'feature' that's poorly integrated and just adds more complexity to an already bloated application. 
Bob: Okay, if you want a toolkit, how about Yahoo! Pipes, or If This Then That? Aren't those sort of what you want? 
Alice: Absolutely not. For one, I don't want my data and functionality locked up with a particular provider like that. I want an open platform. Who knows when Yahoo! might kill off Pipes or start changing inordinate sums of money for it, and who knows if ITTT is going to even be around a year from now given that they seem to have no business model. I would only use these services for throw-away code I don't care about. Have you ever noticed that all the programming languages people use voluntarily are open source? I think it's because no one wants their creations owned by anyone. But beyond that, the bigger reason I don't like these services is that I want a real programming language, with a real type system that lets me assemble complex functionality with ease and guides me through the process.

Why UX designers should care about type theory

Applications are bad enough in that they trap potentially useful building blocks for larger program ideas behind artificial barriers, but they fail at even their stated purpose of providing an 'intuitive' interface to whatever fixed set of actions and functionality its creators have imagined. Here is why: the problem is that for all but the simplest applications, there are multiple contexts within the application and there needs to be a cohesive story for how to present only 'appropriate' actions to the user and prevent nonsensical combinations based on context. This becomes serious business as the total number of actions offered by an application grows and the set of possible actions and contexts grows. As an example, if I just have selected a message in my inbox (this is a 'context'), the 'send' action should not be available, but if I am editing a draft of a message it should be. Likewise, if I have just selected some text, the 'apply Kodachrome style retro filter' action should not be available, since that only makes sense applied to a picture of some sort.

These are just silly examples, but real applications will have many more actions to organize and present to users in a context-sensitive way. Unfortunately, the way 'applications' tend to do this is with various ad hoc approaches that don't scale very well as more functionality is added--generally, they allow only a fixed set of contexts, and they hardcode what actions are allowed in each context. ('Oh, the send function isn't available from the inbox screen? Okay, I won't add that option to this static menu'; 'Oh, only an integer is allowed here? Okay, I'll add some error checking to this text input') Hence the paradox: applications never seem to do everything we want (because by design they can only support a fixed set of contexts and because how to handle each context must be explicitly hardcoded), and yet we also can't seem to easily find the functionality they do support (because the set of contexts and allowed actions is arbitrary and unguessable in a complex application).

There is already a discipline with a coherent story for how to handle concerns of what actions are appropriate in what contexts: type theory. Which is why I now (half) jokingly introduce Chiusano's 10th corollary:
Any sufficiently advanced user-facing program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of a real programming language and type system.
Programming languages and type theory have largely solved the problem of how to constrain user actions to only 'appropriate' alternatives and present these alternatives to users in an exquisitely context-sensitive way. The fundamental contribution of a type system is to provide a compositional language for describing possible forms values can take, and to provide a fully generic program (the typechecker) for determining whether an action (a function) is applicable to a particular value (an argument to the function). Around this core idea we can build UI for autocompletion, perfectly appropriate context menus, program search, and so on. Type systems provide a striking, elegant solution to a problem that UX designers now solve in more ad hoc ways. These ad hoc methods don't scale and can never match what is possible when guided by an actual type system and the programming environment to go with it.

The work that remains is more around how to build meaningful, sensitive, real-time interfaces to the typechecker and integrate it within a larger programming environment supporting a mixture of graphical and textual program elements. Note that the richer the type system, the more mileage we get out of this approach.

Conclusion

I'll conclude with a great quote by Rúnar Bjarnason, explaining how we got to this point, and why it is deeply wrong:
In the early days of programming, there were no computers. The first programs were written, and executed, on paper. It wasn't until later that machines were first built that could execute programs automatically. 
During the ascent of computers, an industry of professional computer programmers emerged. Perhaps because early computers were awkward and difficult to use, the focus of these professionals became less thinking about programs and more manipulating the machine. 
Indeed, if you read the Wikipedia entry on "Computer Program", it tells you that computer programs are "instructions for a computer", and that "a computer requires programs to function". This is a curious position, since it's completely backwards. It implies that programming is done in order to make computers do things, as a primary. I’ll warrant that the article was probably written by a professional programmer. 
But why does a computer need to function? Why does a computer even exist? The reality is that computers exist solely for the purpose of executing programs. The machine is not a metaphysical primary. Reality has primacy, a program is a description, an abstraction, a proof of some hypothesis about an aspect of reality, and the computer exists to deduce the implications of that fact for the pursuit of human values.
Though the post talks specifically about not creating our programming languages in the machine's image, we should apply the same reasoning to the useful bundles of data and functionality that we now call 'applications'.

So there you have it. The machines are no longer primary. End the tyranny of applications!

by Paul Chiusano (noreply@blogger.com) at May 29, 2013 01:12 PM

May 25, 2013

Coderspiel

"completeme is a python script to auto-complete filenames in a given directory, much like..."

completeme is a python script to auto-complete filenames in a given directory, much like Github’s ‘t’ keyboard shortcut or Command-T in TextMate or SublimeText. When you’ve settled on the file you’d like to edit, press “Enter” to open it with whatever’s in your $EDITOR variable or press “Tab” to drop that filename at the end of your current command!”

- mattspitz/completeme · GitHub

May 25, 2013 08:13 PM

implicit.ly: dispatch 0.10.1

implicit.ly: dispatch 0.10.1:

implicitly-notes:

This release improves the behavior of Dispatch 0.10 when used within sbt interactive mode.

In sbt’s process

By default sbt runs its console, run, and test tasks in a JVM shared with sbt itself. Previous releases of Dispatch have, for your convenience, marked all background threads as…

May 25, 2013 05:26 PM

Jesper Nordenberg

Sets and the Type of 1

I've been pondering the subject of types and values for some time and decided to materialize my thoughts in a blog post. Too keep things simple and easily readable I will reason about types from a set theory point of view, not using traditional type theory.

What is a Type?

Let's start by investigating what a type is. According to Wikipedia a type system can be defined as "a tractable syntactic framework for classifying phrases according to the kinds of values they compute". So, the type of a phrase (whatever that is) is simply the set of the values that can be computed from this phrase. In this post I will use the terms type and set interchangeably as I consider them to be the same thing (mathematically this might not be correct).

The Type of 1

So, what is the type of the value 1? In Scala (and Java, C/C++, C# etc.) the type of 1 is a 32-bit signed integer. This is strange as the value 1 is a member of many sets (in fact an infinite number of them), so why is this set chosen? Probably because of historical reasons and because it's a convenient type to do calculations with on most computers, but mathematically this type doesn't have any special meaning.

In Haskell the type of 1 is (Num t) => t. Indeed confusing, the type is defined in terms of a typeclass. This set includes a lot of values, including user defined ones. Really, Haskell must resort to typeclasses to define the type of 1?

I would argue that there are only two reasonable types for the value 1: the set containing all values and the set containing only the value 1.  The set containing all values is really not useful in static type checking, so that leaves the set which only element is the value 1, {1} in set notation (also called a singleton set). The same reasoning would apply to all values. I find it remarkable that pretty much all commonly used programming languages gets this simple type wrong. How can one expect a type system to be useful if it can't even infer the correct type of the most basic expressions?

The Type of a Function

A (total) function is a relation that maps each element of one set, let's call it the input set, to exactly one element of another (or the same) set, let's call it the output set. This relation can be modelled as a set of pairs a -> b where a is a member of the input set and b is a member of the output set. The additional restriction on the relation set is that there must be exactly one pair a -> * in the set (where * represents any member of the output set).

So, let's apply this model to a simple function in Scala:

val foo = (b : Boolean) => if (b) 1 else "a"

What is the only reasonable type of foo? Well, the input set of foo is Boolean which is equal to {true, false}. The output set of foo is {1, "a"}. If the input value is true, foo will output the value 1 and if the input value is false, foo will output value "a". So we can write the type of foo as {true -> 1, false -> "a"}. In Scala the type of foo is Boolean => Any, which is a pretty useless type as Any is the set of all values (i.e. the type doesn't tell us anything about the value). In Haskell the corresponding definition won't even type check. Seriously, these are state of the art programming languages, is this the best we can do?

Subtyping

Well, what if we have:

val addOne : Int => Int
val x : {1} = 1
addOne(x)   // Type error?

No, this should not be a type error. The first thing to notice is that the set Int contains the value 1 and thus {1} is a subset of Int. This means that it's perfectly safe to apply the function addOne on the input set {1} as we're guaranteed that the relation set addOne contains a mapping for each element in the input set. In type theory one would say that {1} is a subtype of Int.


Final Words

I've only skimmed the surface of how to apply the concept of sets and values to programming languages. The concept can be extended to ADT's, pattern matching, type classes etc. I think there is a lot to gain from thinking about types this way because there is something fundamentally broken in the way types are implemented in most programming languages. Do you agree?

by Jesper Nordenberg (noreply@blogger.com) at May 25, 2013 11:03 AM

May 24, 2013

Kevin Albrecht

A Short Survey on the State of Functional Reactive Programming in ClojureScript

In the Object-Oriented world of JavaScript, architectures like MVP, MVC, and their brethren are well established. But when I started using ClojureScript to build rich user interfaces on the web, I was naturally interested in finding ways to architect my site that are friendly to a functional programming language like Clojure. This led me to functional reactive programming, or FRP.

FRP, probably the best-known user interface paradigm for functional programming, models UIs as dataflows, where changes (user actions or data source changes) propagate through the system using purely functional operations and eventually end up as visual changes back in the UI. In short, FRP is similar to how changes to a cell in a spreadsheet propagate to other dependent cells.

Though the ClojureScript ecosystem is still fairly immature (or perhaps because it is), there are many libraries which support the FRP style of programming. If you are looking for an alternative to faking object-oriented styles of programming in ClojureScript, jump in and give one or more a try!

(By the way, if you want my personal recommendation, I have had a lot of luck with the simplicity and power of widje.)

Native ClojureScript libraries

acute (wrapper around AngularJS)
rx-cljs (wrapper around RxJS)
Yolk (wrapper around Bacon.js)
Clang (wrapper around AngularJS)

Using JavaScript libraries directly

by Kevin Albrecht (noreply@blogger.com) at May 24, 2013 05:56 AM

May 23, 2013

scala-lang.org

Scala 2.10.2-RC1 now available!

We are very happy to announce the RC1 release of Scala 2.10.2! If no serious blocking issues are found this will become the final 2.10.2 version. 

The Scala team and contributors fixed 89 issues since 2.10.1!

In total, 164 RC1 pull requests were opened on GitHub, of which 134 were merged after having been tested and reviewed.

by moors at May 23, 2013 12:26 AM

May 21, 2013

scala-lang.org

Scala Workshop (Scala2013) Program Announced!

The Scala2013 Workshop Program is now available! We're quite excited about this year's program-- we received a record number of submissions, leading to a first-class program spanning compilation & metaprogramming, parallelism/concurrency, verification & synthesis, debugging tools and more!
 

by heathermiller at May 21, 2013 07:31 AM

Eric Torreborre

Specs2 2.0 - Interpolated

<status class="ok"></status><status class="ok"></status><status class="ok">

The latest release of specs2 (2.0) deserves a little bit more than just release notes. It needs explanations, apologies and a bit of celebration!

Explanations

  • why is there another (actually several!) new style(s) of writing acceptance specifications
  • what are Scripts and ScriptTemplates
  • what has been done for compilation times
  • what you can do with Snippets
  • what is an ExampleFactory

Apologies

  • the >> / in problem
  • API breaks
  • Traversable matchers
  • AroundOutside and Fixture
  • the never-ending quest for Given/When/Then specifications

Celebration

  • compiler-checked documentation!
  • "operator-less" specifications!
  • more consistent Traversable matchers!

Explanations

Scala 2.10 is a game changer for specs2, thanks to 2 features: String interpolation and Macros.

String interpolation

Specs2 has been designed from the start with the idea that it should be immutable by default. This has led to the definition of Acceptance specifications with lots of operators, or, as someone put it elegantly, "code on the left, brainfuck on the right":

class HelloWorldSpec extends Specification { def is =

"This is a specification to check the 'Hello world' string" ^
p^
"The 'Hello world' string should" ^
"contain 11 characters" ! e1^
"start with 'Hello'" ! e2^
"end with 'world'" ! e3^
end
def e1 = "Hello world" must have size(11)
def e2 = "Hello world" must startWith("Hello")
def e3 = "Hello world" must endWith("world")
}

Fortunately Scala 2.10 now offers a great alternative with String interpolation. In itself, String interpolation is not revolutionary. A string starting with s can have interpolated variables:

</status><status class="ok">

val name = "Eric"
s"Hello $name!"

Hello Eric!

But the great powers behind Scala realized that they could both provide standard String interpolation and give you the ability to make your own. Exactly what I needed to make these pesky operators disappear!

class HelloWorldSpec extends Specification { def is =         s2"""

This is a specification to check the 'Hello world' string

The 'Hello world' string should
contain 11 characters $e1
start with 'Hello' $e2
end with 'world' $e3
"""

def e1 = "Hello world" must have size(11)
def e2 = "Hello world" must startWith("Hello")
def e3 = "Hello world" must endWith("world")
}

What has changed in the specification above is that text Fragments are now regular strings in the multiline s2 string and the examples are now inserted as interpolated variables. Let's explore in more details some aspects of this new feature:

  • layout
  • examples descriptions
  • other fragments
  • implicit conversions
  • auto-examples
Layout

If you run the HelloWorldSpec you will see that the indentation of each example is respected in the output:

This is a specification to check the 'Hello world' string

The 'Hello world' string should
+ contain 11 characters
+ start with 'Hello'
+ end with 'world'

This means that you don't have to worry anymore about the layout of text and use the p, t, bt, end, endp formatting fragments as before.

Examples descriptions

On the other hand, the string which is taken as the example description is not as well delimited anymore, so it is now choosen by convention to be everything that is on the same line. For example this is what you get with the new interpolated string:

</status><status class="ok">

s2"""
My software should
do something that it pretty long to explain,
so long that it needs 2 lines" ${ 1 must_== 1 }
"""
My software should
do something that it pretty long to explain,
+ so long that it needs 2 lines"

If you want the 2 lines to be included in the example description you will need to use the "old" form of creating an example:

</status><status class="ok">

s2"""
My software should
${ """do something that it pretty long to explain,
so long that it needs 2 lines""" ! { 1 must_== 1 } }
"""
My software should+ do something that it pretty long to explain,
so long that it needs 2 lines

But I suspect that there will be very few times when you will want to do that.

Other fragments and variables

Inside the s2 string you can interpolate all the usual specs2 fragments: Steps, Actions, included specifications, Forms... However you will quickly realize that you can not interpolate arbitrary objects. Indeed, excepted specs2 objects, the only other 2 types which you can use as variables are Snippets (see below) and Strings.

The restriction is there to remind you that, in general, interpolated expressions are "unsafe". If the expression you're interpolating is throwing an Exception, as it is commonly the case with tested code, there is no way to catch that exception. If that exception is uncaught, the whole specification will fail to be built. Why is that?

Implicit conversions

When I first started to experiment with interpolated strings I thought that they could even be used to write Unit Specifications:

s2"""
This is an example of conversion using integers ${
val (a, b) = ("1".toInt, "2".toInt)
(a + b) must_== 3
}
"""

Unfortunately such specifications will horribly break if there is an error in one of the examples. For instance if the example was:

This is an example of conversion using integers ${
// oops, this is going to throw a NumberFormatException!
val (a, b) = ("!".toInt, "2".toInt)
(a + b) must_== 3
}

Then the whole string and the whole specification will fail to be instantiated!

The reason is that everything you interpolate is converted, through an implicit conversion, to a "SpecPart" which will be interpreted differently depending on its type. If it is a Result then we will interpret this as the body of an Example and use the preceding text as the description. If it is just a simple string then it is just inserted in the specification as a piece of text. But implicit conversions of a block of code, as above, are not converting the whole block. They are merely converting the last value! So if anything before the last value throws an Exception you will have absolutely no way to catch it and it will bubble up to the top.

That means that you need to be very prudent when interpolating arbitrary blocks. One work-around is to do something like that

import execute.{AsResult => >>}
s2"""

This is an example of conversion using integers ${>>{
val (a, b) = ("!".toInt, "2".toInt)
(a + b) must_== 3
}}
"""

But you have to admit that the whole ${>>{...}} is not exactly gorgeous.

Auto-examples

One clear win of Scala 2.10 for specs2 is the use of macros to capture code expressions. This particularly interesting with so-called "auto-examples". This feature is really useful when your examples are so self-descriptive that a textual description feels redundant. For example if you want to specify the String.capitalize method:

</status><status class="ok">

s2"""
The `capitalize` method verifies
${ "hello".capitalize === "Hello" }
${ "Hello".capitalize === "Hello" }
${ "hello world".capitalize === "Hello world" }
"""
 The `capitalize` method verifies
+ "hello".capitalize === "Hello"
+ "Hello".capitalize === "Hello"
+ "hello world".capitalize === "Hello world"


It turns out that the method interpolating the s2 extractor is using a macro to extract the text for each interpolated expression and so, if on a given line there is no preceding text, we take the captured expression as the example description. It is important to note that this will only properly work if you enable the -Yrangepos scalac option (in sbt: scalacOptions in Test := Seq("-Yrangepos")).

However the drawback of using that option is the compilation speed cost which you can incur (around 10% in my own measurements). If you don't want (or you forget :-)) to use that option there is a default implementation which should do the trick in most cases but which might not capture all the text in some edge cases.

Scripts

The work on Given/When/Then specifications has led to a nice generalisation. Since the new GWT trait decouples the specification text from the steps and examples to create, we can push this idea a bit further and create "classical" specifications where the text is not annotated at all and examples are described somewhere else.

Let's see what we can do with the org.specs2.specification.script.Specification class:

import org.specs2._
import specification._

class StringSpecification extends script.Specification with Grouped { def is = s2"""

Addition
========

It is possible to add strings with the + operator
+ one string and an empty string
+ 2 non-empty strings

Multiplication
==============

It is also possible to duplicate a string with the * operator
+ using a positive integer duplicates the string
+ using a negative integer returns an empty string
"""

"addition" - new group {
eg := ("hello" + "") === "hello"
eg := ("hello" + " world") === "hello world"
}
"multiplication" - new group {
eg := ("hello" * 2) === "hellohello"
eg := ("hello" * -1) must beEmpty
}
}

With script.Specifications you just provide a piece of text where examples are starting with a + sign and you specify examples groups. Example groups were introduced in a previous version of specs2 with the idea of providing standard names for examples in Acceptance specifications.

When the specification is executed, the first 2 example lines are mapped to the examples of the first group, and the examples lines from the next block (as delimited with a Markdown title) are used to build examples by taking expectations in the second group (those group are automatically given names, g1 and g2, but you can specify them yourself: "addition" - new g1 {...).

This seems to be a lot of "convention over configuration" but this is actually all configurable! The script.Specification class is an example of a Script and it is associated with a ScriptTemplate which defines how to parse text to create fragments based on the information contained in the Script (we will see another example of this in action below with the GWT trait which proposes another type of Script named Scenario to define Given/When/Then steps).

There are lots of advantages in adopting this new script.Specification class:

  • it is "operator-free", there's no need to annotate your specification on the right with strange symbols

  • tags are automatically inserted for you so that it's easy to re-run a specific example or group of examples by name: test-only StringSpecification -- include g2.e1

  • examples are marked as pending if you haven't yet implemented them

  • it is configurable to accomodate for other templates (you could even create Cucumber-like specifications if that's your thing!)

The obvious drawback is the decoupling between the text and the examples code. If you restructure the text you will have to restructure the examples accordingly and knowing which example is described by which piece of text is not obvious. This, or operators on the right-hand side, choose your poison :-)

Compilation times

Scala's typechecking and JVM interoperability comes with a big price in terms of compilation times. Moderately-sized projects can take minutes to compile which is very annoying for someone coming from Java or Haskell.

Bill Venners has tried to do a systematic study of which features in testing libraries seems to have the biggest impact. It turns out that implicits, traits and byname parameters have a significant impact on compilation times. Since specs2 is using those features more than any other test library, I tried to do something about it.

The easiest thing to do was to make Specification an abstract class, not a trait (and provide the SpecificationLike trait in its place). My unscientific estimation is that this single change removed 0.5 seconds per compiled file (from 313s to 237s for the specs2 build, and a memory reduction of 55Mb, from 225Mb to 170Mb).

Then, the next very significant improvement was to use interpolated specifications instead of the previous style of Acceptance specifications. The result is impressive: from 237 seconds to 150 seconds and a memory reduction of more than 120Mb, from 170Mb to 47Mb!

On the other hand, when I tried to remove some of the byname parameters (the left part of a must_== b) I didn't observe a real impact on compilation times (only 15% less memory).

The last thing I did was to remove some of the default matchers (and to add a few others). Those matchers are the "content" matchers: XmlMatchers, JsonMatchers, FileMatchers, ContentMatchers (and I added instead the TryMatchers). I did this to remove some implicits from the scope when compiling code but also to reduce the namespace footprint everytime you extend the Specification class. However I couldn't see a major improvement to compile-time performances with this change.

Snippets

One frustration of software documentation writers is that it is very common to have stale or incorrect code because the API has moved on. What if it was possible to write some code, in the documentation, that will be checked by the compiler? And automatically refactored when you change a method name?

This is exactly what Snippets will do for you. When you want to capture and display a piece of code in a Specification you create a Snippet:

s2"""
This is an example of addition: ${snippet{

// who knew?
1 + 1 == 2
}}
"""

This renders as:

This is an example of addition

// who knew?
1 + 1 == 2

And yes, you guessed it right, the Snippet above was extracted by using another Snippet! I encourage you to read the documentation on Snippets to see what you can do with them, the main features are:

  • code evaluation: the last value can be displayed as a result

  • checks: the last value can be checked and reported as a failure in the Specification

  • code hiding: it is possible to hide parts of the code (initialisations, results) by enclosing them in "scissors" comments of the form // 8<--

Example factory

Every now and then I get a question from users who want to intercept the creation of examples and use the example description to do interesting things before or after the example execution. It is now possible to do so by providing another ExampleFactory rather than the default one:

import specification._

class PrintBeforeAfterSpec extends Specification { def is =
"test" ! ok

case class BeforeAfterExample(e: Example) extends BeforeAfter {
def before = println("before "+e.desc)
def after = println("after "+e.desc)
}

override def exampleFactory = new ExampleFactory {
def newExample(e: Example) = {
val context = BeforeAfterExample(e)
e.copy(body = () => context(e.body()))
}
}
}

The PrintBeforeAfterSpec will print the name of each example before and after executing it.

Apologies

the >> / in problem

This issue has come up at different times and one lesson is: Unit means "anything" so don't try to be too smart about it. So I owe an apology to the users for this poor API design choice and for the breaking API change that is now ensuing. Please read the thread in the Github issue to learn how to fix compile errors that would result from this change.

API breaks

While we're on the subject of API breaks, let's make a list:

  • Unit values in >> / in: now you need to explicitly declare if you mean "a list of examples created with foreach" or "a list of expectations created with foreach"

  • Specification is not a trait anymore so you should use the SpecificationLike trait instead if that's what you need (see the Compilation times section)

  • Some matchers traits have been removed from the default matchers (XML, JSON, File, Content) so you need to explicitly mix them in (see the Compilation times section)

  • The Given/When/Then functionality has been extracted as a deprecated trait specification.GivenWhenThen (see the Given/When/Then? section)

  • the negation of the Map matchers has changed (this can be considered as a fix but this might be a run-time break for some of you)

  • many of the Traversable matchers have been deprecated (see the next section)

Traversable matchers

I've had this nagging thought in my mind for some time now but it only reached my conscience recently. I always felt that specs2 matchers for collections were a bit ad-hoc, with not-so-obvious ways to do simple things. After lots of fighting with implicit classes, overloading and subclassing, I think that I have something better to propose.

With the new API we generalize the type of checks you can perform on elements:

  • Seq(1, 2, 3) must contain(2) just checks for the presence of one element in the sequence

  • this is equivalent to writing Seq(1, 2, 3) must contain(equalTo(2)) which means that you can pass a matcher to the contain method. For example containAnyOf(1, 2, 3) is contain(anyOf(1, 2, 3)) where anyOf is just another matcher

  • and more generally, you can pass any function returning a result! Seq(1, 2, 3) must contain((i: Int) => i must beEqualTo(2)) or Seq(1, 2, 3) must contain((i: Int) => i == 2) (you can even return a ScalaCheck Prop if you want)

Then we can use combinators to specify how many times we want the check to be performed:

  • Seq(1, 2, 3) must contain(2) is equivalent to Seq(1, 2, 3) must contain(2).atLeastOnce

  • Seq(1, 2, 3) must contain(2).atMostOnce

  • Seq(1, 2, 3) must contain(be_>=(2)).atLeast(2.times)

  • Seq(1, 2, 3) must contain(be_>=(2)).between(1.times, 2.times)

This covers lots of cases where you would previously use must have oneElementLike(partialFunction) or must containMatch(...). This also can be used instead of the forall, atLeastOnce methods. For example forall(Seq(1, 2, 3)) { (i: Int) => i must be_>=(0) } is Seq(1, 2, 3) must contain((i: Int) => i must be_>=(0)).forall.

The other type of matching which you want to perform on collections is with several checks at the time. For example:

  • Seq(1, 2, 3) must contain(allOf(2, 3))

This seems similar to the previous case but the combinators you might want to use with several checks are different. exactly is one of them:

  • Seq(1, 2, 3) must contain(exactly(3, 1, 2)) // we don't expect ordered elements by default

Or inOrder

  • Seq(1, 2, 3) must contain(exactly(be_>(0), be_>(1), be_>(2)).inOrder) // with matchers here

One important thing to note though is that, when you are not using inOrder, the comparison is done greedily, we don't try all the possible combinations of input elements and checks to see if there would be a possibility for the whole expression to match.

Please explore this new API and report any issue (bug, compilation error) you will find. Most certainly the failure reporting can be improved. The description of failures is much more centralized with this new implementation but also a bit more generic. For now, the failure messages are just listing which elements were not passing the checks but they do not output something nice like: The sequence 'Seq(1, 2, 3) does not contain exactly the elements 4 and 3 in order: 4 is not found'.

AroundOutside vs Fixture

My approach to context management in specs2 has been very progressive. First I provided the ability to insert code (and more precisely effects) before or after an Example, reproducing here standard JUnit capabilities. Then I've introduced Around to place things "in" a context, and Outside to pass data to an example. And finally AroundOutside as the ultimate combination of both capabilities.

I thought that with AroundOutside you could do whatever you needed to do, end of story. It turns out that it's not so simple. AroundOutside is not general enough because the generation of Outside data cannot be controled by the Around context. This proved to be very problematic for me on a specific use case where I needed to re-run the same example, based on different parameters, with slightly different input data each time. AroundOutside was just not doing it. The solution? A good old Fixture. Very simple, a Fixture[T], is a trait like that:

trait Fixture[T] {
def apply[R : AsResult](f: T => R): Result
}

You can define an implicit fixture for all the examples:

class s extends Specification { def is = s2"""
first example using the magic number $e1
second example using the magic number $e1
"""

implicit def magicNumber = new specification.Fixture[Int] {
def apply[R : AsResult](f: Int => R) = AsResult(f(10))
}

def e1 = (i: Int) => i must be_>(0)
def e2 = (i: Int) => i must be_<(100)
}

I'm not particularly happy to add this to the API because it adds to the overall API footprint and learning curve, but in some scenarios this is just indispensable.

Given/When/Then?

With the new "interpolated" style I had to find another way to write Given/When/Then (GWT) steps. But this is tricky. The trouble with GWT steps is that they are intrisically dependent. You cannot have a Then step being defined before a When step for example.

The "classic" style of acceptance specification is enforcing this at compile time because, in that style, you explicitly chain calls and the types have to "align":

class GivenWhenThenSpec extends Specification with GivenWhenThen { def is =

"A given-when-then example for a calculator" ^ br^
"Given the following number: ${1}" ^ aNumber^
"And a second number: ${2}" ^ aNumber^
"And a third number: ${6}" ^ aNumber^
"When I use this operator: ${+}" ^ operator^
"Then I should get: ${9}" ^ result^
end

val aNumber: Given[Int] = (_:String).toInt
val operator: When[Seq[Int], Operation] = (numbers: Seq[Int]) => (s: String) => Operation(numbers, s)
val result: Then[Operation] = (operation: Operation) => (s: String) => { operation.calculate must_== s.toInt }

case class Operation(numbers: Seq[Int], operator: String) {
def calculate: Int = if (operator == "+") numbers.sum else numbers.product
}
}

We can probably do better than this. What is required?

  • to extract strings from text and transform them to well-typed values
  • to define functions using those values so that types are respected
  • to restrict the composition of functions so that a proper order of Given/When/Then is respected
  • to transform all of this into Steps and Examples

So, with apologies for coming up with yet-another-way of doing the same thing, let me introduce you to the GWT trait:

</status><status class="ok">

import org.specs2._                                                                                      
import specification.script.{GWT, StandardRegexStepParsers}

class GWTSpec extends Specification with GWT with StandardRegexStepParsers { def is = s2"""

A given-when-then example for a calculator ${calculator.start}
Given the following number: 1
And a second number: 2
And a third number: 6
When I use this operator: +
Then I should get: 9
And it should be >: 0 ${calculator.end}
"""

val anOperator = readAs(".*: (.)$").and((s: String) => s)

val calculator =
Scenario("calculator").
given(anInt).
given(anInt).
given(anInt).
when(anOperator) { case op :: i :: j :: k :: _ => if (op == "+") (i+j+k) else (i*j*k) }.
andThen(anInt) { case expected :: sum :: _ => sum === expected }.
andThen(anInt) { case expected :: sum :: _ => sum must be_>(expected) }

}

In the specification above, calculator is a Scenario object which declares some steps through the given/when/andThen methods. The Scenario class provides a fluent interface in order to restrict the order of calls. For example, if you try to call a given step after a when step you will get a compilation error. Furthermore steps which are using extracted values from previous steps must use the proper types, what you pass to the when step has to be a partial function taking in a Shapeless HList of the right type.

You will also notice that the calculator is using anInt, anOperator. Those are StepParsers, which are simple objects extracting values from a line of text and returning Either[Exception, T] depending on the correct conversion of text to a type T. By default you have access to 2 types of parsers. The first one is DelimitedStepParser which expects that values to extract are enclosed in {} delimiters (this is configurable). The other one is RegexStepParser which uses a regular expression with groups in order to know what to extract. For example anOperator defines that the operator to extract will be just after the column at the end of the line.

Finally the calculator scenario is inserted into the s2 interpolated string to delimitate the text it applies to. Scenario being a specific kind of Script it has an associated ScriptTemplate which defines that the last lines of the text should be paired with the corresponding given/when/then method declarations. This is configurable and we can imagine other ways of pairing text to steps (see the org.specs2.specification.script.GWT.BulletTemplate class for example).

For reasons which are too long to expose here I've never been a big fan of Given/When/Then specifications and I guess that the multitude of ways to do that in specs2 shows it. I hope however that the GWT fans will find this approach satisfying and customisable to their taste.

Celebration!

I think there are some really exciting things in this upcoming specs2 release for "Executable Software Specifications" lovers.

Compiler-checked documentation

Having compiler-checked snippets is incredibly useful. I've fixed quite a few bugs in boths specs2 and Scoobi user guides and I hope that I made them more resistant to future changes that will happen through refactoring (when just renaming things for example). I'm also very happy that, thanks to macros, the ability to capture code was extended to "auto-examples". In previous specs2 versions, this is implemented by looking at stack traces and doing horrendous calculations on where a piece of code would be. This gives me the shivers everytime I have to look at that code!

No operators

The second thing is Scripts and ScriptTemplates. There is a trade-off when writing specifications. On one hand we would like to read pure text, without the encumbrance of implementation code, on the other hand, when we read specification code, it's nice to have a short sentence explaining what it does. With this new release there is a continuum of solutions on this trade-off axis:

  1. you can have pure text, with no annotations but no navigation is possible to the code (with org.specs2.specification.script.Specification)
  2. you can have annotated text, with some annotations to access the code (with org.specs2.Specification)
  3. you can have text interspersed with the code (with org.specs2.mutable.Specification)
New matchers

I'm pretty happy to have new Traversable matchers covering a lot more use cases than before in a straight-forward manner. I hope this will reduce the thinking time between "I need to check that" and "Ok, this is how I do it".


Please try out the new Release Candidate, report bugs, propose enhancements and have fun!

</status><status class="ok">

</status>

by Eric (noreply@blogger.com) at May 21, 2013 12:33 AM

May 19, 2013

Coderspiel

Senistive touchpads and Ubuntu

Last fall Meetup furnished me with a ThinkPad X1 Carbon. I was excited about this model because, as much as I love the x201 I use at home, the X1 Carbon is an all-new machine that finally starts a new chapter in the legendary laptop’s design. As I said back then:

It’s unfortunate that Lenovo like most companies, when they hit on a winning hardware design (or buy one), will just tack on a few bells and whistles year after year instead of, you know, refining the design.

But no longer! Not only is the phone jack (!??!) gone, they’ve given up ethernet, VGA, and (I think… I’m not really an expert on this stuff…) the “PC Card” slot. There’s also no removable battery.

In other words they made a number of difficult tradeoffs, very much following in the footsteps of tradeoffs that Apple made years ago for the Air and for the same reasons: to innovate in design. But unlike most other companies following in the Air’s footsteps, Lenovo actually did innovate. Instead of yet another slippery metal or faux-metal case, the Carbon has grippy black plastic that looks distinguished and feels great.

Here are some X1 Carbons poorly composited with an enormous pencil.

Thinkpads X1 Carbon, giant pencil

Sensitive touchpads and Ubuntu

But this post’s title promises information about Ubuntu and I hope that googlers do indeed land here to find it. Because I had this laptop for 8 months before I spent a weekend day figuring out how to make the large super-sensitive touchpad work well with Ubuntu.

Under the default configuration it was just not possible for me to use tap-to-click, as I prefer, or even press-to-click. In both cases the cursor would jump right as the click registered. (i.e. the worst possible time.) So instead of clicking the coordinates I had painstakingly positioned the cursor above, I would be clicking some other dumb place and often as not miss the area defined for whatever action I was trying to take.

I just couldn’t use the big fancy touchpad much at all, and stuck with the trackpoint. What a shame.

Anyway, here’s my new config, the comments at the top tell you how to make it take effect. I keep the file under my home directory and softlink to where the system will read it. I’m pretty happy with this config. It’s not 100% perfect and I’ll probably be tweaking it until the day I die, but at least now I can use my touchpad without despair.

# softlink this file into:
# /usr/share/X11/xorg.conf.d

# and prevent the settings app from overwriting our settings:
# gsettings set org.gnome.settings-daemon.plugins.mouse active false


Section "InputClass"
    Identifier "nathan touchpad catchall"
    MatchIsTouchpad "on"
    MatchDevicePath "/dev/input/event*"
    Driver "synaptics"

    # three fingers for the middle button
    Option "TapButton3" "2"
    # drag lock
    Option "LockedDrags" "1"
    # accurate tap-to-click!
    Option "FingerLow" "50"
    Option "FingerHigh" "55"

    # prevents too many intentional clicks
    Option "PalmDetect" "0"

    # "natural" vertical and horizontal scrolling
    Option "VertTwoFingerScroll" "1"
    Option "VertScrollDelta" "-75"
    Option "HorizTwoFingerScroll" "1"
    Option "HorizScrollDelta" "-75"

    Option "MinSpeed" "1"
    Option "MaxSpeed" "1"

    Option "AccelerationProfile" "2"
    Option "ConstantDeceleration" "4"
EndSection

I hope this is helpful. I assume it is at least a step in the right direction for other laptops with big, sensitive touchpads.

For home use I’m getting antsy to replace my x201, as I am tiring of its tiny touchpad, paltry number of pixels, and general non-ultraness. Unless Lenovo makes a smaller version of the X1 Carbon I may have to jump ship for the Dell XPS 13 of all things. But something is telling me to wait a bit longer.

May 19, 2013 06:22 PM

May 14, 2013

Functional Jobs

Test Engineer at Klarna (Full-time)

For us QA should be part of the entire development process, from concept to execution. We believe in agile methodologies and believe that to be able to build largescale, world class systems we need to focus on the users, their needs and how we can make sure that we deliver what they want. Now we are looking for Test Engineers that share our view on how to do QA in product development. As a Test Engineer, you will be part of creating a world class agile software development organisation where QA is at the center. We are not there today, but that is of course just another challenge.

As a Test Engineer you are part of an agile development team that consists of around 5-8 members. You work closely to the developers and will develop the ways you work with testing within your team. The developers do unit tests and you will mostly focus on white-box testing but also some black-box testing. The test Engineer also creates tools and test frameworks when needed to be used inside the team.

We believe that you should automate what can be automated. This means that your mission is to implement test cases, automate tests and make sure we deliver in accordance to our high demands. Since we are forming our future test organisation it is a great opportunity to help setting up and develop our ways of testing. Make your ideas be part of our overall plan.

Requirements:

  • Degree in Computer science or similar degree with focus on programming
  • 3+ Years of experience in testing in medium- or largescale software development
  • Experience of agile software development (TDD / Scrum / Kanban)
  • Broad skills in programming (preferably functional programming such as Erlang, Lisp, Scala, Scheme, F#, Haskell etc)
  • Great skills in automation and scripting (Perl, Bash, Python etc)
  • Broad experience in white-box testing frameworks such as xUnit

Preferred skills:

  • ISTQB-certificate (or ISEB, CSTE)
  • Experience of BDD, Behaviour-driven Development, or ATDD, Acceptance Test Driven Development
  • Experience in different higher level testing frameworks (Selenium, Cucumber etc)

We offer you an international working environment filled with smart and ambitious colleagues. As an employee at one of Sweden’s fastest growing companies, you will play an important role in taking Klarna to the next level. You won’t work for Klarna, you’ll work with Klarna.‬

For questions please contact Philip Andrén at philip [dot] andren [at] klarna [dot] com or +46 (0) 76-526 00 70. We recommend you to apply as soon as possible, selection and interviews are held continuously.

Location Stockholm, Sweden

Get information on how to apply for this position.

May 14, 2013 07:28 AM

May 03, 2013

Tony Sloane

Scala worksheet for Sublime Text 3

My latest weekend hacking project is a worksheet plugin for Scala in Sublime Text 3. It was inspired by the Scala IDE for Eclipse worksheet plugin.

The idea of the Sublime Text plugin is that you enter Scala REPL commands into a file, then hit a keystroke to send the commands to the REPL. The plugin will display the results in another editor view and align them with the input so you can easily see which result was produced by which command. It’s a nice way to try some things out without having to re-enter or edit things in the REPL itself.

The project page has more details. Please let me know how it goes if you try it out.

by inkytonik (noreply@blogger.com) at May 03, 2013 01:38 AM

April 17, 2013

Adrian King

Sugar-Free Loops

Sugar-free loops: not a breakfast cereal.

I've recently been playing with a minimalist language that eschews pretty much all syntactic sugar. It has no classes (or typeclasses), no mutable state, no jumps or labels, no modules or packages. It has no libraries—and if it did, they'd be useless, because it has no identifiers with which to name the contents of the libraries.

The language is basically the lambda calculus, plus built-in integers, a few integer functions, and a conditional If. Variables are represented as zero-based De Bruijn indices, using the notation V(i) for the variable in the ith enclosing scope. V is actually a special case of the Var operator, whose argument can be an arbitrary expression rather than just an integer constant—useful if you want to invent arrays.

Calling this thing a “language” is probably an abuse of the term; it's just a data structure with an interpreter.

Although I wouldn't want to program such a thing on a regular basis, I did want to be able to process sequences of varying lengths with it. Given the pure functional nature of the language, this requires recursion. And given the lack of identifiers, the language doesn't do the recursion for me; I need to use an explicit fixpoint combinator. The call-by-value Y combinator in this language looks like:


fix =
Lam(
Lam(V(1)(Lam(V(1)(V(1))(V(0)))))(
Lam(V(1)(Lam(V(1)(V(1))(V(0)))))))

So I know I'll apply this to my looping function, but what does the looping function look like? Well, you can generally view a loop that computes a value as a fold. But unlike folds in high-level languages, there aren't have any prepackaged sequence operations to use in the fold, so I have to take those operations as parameters. Specifically, my fold (as passed to the Y comnbinator to create a usable recursive fold) will look like:


λf.λmore?.λhead.λtail.λstep.λaccum.λnext.
if not more? next then
accum
else
f more? head tail step (step accum (head next)) (tail next)

where more? applied to a sequence tells whether there are any more elements (the sequence is not empty), head provides the current element of the sequence and tail the rest, step performs one step of the fold operation, accum is the value accumulated by the fold, and next is the sequence from which to get the next element. Stripped of sugar, this becomes:


unfixedFold =
Lam( // f (fold itself): V(6)
Lam( // more: V(5)
Lam( // head: V(4)
Lam( // tail: V(3)
Lam( // step: V(2)
Lam( // accum: V(1)
Lam( // next: V(0)
If(
Not(V(5)(V(0))), // not more?
V(1), // no more, return accum
V(6)( // else apply f to
V(5))(V(4))(V(3))(V(2))(
// same more?, head, tail, step,
V(2)(V(1))(V(4)(V(0))))(
// step(accum,head(next)),
V(3)(V(0)))))))))))
// and tail(next)

The only form of input in this language is to bind the input to variables, as if the program were called from within a series of nested functions applied to the inputs. A program to iterate through such inputs and compute the maximum value looks like:


fix(unfixedFold)(
Lam(Lt(V(0),Minus(Depth,I(1)))))( // more?: variable index less
// than lexical depth
Lam(Var(Plus(V(0),I(1)))))( // head: index into inputs (add
// 1 to index to account for
// head function itself)
Lam(Plus(V(0),I(1))))( // tail: increment index
Lam(If(Gt(V(0),V(1)),V(0),V(1))))( // step: max of arguments
V(0))( // accum starts as zeroth input
I(1)) // index after zeroth input

Four out of five dentists do not recommend sugar-free languages, at least for their patients without patience.

by Archontophoenix (noreply@blogger.com) at April 17, 2013 04:55 AM

April 12, 2013

Erkki Lindpere

Slang: Review of 2012

Already the first quarter of 2013 is behind us, but I wanted to take a look whether I accomplished the goals I set for Slang development in 2012. I’m not going to reiterate the goals here, but almost none of them were completed.

I wanted to bring the language into a usable form, but so far there is still a lot of work to do. I wanted to make the compiler open source, but I’m still not sure at which point I would like to do that. I think I will postpone that quite a bit, because I don’t have the time to run an open source project at the moment (and parts of the compiler are still full of horrible hacks). Actually, lack of development time was one of the things that prevented me from fulfilling the goals. I worked on Slang mostly during vacations and when I found a drive to start work during other times I would continue for a couple of weeks and then take a break for a few months.

I am suspecting that development will continue at a similar pace. I have made some progress, though, sporadically during 2012 and more continuously during the last few weeks.

New Parser

I think I spent most of my Slang development time last year on the new parser, using the mixfix parser combinators I blogged about (sorry, I kind of forgot to post the third part of that). I have improved the parser framework a lot since then, but it does have a downside of being quite slow for large programs, even with Packrat parsers. So I have some further work to do here, like maybe replacing the whole parser again, but I will probably leave that to some unknown future date.

Custom Operator Precedence Rules

The new mixfix parser theoretically enabled a modular grammar: the default grammar can be relatively small, but programs could define their own prefix, postfix, infix and closed operators and their precedence rules. And recently, I made that actually work. You still can’t quite define a grammar per Slang source file, but you can add operator graph (.ops) files to the list of compiled files and together with the standard rules, they will define the grammar for that compiler run. Some examples.

New Compiler Infrastructure

I just finished the initial work on this, a step towards separate compilation: instead of including library code into the source file being compiled, now the new Slang compiler driver can compile multiple source files at the same time, but still requires that the library sources are available. The end result is linked together with the LLVM IR linker.

Slang IDE

I started work on an Eclipse-based Slang IDE. Currently it includes syntax highlighting, error reporting, a few quick fixes, and quick-assist for rewriting expressions to use different aliases of methods. E.g. if you write x.times(y) you can then click Ctrl+1 and select “Use alias: *”. Then the expression will be rewritten to x * y. This is much more useful if you have Unicode operators that can’t be easily typed. Another example: you write array(1), Ctrl+1 suggests “Use alias: subscript” and selecting it turns the expression into array₁

The IDE also shows warnings and errors with squiggly lines, and I added a few more useful warnings to the compiler and improved the messages.

Debugging

This is still in progress, but for some programs, Slang compiler can now emit DWARF debug info, and the programs can be debugged using the GNU Debugger. I was quite pleased to get that working, and together with the IDE, and new compiler driver, it has made Slang much more usable for myself.

Next Steps

This time, I don’t want to give any deadlines, but the next steps are to make the tooling more usable, improve error messages, add some of the language features I didn’t get to add in 2012, etc. I think I might even release a build in the form of the Slang IDE with the compiler embedded, even if it will not be open source.

I have various language features in mind that I want to explore, such as proper generic types, some form of dependent types, Strings, IO, better interoperability with C, JVM back-end. Strings and IO are pretty much given, and generics as well. Not sure how soon I’ll get to the other things.

I think I will pretty much be doing pain-driven development: what is currently giving me most pain will get fixed first. I think actually generics might be the thing, because adding collection methods for only a specific type of arrays is no fun.


by Erkki Lindpere at April 12, 2013 11:48 PM

April 11, 2013

scala-lang.org

Prof. Philip Wadler to Keynote at the Scala Workshop (Scala2013)!

We're excited to announce that Prof. Philip Wadler will be keynoting at this year's Scala Workshop (Scala2013)

Prof. Wadler is Professor of Theoretical Computer Science at the University of Edinburgh. An ACM Fellow, he is well-known for his seminal work on effectful computations in purely functional languages based on monads, as well as his contributions to the design of Haskell, Java, and XQuery.

The fourth Scala Workshop is the leading forum for research and development on or related to the Scala programming language. It will take place July 2nd, 2013, and will be co-located this year with ECOOP, ECMFA, and ECSA in Montpellier, France.

by heathermiller at April 11, 2013 02:42 PM

Scala Workshop (Scala2013) Student Talks to be Awarded Full ECOOP Registration!

Thanks to a generous donation from Typesafe and Oracle Labs, we will be awarding a limited number of accepted student talks with full ECOOP registration (a value of 350EUR). Student talks are designed to be 5-10 minutes in duration, presenting ongoing or completed research related to Scala, or announcing a project that would be of interest to the Scala community. To be considered, simply submit a title and abstract by April 12th (students may update/change title/abstract until the final April 19th deadline) at the Scala2013 website. In addition to student talks, we solicit full research papers, short research papers, and tool demos. 

by heathermiller at April 11, 2013 02:27 PM

April 08, 2013

Coderspiel

implicit.ly: dispatch 0.10.0

implicit.ly: dispatch 0.10.0:

implicitly-notes:

scala.concurrent.Future

From this release forward Dispatch uses the standard Future interface that was introduced in Scala 2.10 and backported for Scala 2.9.3. This required some breaking changes in the API, but the migration is straightforward.

Imports

The shortest import is now this:

import...

Sooooooooooo glad that’s done.

April 08, 2013 05:07 AM

April 07, 2013

Adrian King

No Comment

Of all the features ever introduced into programming languages, surely the ability to insert natural-language comments is the worst thought out.

Think about it: have you ever seen a comment of more than one line that wasn't out-of-date by the time you read it? Or, if the comment was more-or-less accurate, one that didn't at least contain distracting misspellings and mispunctuations?

An ideal programming language would let you write all comments in the language itself, so that the compiler could verify that comments are properly structured and consistent with the code they describe.

Now I realize this post is appearing just a few days after April 1st, but I'm not entirely joking. Static type checking is, in effect, a system of compiler-checked comments. Type checking, of course, has a role in avoiding errors—and I do find that I do make type errors that the compiler flags, and I appreciate the ability to correct those errors before runtime. But far more often than the compiler finds a type error in my code, I read the type annotations I've made, because from day to day I tend to forget the types I'm working with at any given point in the code. That is, type annotations—like pretty much everything else in programming language source code—are best thought of as more for human consumption than for the compiler.

One reason I enjoy working with Scala more than Haskell is precisely what many people consider to be a flaw in Scala, namely that Scala can't do type inference in many places where Haskell can. Scala code is consequently littered with type annotations in places where Haskell would omit them—but that means I don't have to look very far to be reminded what anything is. As with natural language, redundancy may take up more space or time in transmission, but if not overdone it's a great aid to comprehension.

I've written some Python, and I enjoy the speed and concision with which you can whip up some code when you don't have to bother with type annotations. But it's not a language I would use for code I planned to work on for a long time, or to share with other people; I don't think I could keep the types clear in my head long enough. Unless, of course, I wrote some natural-language comments to remind me. But then I'd really want the compiler to make sure those comments didn't get out of date.

I'm still looking forward to the compiler that will complain when I write a comment that says “here is an implementation of quicksort” when what I've actually coded is bubblesort. But perhaps, a few years from now, someone will announce such a thing. On April 1st.

by Archontophoenix (noreply@blogger.com) at April 07, 2013 08:18 PM

March 30, 2013

Tomás Lázaro

Automatic resources processing with caching in SBT

I just wrote an article on my game development blog titled Automatic texture packaging with Libgdx's TexturePacker. It talks about an SBT plugin I just published that does some automatic process and packaging of resources during the 'package' task.

I took advantage of SBT’s built in cache system. It’s possible to associate a task with a bunch of input files and cache the modification date. Same applies to the output files. That way the task only runs if something changed, otherwise it just does a no-op. This is extremely important when that task might take a long time.

I'm sure some people will find the source code useful to do something of their own and improve their sbt-fu. You can check it out here.

Please let me know what you think!

by Tomás Lázaro (noreply@blogger.com) at March 30, 2013 07:00 PM

March 26, 2013

James Iry

King Null the Stubborn

It's mostly pretty easy to eliminate null in a language: just replace it with Maybe/Option types. Unfortunately it's only mostly easy to get rid of null that way in class based OO languages. There's one corner where null is surprisingly stubborn without limiting OO languages: initialization. I'll demonstrate using Java but I'll show that the same or similar problem can manifest in most class

by James Iry (noreply@blogger.com) at March 26, 2013 07:53 PM

March 20, 2013

scala-lang.org

Google Summer of Code 2013 Scala Projects

This year the Scala team applied again for the Google Summer of Code program to work with enthusiastic students on challenging Scala projects and got accepted!

by vogt at March 20, 2013 12:33 PM

March 15, 2013

Coderspiel

Blake Matheny: Eliminating Duplicate Requests

Blake Matheny: Eliminating Duplicate Requests:

mobocracy:

If you’re building a backend service and can avoid the problem of serving duplicate requests, that’s a good thing. A duplicate is defined in this case as: if two requests R1 and R2 both have a response r_1, they are duplicates. Eliminating duplicates avoids doing unnecessary additional work.

At…

March 15, 2013 02:20 AM

March 14, 2013

The Careful Programmer

Clojure: from zero to grok

What's often missing when trying to learn a programming language is something between the 10 thousand feet overview and the medium to high tutorial assuming you've bootstrap yourself.

Eric Normand is offering insanely affordable Clojure learning videos. It's a Kickstarter campaign with only 6 hours left!

You can donate in all confidence, the main goal has been reached, but we need your help to reach the first stretch goal.

If you've ever wondered what all the hoopla about Clojure is, you can find out for a few bucks.

You'll learn enough to put it aside if you don't like it, or if you're lucky, you'll ignite a passion!

Click on the link. Do it now 8)

LispCast introduction to Clojure videos

by The Careful Programmer (noreply@blogger.com) at March 14, 2013 11:57 AM

March 13, 2013

scala-lang.org

Scala 2.10.1 now available!

We are very happy to announce the final release of Scala 2.10.1! To make your upgrade seamless, this release is fully binary compatible with Scala 2.10.0.

The Scala team and contributors fixed 166 reported issues since 2.10.0. In total, 242 RC1 pull requests, 7 RC2 pull requests, and 4 RC3 pull requests were opened on GitHub, of which 94.5% were merged after having been tested and reviewed.

by moors at March 13, 2013 01:12 AM

March 12, 2013

Jesper Nordenberg

A More Efficient Option

Updated: 2013-03-12

Scala's Option type is a big improvement in type safety over Java's null checking and NullPointerException's. Unfortunately when wrapping a value in Some there is a quite big overhead: creation of one additional object containing a reference to the value. It would be more efficient if we could represent Some as the unboxed pointer value and encode None as the null value, but at the same time preserving the type safety as we get with the Option type (for example Kotlin uses this approach in it's builtin support for nullable types). Well, we can do exactly this with value classes introduced in Scala 2.10:


final case class Option[+T](value: Any = null) extends AnyVal with Product with Serializable {
private def unsafeGet = value.asInstanceOf[T]
def isEmpty: Boolean = value == null
...
}

The reason that the class parameter is of type Any and not T is that it's not allowed to create a value of type Nothing. We still want to be able to create a None value of type Option[Nothing] though so we delay the unsafe cast to T (using the unsafeGet method) until the value is actually required and we've checked that it's actually there using the isEmpty method.

The code is on GitHub if someone wants to try it out. It's pretty much a dropin replacement for the standard Scala Option type.

Memory Usage Comparisons

Below is a comparison in memory usage between the scala.Option type and the unboxed AnyVal option type when allocating 1M objects each containing one optional value:


scala.Some: 33 MB
scala.None: 19 MB
scala.Some : Any: 34 MB
scala.None : Any: 19 MB
AnyVal Some: 19 MB
AnyVal None: 19 MB
AnyVal Some : Any: 34 MB
AnyVal None : Any: 34 MB

As one would expect, memory usage for Some is almost halved and for None the memory usage is unchanged. The downside of using the AnyVal option is that it uses more memory when a None is upcasted to a super type because then the compiler will box the reference. I would assume this is a quite rare case though.

by Jesper Nordenberg (noreply@blogger.com) at March 12, 2013 10:14 PM

scala-lang.org

4th Scala Days Conference to run in New York City

The fourth annual Scala Days will be held this year at The Hudson Theater in NYC, June 10th-12th. Now is the time to submit speaking sessions and register to attend!

by odersky at March 12, 2013 05:47 PM

March 11, 2013

Eric Torreborre

The Essence of the Iterator Pattern

"The Essence of the Iterator Pattern"(EIP) is the paper I liked the most last year. It gave me a brand new look over something which I had be using for years: the for loop.

In this post I'll try to present some ideas of that paper and show how to implement the solution described by the authors using Scalaz-like code. A minimum previous exposure to functional programming, functors and monads will definitely help!

What's in a for loop?

That was really what hooked me. What do you mean "what's in a for loop"?. Is there anything magic in that construct I've been using for years?

The introduction of EIP shows an example of a for loop to iterate on elements (not the C-like for used with an index). I'm transmogrifying it here to Scala but the idea remains the same:

  val basket: Basket[Fruit] = Basket(orange, apple)
var count = 0

val juices = Basket[Juice]()
for (fruit <- basket) {
count = count + 1
juices.add(fruit.press)
}

We start from a "container" of fruits: Basket. It could actually be anything, a List, a Tree, a Map... Then the for loop actually does 3 things:

  1. it returns a container having the same "shape"; juices is still a Basket
  2. it accumulates some kind of measure. Here, the number of fruits in the count variable
  3. it maps the elements to other elements: pressing the fruits to get some juice

And this for loop is actually not the most complex:

  • the count variable could influence the mapping of elements: juices.add(fruit.press(harder=count))
  • we could have several variables depending on each other: cumulative = cumulative + count
  • the mapping could also influence a "measure" variable: liquid = liquid + fruit.press.quantity

The purpose of EIP is to show that the "essence" of what happens in the for loop above can be abstracted by an Applicative Traversal. And the authors go on showing that given this Applicative abstraction, we get an incredible modularity for programming.

The Applicative typeclass

How can an Applicative traversal be better than a for loop, and what does that even mean?? EIP has a lot of sentences and expressions which can be hard to grasp if you don't have a strong functional programming / Haskell background. Let's try to dissect that slowly and start with the formal definitions anyway.

What is a Functor?

The first thing we need to talk about is the Functor typeclass:

  trait Functor[F[_]] {
def fmap[A, B](f: A => B): F[A] => F[B]
}

One way of interpreting a Functor is to describe it as a computation of values of type A. For example List[A] is a computation returning several values of type A (non-deterministic computation), Option[A] is for computations that you may or may not have, Future[A] is a computation of a value of type A that you will get later, and so on. Another way of picturing it is as some kind of "container" for values of type A.

Saying that those computations are Functors is essentially showing that we can have a very useful way of combining them with regular functions. We can apply a function to the value that is being computed. Given a value F[A] and a function f, we can apply that function to the value with fmap. For example, fmap is a simple map for a List or an Option.

Pointed Functor

By the way, how do you even create a value of type F[A]? One way to do that is to say that F[_] is Pointed:

  trait Pointed[F[_]] {
def point[A](a: => A): F[A]
}

That is, there is a point function taking a value of type A and returning a F[A]. For example, a regular List is Pointed just by using the constructor for Lists:

  object PointedList extends Pointed[List] {
def point[A](a: => A) = List(a)
}

Then combining the 2 capabilities, pointed and functor, gives you a PointedFunctor:

  trait PointedFunctor[F[_]] {
val functor: Functor[F]
val pointed: Pointed[F]

def point[A](a: => A): F[A] = pointed.point(a)

def fmap[A, B](f: A => B): F[A] => F[B] = functor.fmap(f)
}

The PointedFunctor trait is merely the aggregation of a Pointed and a Functor.

What about Applicative then? We're getting to it, the last missing piece is Applic.

Applic

Applic is another way to combine a "container" with a function.

Instead of using fmap to apply the function to the computed value we suppose that the function is itself a computed value inside the container F (F[A => B]) and we provide a method applic to apply that function to a value F[A]:

  trait Applic[F[_]] {
def applic[A, B](f: F[A => B]): F[A] => F[B]
}

Let's take an example. Say I have a way to compute the price of a Fruit when the market is open:

  def pricer(market: Market): Option[Fruit => Double]

If the market is closed, pricer returns None, because we don't know what are the prices. Otherwise it returns a pricing function. Now if I have a grow function possibly returning a Fruit:

  def grow: Option[Fruit]

Then, using the Applic instance, you can price the Fruit:

  val price: Option[Double] = applic(pricer(market)).apply(grow)

The price will necessarily be an Option because you may not have a pricer nor a Fruit to price. And a bit of renaming and pimping reveals why we're using the term "Applicative":

  val pricingFunction = pricer(market)
val fruit = grow

val price: Option[Double] = pricingFunction ⊛ fruit

In a way we're just doing a normal function application, but we're just doing it inside the Applicative container. Now we have all the pieces to build the Applicative functor that EIP is talking about.

Applicative Functor

An Applicative Functor is the aggregation of an Applic and a PointedFunctor:

  trait Applicative[F[_]] {
val pointedFunctor: PointedFunctor[F]
val applic: Applic[F]

def functor: Functor[F] = new Functor[F] {
def fmap[A, B](f: A => B) = pointedFunctor fmap f
}
def pointed: Pointed[F] = new Pointed[F] {
def point[A](a: => A) = pointedFunctor point a
}

def fmap[A, B](f: A => B): F[A] => F[B] = functor.fmap(f)
def point[A](a: => A): F[A] = pointed.point(a)
def apply[A, B](f: F[A => B]): F[A] => F[B] = applic.applic(f)
}

Let's see how that can be implemented for a List. fmap and point are straightforward:

   def fmap[A, B](f: A => B): F[A] => F[B] = (l: List[A]) => l map f
def point[A](a: => A): F[A] = List(a)

apply turns out to be more interesting because there are 2 ways to implement it, both of them being useful:

  1. apply a list of functions to each element and gather the results in a List:

    def apply[A, B](f: F[A => B]): F[A] => F[B] = (l: List[A]) =>
    for { a <- l; func <- f } yield func(a)
  2. zip the list of functions to the list of elements to apply each function to each element

    def apply[A, B](f: F[A => B]): F[A] => F[B] = (l: List[A]) =>
    (l zip f) map (p => p._2 apply p._1)

There is even a third way to use List as an Applicative by using the fact that List is a Monoid. But more on that later, for now we still have to see how all of this relates to the for loop...

Traversing the structure

When we do a for loop, we take a "structure" containing some elements and we "traverse" it to return:

  • that same structure containing other elements
  • a value computed from the structure elements
  • some combination of above

Gibbons & Oliveira argue that any kind of for loop can be represented as the following traverse operation:

  trait Traversable[T[_]] {
def traverse[F[_] : Applicative, A, B](f: A => F[B]): T[A] => F[T[B]]
}

That is, if the container/structure of type T has this traverse function using an Applicative F, then we can do whatever we would do with a for loop on it.

To get a better feel for this traverse function, we're going to implement the Traversable trait for a binary tree and then we'll see how can we loop on that tree.

A Binary Tree

For all the other examples in this post, we're going to use a very simple binary tree:

  sealed trait BinaryTree[A]
case class Leaf[A](a: A) extends BinaryTree[A]
case class Bin[A](left: BinaryTree[A], right: BinaryTree[A]) extends BinaryTree[A]

On the other hand, the first shot at the Traversable implementation is barely readable!

 def BinaryTreeIsTraversable[A]: Traversable[BinaryTree] = new Traversable[BinaryTree] {

def createLeaf[B] = (n: B) => (Leaf(n): (BinaryTree[B]))
def createBin[B] = (nl: BinaryTree[B]) =>
(nr: BinaryTree[B]) => (Bin(nl, nr): BinaryTree[B])

def traverse[F[_] : Applicative, A, B](f: A => F[B]):
BinaryTree[A] => F[BinaryTree[B]] = (t: BinaryTree[A]) => {
val applicative = implicitly[Applicative[F]]
t match {
case Leaf(a) => applicative.apply(applicative.point(createLeaf[B]))(f(a))
case Bin(l, r) =>
applicative.apply(applicative.apply(applicative.point(createBin[B]))(traverse[F, A, B](f).apply(l))).
apply(traverse[F, A, B](f).apply(r))
}
}
}

This is a shame because the corresponding Haskell code is so concise:

  instance Traversable Tree where
traverse f (Leaf x) = pure Leaf ⊛ f x
traverse f (Bin t u) = pure Bin ⊛ traverse f t ⊛ traverse f u

A bit of pimping to the rescue and we can improve the situation:

def traverse[F[_] : Applicative, A, B](f: A => F[B]): BinaryTree[A] => F[BinaryTree[B]] = (t: BinaryTree[A]) => {
t match {
case Leaf(a) => createLeaf[B] ∘ f(a)
case Bin(l, r) => createBin[B] ∘ (l traverse f) <*> (r traverse f)
}
}

Informally the traverse method applies the function f to each node and "reconstructs" the tree by using the apply method (<*>) of the Applicative functor.

That's certainly still some Ancient Chinese to you (as it was for me) so we'd better see the traverse method at work now. But we need to take another detour :-)

Applicative Monoid

One simple thing we might want to do, when iterating on a BinaryTree, is to get the content of that tree in a List. To do that, we're going to use the 3rd way to use List as an Applicative, as mentioned earlier. It turns out indeed that any Monoid (what is it?) gives rise to an Applicative instance but in a way that's a bit surprising.

  /** Const is a container for values of type M, with a "phantom" type A */
case class Const[M, +A](value: M)

implicit def ConstIsPointed[M : Monoid] = new Pointed[({type l[A]=Const[M, A]})#l] {
def point[A](a: => A) = Const[M, A](implicitly[Monoid[M]].z)
}

implicit def ConstIsFunctor[M : Monoid] = new Functor[({type l[A]=Const[M, A]})#l] {
def fmap[A, B](f: A => B) = (c: Const[M, A]) => Const[M, B](c.value)
}

implicit def ConstIsApplic[M : Monoid] = new Applic[({type l[A]=Const[M, A]})#l] {
def applic[A, B](f: Const[M, A => B]) = (c: Const[M, A]) => Const[M, B](implicitly[Monoid[M]].append(f.value, c.value))
}

implicit def ConstIsPointedFunctor[M : Monoid] = new PointedFunctor[({type l[A]=Const[M, A]})#l] {
val functor = Functor.ConstIsFunctor
val pointed = Pointed.ConstIsPointed
}

implicit def ConstIsApplicative[M : Monoid] = new Applicative[({type l[A]=Const[M, A]})#l] {
val pointedFunctor = PointedFunctor.ConstIsPointedFunctor
val applic = Applic.ConstIsApplic
}

In the code above, Const is the Applicative instance for a given Monoid. Const contains values of type T where T is a Monoid and we progressively establish what are the properties that Const must satisfy to be Applicative.

  • it must first be Pointed. Informally, the point method puts the neutral element of the Monoid in a Const instance

  • then it must be a Functor. Here the fmap function doesn't do anything but changing the type of Const from Const[M, A] to Const[M, B]

  • finally it must be an Applic where the apply method of Applic uses the append method of the Monoid to "add" 2 values and return the result in a Const instance.

There is unfortunately a lot of typing vodoo thing here:

  • the type declaration for Const is Const[A, +B]. It has a type parameter B which is actually not represented by a value in the Const class! It is a phantom type. But it is actually indispensable to match the type declarations of the typeclasses

  • the type F that is supposed to be Applicative is... ({type l[A] = Const[T, A]})#l. Ouch, this deserves some explanation!

What we want is not so hard. The type Const[A, B] has 2 type parameters. We need a way to fix A to be T and get the resulting type which will have only one type parameter. The expression above is the most concise way to get this desired type:

  • { type l = SomeType } is an anonymous type with a type member called l. We can access that type l in Scala by using #: { type l = SomeType }#l

  • Then, in { type l[A] = SomeType[T, A] }#l, l is a higher-kinded type, having a type variable A (actually SomeType[T, A] where T is fixed)

That was a really long detour for a mere for loop, isn't it? Now... profit!


Contents of a BinaryTree...

We're going to use the Traversable instance for the BinaryTree and the List Monoid Applicative to get the contents of a BinaryTree:

  import Applicative._

val f = (i: Int) => List(i)
val tree = Bin(Leaf(1), Leaf(2))

(tree.traverse[...](f)).value must_== List(1, 2)

Simple, for each element of the tree, we put it in a List then we let the List Monoid do its magic and aggregate all the results as we traverse the tree. The only difficulty here is the limits of Scala type inference. The ... stands for type annotations that the compiler requires:

  tree.traverse[Int, ({type l[A]=Const[List[Int], A]})#l](f)

Not pretty :-(

Update: As pointed out by Ittay Dror in the comments, List[Int] is not an applicative by itself and we need to put this list into a Const value to make it usable by the traverse function.

This is actually done by an implicit conversion method, liftConst, provided by the Applicative object:
  implicit def liftConst[A, B, M : Monoid](f: A => M): A => Const[M, B] = 
(a: A) => Const[M, B](f(a))

Profit time

Not everything is lost! We can encapsulate a bit the complexity in this case. We can extract part of the code above and create a contents method which will work on any of Traversable instance (assume I'm pimping the following examples so that I can write tree.method instead of method(tree)):

  val tree: BinaryTree[Int] = Bin(Leaf(1), Leaf(2))
tree.contents must_== List(1, 2)

This is based on the following definition:

  def contents[A]: T[A] => List[A] = {
val f = (a: A) => Const[List[A], Any](List(a))
(ta: T[A]) => traverse[({type l[U]=Const[List[A], U]})#l, A, Any](f).apply(ta).value
}

It also turns out that the contents function is a specialized version of something even more generic, the reduce function, working with any Monoid:

  def contents[A]: T[A] => List[A] = reduce((a: A) => List(a))

def reduce[A, M : Monoid](reducer: A => M): T[A] => M = {
val f = (a: A) => Const[M, Any](reducer(a))
(ta: T[A]) => traverse[({type l[A]=Const[M, A]})#l, A, Any](f).apply(ta).value
}

The reduce function can traverse any Traversable structure with a function mapping each element to a Monoid element. We've used it to get the contents of the tree but can as easily get the number of elements:

  def count[A]: T[A] => Int = reduce((a: A) => 1)

tree.count must_== 2

Can it get simpler than this :-)? Actually in that case it can! Since we don't need (a: A) at all we can use reduceConst:

  def reduceConst[A, M : Monoid](m: M): T[A] => M = reduce((a: A) => m)

def count[A]: T[A] => Int = reduceConst(1)

It's like a Scala standard reduce on steroids because instead you don't need to provide a binary operation, you just need a Monoid instance.

.... and shape of a BinaryTree

We've addressed the question of doing some kind of accumulation based on the elements in the tree, now we're going to "map" them.

Monads are Applicatives too!

The following map method can be derived from the traverse method (note that no type annotations are necessary in that case, yes!):

  def map[A, B](mapper: A => B) = (ta: T[A]) => traverse((a: A) => Ident(mapper(a))).apply(ta).value

Here we're traversing with an Applicative which is very simple, the Ident class:

  case class Ident[A](value: A)

The Ident class is a simple wrapper around a value, nothing more. That simple class is an Applicative. But how?

Easy. Ident is actually a Monad and we can construct an Applicative instance from every Monad. This comes from the fact that a Monad is both a PointedFunctor and an Applic:

  trait Monad[F[_]] {
val pointed: Pointed[F]
val bind: Bind[F]

def functor: Functor[F] = new Functor[F] {
def fmap[A, B](f: A => B): F[A] => F[B] = (fa: F[A]) =>
bind.bind((a: A) => pointed.point(f(a))).apply(fa)
}

def pointedFunctor: PointedFunctor[F] = new PointedFunctor[F] {
val functor = Monad.this.functor
val pointed = Monad.this.pointed
}

def applic: Applic[F] = new Applic[F] {
def applic[A, B](f: F[A => B]) = a =>
bind.bind[A => B, B](ff => functor.fmap(ff)(a))(f)
}

def applicative: Applicative[F] = new Applicative[F] {
val pointedFunctor = Monad.this.pointedFunctor
val applic = Monad.this.applic
}
}

And the Ident class is trivially a Monad (having a pointed and a bind member):

  implicit def IdentIsMonad = new Monad[Ident] {

val pointed = new Pointed[Ident] {
def point[A](a: => A): Ident[A] = Ident(a)
}
val bind = new Bind[Ident] {
def bind[A, B](f: A => Ident[B]): Ident[A] => Ident[B] =
(i: Ident[A]) => f(i.value)
}

}

We can use our brand new map function now:

  tree.map((i: Int) => i.toString) must_== Bin(Leaf("1"), Leaf("2"))

We can even use it to get the "shape" of our container and discard all the elements:

  tree.shape must_== Bin(Leaf(()), Leaf(()))

The shape method just maps each element to ().

Decompose / Compose

I recap. We implemented a very generic way to iterate over a structure, any kind of structure (as long as it's Traversable), containing elements, any kind of element, with a function which does an "application", any kind of application. Among the possible "applications", we've seen 2 examples: collecting and mapping which are the essential operations that we usually do in a for loop.

Specifically we were able to get the contents of a tree and its shape. Is there a way to compose those 2 operations into a decompose operation that would get both the content and the shape at once? Our first attempt might be:

  def decompose[A] = (t: T[A]) => (shape(t), contents(t))

tree.decompose must_== (Bin(Leaf(()), Leaf(())), List(1, 2))

This works but it is pretty naive because this requires 2 traversals of the tree. Is that possible to do just one?

Applicative products

This is indeed possible by noticing the following: the product of 2 Applicatives is still an Applicative.

Proof, proof. We define Product as:

  case class Product[F1[_], F2[_], A](first: F1[A], second: F2[A]) {
def tuple = (first, second)
}

I spare you the full definition of Product as an Applicative to just focus on the Applic instance:

  implicit def ProductIsApplic[F1[_] : Applic, F2[_] : Applic] =
new Applic[({type l[A]=Product[F1, F2, A]})#l] {
val f1 = implicitly[Applic[F1]]
val f2 = implicitly[Applic[F2]]

def applic[A, B](f: Product[F1, F2, A => B]) = (c: Product[F1, F2, A]) =>
Product[F1, F2, B](f1.applic(f.first).apply(c.first),
f2.applic(f.second).apply(c.second))
}

That's not too complicated, you just have to follow the types. What's more troubling is the amount of type annotations which are necessary to implement decompose. Ideally we would like to write:

  def decompose[A] = traverse((t: T[A]) => shape(t) ⊗ contents(t))

Where is an operation taking 2 Applicatives and returning their product. Again the lack of partial type application for Const muddies the whole (upvote SI-2712 please!):

val shape   = (a: A) => Ident(())
val content = (a: A) => Const[List[A], Unit](List(a))

val product = (a: A) => (shape(a).⊗[({type l[T] = Const[List[A], T]})#l](content(a)))

implicit val productApplicative =
ProductIsApplicative[Ident, ({type l1[U] = Const[List[A], U]})#l1]

(ta: T[A]) => { val (Ident(s), Const(c)) =
traverse[({type l[V] = Product[Ident, ({type l1[U] = Const[List[A], U]})#l1, V]})#l, A, Unit](product).
apply(ta).tuple
(s, c)
}

We can improve the code sligthly by moving the implicit definition for productApplicative inside the Applicative companion object:

  object Applicative {
...
implicit def ProductWithListIsApplicative[A[_] : Applicative, B] =
ProductIsApplicative[A, ({type l1[U] = Const[List[B], U]})#l1]
}

Then no implicit val productApplicative is necessary and the Applicative imports will be all we need.

Collection and dispersal

There is another way to do things "in parallel" while traversing the structure. The collect method that we're going to build will do 2 things:

  • it will accumulate some kind of state, based on the elements that we meet

  • it will map each element to another kind of element

So, as we're iterating, we can do a regular mapping while computing some kind of measure. But before that, we need to take a little detour (again?? Yes, again) with the State monad.

The State monad

The State Monad is defined by:

  trait State[S, +A] {
def apply(s: S): (S, A)
}

It is basically:

  • an object keeping some previous "state", of type S
  • a method to extract a meaningful value from this "state", of type A

  • this method computes a new "state", of type S

For example, a simple counter for the number of elements in a List[Int] can be implemented by:

  val count = state((n: Int) => (n+1, ()))

It takes the previous "count" number n and returns the new state n+1 and the extracted value (() here, because we don't need to extract anything special).

The State type above is a Monad. I encourage you to read "Learn You a Haskell" to get a better understanding on the subject. I will just show here that the flatMap (or bind) method of the Monad typeclass is central in putting that State to work:

  val count = (s: String) => state((n: Int) => (n+1, s + n))

(count("a-") flatMap count flatMap count).apply(0) must_== (3, "a-012")

The count function takes the latest computed string and returns a State where we increment the current "state" by 1 and we have a new String as the result, where the current count is appended. So when we start with the string "a-" and we flatMap count 2 times, we get (3, "a-012") where 3 is the number of times we've applied the n+1 function and "a-012" the result of appending to the current string.

By the way, why do we need to apply(0)?

When we do all the flatMaps, we actually store "stateful computations". And they are executed only once we provide the initial state: 0!

Collecting elements

Let's now define a collect operation on Traversable which will help us to count:

  def collect[F[_] : Applicative, A, B](f: A => F[Unit], g: A => B) = {
val applicative = implicitly[Applicative[F]]
import applicative._

val application = (a: A) => point((u: Unit) => g(a)) <*> f(a)
traverse(application)
}

This collect operation, defined in EIP, is different from the collect operation on Scala collections which is the equivalent of filter + map. The collect of EIP is using 2 functions:

  • f: A => F[Unit] which collects data from each element "effectfully" (that is, possibly keeping state)

  • g: A => B which maps each element to something else

So we could say that the EIP collect is a bit like fold + map. Knowing this, we can use collect to count elements and do some mapping:

  val count = (i: Int) => state((n: Int) => (n+1, ()))
val map = (i: Int) => i.toString

tree.collect[({type l[A]=State[Int, A]})#l, String](count, map).apply(0) must_==
(2, Bin(Leaf("1"), Leaf("2")))

Here again the type annotations are obscuring the intent a bit and if type inference was perfect we would just read:

  val count = (i: Int) => state((n: Int) => (n+1, ()))
val map = (i: Int) => i.toString

tree.collect(count, map).apply(0) must_== (2, Bin(Leaf("1"), Leaf("2")))

I don't know about you, but I find this a bit magical. With the Applicative and Traversable abstractions, we can assemble our program based on 2 independent functions possibly developed and tested elsewhere.

Dispersing elements

The next utility function proposed by EIP is the disperse function. Its signature is:

  def disperse[F[_] : Applicative, A, B, C](f: F[B], g: A => B => C): T[A] => F[T[C]]

What does it do?

  • f is the Applicative context that's going to evolve when we traverse the structure, but regardless of what the elements of type A are
  • g is a function which, for each element of type A says what to do with the current context value, B, and map that element back to the structure

Please, please, a concrete example!

Say I want to mark each element of a BinaryTree with its "number" in the Traversal (the "label"). Moreover I want to use the element name to be able to qualify this label:

  // a BinaryTree of Doubles
val tree: BinaryTree[Double] = Bin(Leaf(1.1), Bin(Leaf(2.2), Leaf(3.3)))

// the "label" state returning integers in sequence
val labelling: State[Int, Int] = state((n: Int) => (n+1, n+1))

// for each element in the tree, and its label,
// produce a String with the name and label
val naming: Double => Int => String = (p1: Double) => (p2: Int) => p1+" node is "+p2

// testing by applying an initial state (label `0`) and
// taking the second element of the pair `(last label, resulting tree)`
tree.disperse[elided for sanity](labelling, naming).apply(0)._2 must_==
Bin(Leaf("1.1 node is 1"), Bin(Leaf("2.2 node is 2"), Leaf("3.3 node is 3")))

Note that the naming function above is curried. A more familiar way to write it would be:

  val naming: (Double, Int) => String = (p1: Double, p2: Int) => p1+" node is "+p2

But then you would have to curry that function to be able to use it with the disperse function:

  tree.disperse[...](labelling, naming.curried)

The implementation of disperse is:

  def disperse[F[_] : Applicative, A, B, C](f: F[B], g: A => B => C) = {
val applicative = implicitly[Applicative[F]]
import applicative._

val application = (a: A) => point(g(a)) <*> f
traverse(application)
}

It is using the very capabilities of the applicative functor, the point method and the <*> application.

An overview of traversals

We've seen in the 2 examples above that we get different, specialized, versions of the traverse function by constraining how mapping and Applicative effects occur. Here's a tentative table for classifying other specialized versions of the traverse function:

function map element create state mapped depend on state state depend on element
collect X X X
disperse X X X
measure X X
traverse X X X X
reduce X X
reduceConst X
map X

The only function we haven't shown before is measure. It is mapping and accumulating state but this accumulation does not depend on the current element. Here's an example:

  val crosses = state((s: String) => (s+"x", ()))
val map = (i: Int) => i.toString

tree.measure(crosses, map).apply("") must_==
("xxx", Bin(Leaf("1"), Bin(Leaf("2"), Leaf("3"))))

Other than not looking very useful, the code above is also lying! It is not possible to have a measure function accepting a State monad without having to provide the usual ugly type annotations. So the actual example is:

  tree.measureState(crosses, map).apply("") must_== 
("xxx", Bin(Leaf("1"), Bin(Leaf("2"), Leaf("3"))))

where measureState is a specialization of the measure method to States. I think that one take-away of this post is that it might be beneficial to specialize a few generic functions in Scala , like traverse, collect... for Const and State in order to avoid type annotations.

Composing traversals

There's another axis of composition that we haven't exploited yet.

In a for loop, without thinking about it, you may write:

  for (a <- as) {
val currentSize = a.size
total += currentSize
result.add(total)
}

In the body of that for loop, you have statements with dependency on each other. In an Applicative traversal, this translates to the Sequential composition of Applicatives. From 2 Applicatives, we can create a third one which is their Sequential composition. More precisely, this means that if F1[_] and F2[_] are Applicatives then F1[F2[_]] is an Applicative as well. You want the demonstration? Ok, go.

First, we introduce a utility function on ApplicFunctors:

  def liftA2[A, B, C](function: A => B => C): F[A] => F[B] => F[C] = 
fa => applic.applic(functor.fmap(function)(fa))

liftA2 allows to lift a regular function of 2 arguments to a function working on the Applicative arguments. This is using the fact that an ApplicFunctor is a Functor so we can apply function: A => B => C to the "a in the box", to get a F[B => C] "in the box". And then, an ApplicFunctor is an Applic, so we can "apply" F[B] to get a F[C]

Armed with this function, we can write the applic method for F1[F2[_]]:

  implicit val f1ApplicFunctor = implicitly[ApplicFunctor[F1]]
implicit val f2ApplicFunctor = implicitly[ApplicFunctor[F2]]

val applic = new Applic[({type l[A]=F1[F2[A]]})#l] {
def applic[A, B](f: F1[F2[A => B]]) = (c: F1[F2[A]]) => {
f1ApplicFunctor.liftA2((ff: F2[A => B]) => f2ApplicFunctor.apply(ff))(f).apply(c)
}
}

It's not so easy to get an intuition for what the code above is doing except that saying that we're using previous definitions to allow a F1[F2[A => B]] to be applied to F1[F2[A]].

In mere mortal terms, this means that if we do an Applicative computation inside a loop and if we reuse that computation in another Applicative computation, we still get an Applicative computation. The EIP illustration of this principle is a crazy function, the assemble function.

The assemble function

The assemble function takes the shape of a Traversable and a list of elements. If there are enough elements it returns Some[Traversable] filled with all the elements (+ the reminder), otherwise it returns None (and an empty list). Let's see it in action:

        // the "shape" to fill
val shape: BinaryTree[Unit] = Bin(Leaf(()), Leaf(()))

// we assemble the tree with an exact list of elements
shape.assemble(List(1, 2)) must_== (List(), Some(Bin(Leaf(1), Leaf(2))))

// we assemble the tree with more elements
shape.assemble(List(1, 2, 3)) must_== (List(3), Some(Bin(Leaf(1), Leaf(2))))

// we assemble the tree with not enough elements
shape.assemble(List(1)) must_== (List(), None)

What's the implementation of the assemble function? The implementation uses 2 Monads (which are also Applicatives as we know now):

  • the State[List[Int], _] Monad is going to keep track of what we've already consumed
  • the Option[_] Monad is going to provide, or not, an element to put in the structure
  • the composition of those 2 monads is State[List[Int], Option[_]] (our F1[F2[_]] in the ApplicFunctor definitions above

So we just need to traverse the BinaryTree with one function:

def takeHead: State[List[B], Option[B]] = state { s: List[B] =>
s match {
case Nil => (Nil, None)
case x :: xs => (xs, Some(x))
}
}

The takeHead function is a State instance where each state application removes the first element of the list of elements if possible, and returns it in an Option.
This is why the result of the assemble function, once we apply it to a list of elements, is of type (List[Int], Option[BinaryTree[Int]]).

A recursive implementation

Just for the fun of comparison, I'm going to write a recursive version doing the same thing:

  def assemble(es: List[Int], s: BinaryTree[Unit]) : (List[Int], Option[BinaryTree[Int]]) = {
(es, s) match {
case (Nil, _) => (es, None)
case (e :: rest, Leaf(())) => (rest, Some(Leaf(e)))
case (_, Bin(left, right)) => {
assemble(es, left) match {
case (l, None) => (l, None)
case (Nil, Some(l)) => (Nil, None)
case (rest, Some(l)) => assemble(rest, right) match {
case (r, None) => (r, None)
case (finalRest, Some(r)) => (finalRest, Some(Bin(l, r)))
}
}
}
}
}
assemble(List(1, 2, 3), shape) must_== (List(3), Some(Bin(Leaf(1), Leaf(2))))

It works, but it makes my head spin!

A classical for-loop implementation

By the way, what would be the real for loop version of that functionality? That one is not so easy to come up with because AFAIK there's no easy way to iterate on a BinaryTree to get a similar BinaryTree with just a for loop! So, for the sake of the argument, we're going to do something similar with just a List structure:

  def assemble[T](es: List[T], shape: List[Unit]) = {
var elements = es
var list: Option[List[T]] = None
for (u <- shape) {
if (!elements.isEmpty) {
list match {
case None => list = Some(List(elements.first))
case Some(l) => list = Some(l :+ elements.first)
}
elements = elements.drop(1)
} else {
list = None
}
}
(elements, list)
}
assemble(List(1, 2, 3), List((), ())) must_== (List(3), Some(List(1, 2)))

Contrast and compare with:

  List((), ()).assemble(List(1, 2, 3)) must_== (List(3), Some(List(1, 2)))

where you just define List as a Traversable:

  implicit def ListIsTraversable[A]: Traversable[List] = new Traversable[List] {

def traverse[F[_] : Applicative, A, B](f: A => F[B]): List[A] => F[List[B]] =
(l: List[A]) => {
val applicative = implicitly[Applicative[F]]
l match {
case Nil => applicative.point(List[B]())
case a :: rest =>
((_:B) :: (_: List[B])).curried ∘ f(a) <*> (rest traverse f)
}
}

}

The Applicative composition is indeed very powerful, but we're going to see that there are other ways to compose functions and use them with Traversables.

Monadic composition

This paragraph is exploring the fine relationships between applicative composition and monadic composition when doing traversals. We've seen that Applicative instances can be composed and that Monads can be Applicative. But Monads can also be composed using the so-called Kleisli composition. If we have:

  val f: B => M[C]
val g: A => M[B]

Then

  val h: A => M[C] = f ∎ g // is also a function from a value to a Monad

If we have 2 "monadic" functions f and g, we can then compose them, in the Kleisli sense, and use the composed version for a traversal. Indeed we can, but does this traversal have "nice properties"? Specifically, do we have:

  traverse(f ∎ g) == traverse(f) ∎ traverse(g)

The answer is... it depends.

Monad commutativity

EIP shows that, if the Monad is commutative, then this will always be true. What is a commutative Monad you ask?

A Monad is commutative if for all mx: M[X] and my: M[Y] we have:

    val xy = for {
x <- mx
y <- my
} yield (x, y)

val yx = for {
y <- my
x <- mx
} yield (x, y)

xy == yx

This is not the case with the State Monad for example:

   val mx = state((n: Int) => (n+1, n+1))
val my = state((n: Int) => (n+1, n+1))

xy.apply(0) must_== (2, (1, 2))
yx.apply(0) must_== (2, (2, 1))

Monadic functions commutativity

Another slightly different situation is when we have a non-commutative Monad but commutative functions:

  val plus1  = (a: A) => state((n: Int) => (n+1, a))
val plus2 = (a: A) => state((n: Int) => (n+2, a))
val times2 = (a: A) => state((n: Int) => (n*2, a))

Here plus1 and times2 are not commutative:

  (0 + 1) * 2 != (0 * 2) + 1

However it is obvious that plus1 and plus2 are commutative. What does that mean when we do a traversal?

If we traverse a simple List of elements using monadic composition we get:

  • List(1, 2, 3).traverse(times2 ∎ plus1) === 22
  • List(1, 2, 3).traverse(times2) ∎ List(1, 2, 3).traverse(plus1) === 32

We get different results. However, when f and g commute we get the same result:

  • List(1, 2, 3).traverse(plus2 ∎ plus1) === 10
  • List(1, 2, 3).traverse(plus2) ∎ List(1, 2, 3).traverse(plus1) === 10

Applicative composition vs Monadic composition

Another question we can ask ourselves is: if we consider the monadic functions as applicative functions (because each Monad is Applicative), do we get the nice "distribution" property we're after? The answer is yes, even when the functions are not commutative:

  • List(1, 2, 3).traverse(times2 ⊡ plus1) === 4
  • List(1, 2, 3).traverse(times2) ⊡ List(1, 2, 3).traverse(plus1) === 4

Well... more or less. The real situation is a bit more complex. List(1, 2, 3).traverse(times2 ⊡ plus1) returns a State[Int, State[Int, List[Int]]] while the second expression returns a State[Int, List[State[Int, Int]] so what I'm hiding here is some more manipulations to be able to query the final result with some kind of join.

Conclusion

You wouldn't believe it but I've only shown here half of the ideas presented in EIP!

To finish off this post here's 3 take-away points that I've learned while writing it:

  • functional programming is also about mastering some of these higher-level control structures like Applicative. Once you master them, your toolbox expands considerably in power (just consider the assemble example)

  • Scalaz is an incredible library but somewhat obscure to the beginner. For this post I've rewritten all the typeclasses I needed to have, and lots of examples (using specs2 of course). That gave me a much better understanding of the Scalaz functionality. You may consider doing the same to learn Scalaz (my code is available on github)

  • Scala is lacking behind Haskell in terms of type inference and it's a real pain for higher-order, generic programming. This can be sometimes encapsulated away by specializing generic functions to very common types (like traverseState instead of traverse). Again, please upvote SI-2712!

Finally, I want to mention that there are many other Haskell functional pearls waiting to be transliterated to Scala. I mean, it's a shame that we don't have yet any equivalent for "Learn you a Haskell" or "Typeclassopedia" in the Scala world. I hope that my post, like this other one by Debasish Ghosh, will also contribute to bridge the gap.

by Eric (noreply@blogger.com) at March 11, 2013 03:17 AM

March 07, 2013

scala-lang.org

Poll: Which version of Java should Scala 2.11 target?

Java 6
14% (433 votes)
Java 7
46% (1419 votes)
Java 8
40% (1242 votes)
Total votes: 3094

by cunei at March 07, 2013 02:06 AM

March 06, 2013

Gregg Carrier

Scala Either Example - Responding to Failures



A lot of Scala developers I know regularly use Option to write concise code in situations where something may or may not be returned by a given function. By getting familiar with the functions available on the Option class, you can chain many operations together without a lot of boilerplate or defensive protection against nonexistent values. Something like:

getAnOption
.flatMap(transformIntoAnotherOption)
.map(changeThing)
.filter(thingIsTheRightType)

Assuming in this contrived bit of pseudo-code that getAnOption and transformIntoAnotherOption are functions that return Option types, this chain of function calls will short-circuit if any call returns a None. For example, if transformIntoAnotherOption returns None, then the expression evaluation is complete without ever running changeThing.

But what if I want to know which part of a complex expression like the one above returned None? It's tricky and less elegant when using an Option expression which simply evaluates to Some or None.

Let's say I'm writing a database application that requires a two-phase lookup, either phase of which may fail to find the requested record. I want to be able to:
  • Look up an id based on a user-provided String
  • If we found an id, look up the Record
  • If we found a Record, validate it, process it, display it in an app, etc
The driver I am using returns Option[Record] from find calls. For this simple example, I mocked up a call with a frequent random failure that looks like this:

def databaseCall[A](value: A) = 
if (nextInt % 5 == 0) None else Some(value)

def nextInt = math.abs(scala.util.Random.nextInt)

And two wrapper calls to represent our two phases of lookup:

def lookupId(key: String): Option[Int] = 
databaseCall(nextInt)

def lookupRecord(id: Int): Option[Record] =
databaseCall(Record(id))

Chaining Option calls is a natural fit if we just need to find a record and return it (or not):

def findRecordByKey(key: String): Option[Record] =
databaseCall(key)
.flatMap(databaseCall)
.filter(validateIt)
.map(processIt)
.foreach(displayInApp)

The code above will sometimes call displayInApp with a value, and sometimes it will not. It is concise and avoids cluttering the logic with explicit conditionals and error handling. But it does not allow us to provide detailed feedback about what part of the process failed. Say we want to log whenever a databaseCall fails. We have to break up our nice single expression with calls to isDefined and error logging code. Not exactly elegant functional monadic wizardry that will get you invited to the cool kids' parties.

Either FTW


The scala.Either class can be used in ways very similar to Option. Either represents an instance of either Left or Right. By convention, Left is the "bad" or failure result and Right is the "good" one. In our example, that means Right is a value we found in the database and Left is a richer version of None where we can put a sensible message, an Exception or whatever we like. Profit!

First thing, let's change our database functions so they return Either instead of Option. Conveniently, Option has the functions toLeft and toRight for just this purpose. We'll use toRight in our example, which returns a Right containing the value of the Option it is called on if it is a Some. toRight() takes an argument that will be used to construct a Left if the Option is None. In our case, we'll provide a straightforward error message to use in case of failures:

def lookupId(key: String): Either[String, Int] = 
databaseCall(nextInt).toRight("failed to find id based on key")

def lookupRecord(id: Int): Either[String, Record] =
databaseCall(Record(id)).toRight("failed to find record based on id")

Easy enough. Here's how we can chain the calls to these functions:

lookupId("key").right
.flatMap(lookupRecord(_))
Pretty similar to how we use Options. Calling right() on the result of lookupId("key") projects the Either as a Right. The contained value is the passed to lookupRecord and so on. If the Either is a Left, lookupRecord is never called. So just as with Option, we can chain our calls, evaluating to an Either in the end.

So we end up with either a Right(someRecordInstance) or a Left(someErrorMessage). We can use pattern matching to process this result, or you can use the fold() function, which takes two function arguments. The first is run in the case of a Left, the second in the case of a Right. So that's just super for us. Here's the whole thing:

lookupId("key").right
.flatMap(lookupRecord(_)).fold(
s => println("ERROR - " + s),
s => println("looked up record " + s))

Running this code a thousand or so times prints a bunch of lines like the following:
looked up record Record(2073762079)
ERROR - failed to find id based on key
ERROR - failed to find record based on id
ERROR - failed to find record based on id
looked up record Record(1377010886)
looked up record Record(1665383097)
looked up record Record(646489647)

We now know when we failed to resolve an id to a key. We know if the record itself was not found. And assuming all went well, we get our Record result returned, validated, processed and displayed in our app in one neat expression. And we get invited to the cool kids' parties.

Download a buildable version of this code from github, and enjoy using Either in your projects.

by Gregg Carrier (noreply@blogger.com) at March 06, 2013 07:58 AM

scala-lang.org

Scala 2.10.1-RC3 now available!

We are pleased to announce the third release candidate of Scala 2.10.1!

The Scala team and contributors fixed 189 issues since 2.10.0! In total, 242 pull requests (+ 4 for RC3) were opened on GitHub, of which 225 were merged (+ 4 for RC3) after having been tested and reviewed.

Please give 2.10.1-RC3 a spin! It's designed to be a drop-in replacement for 2.10.0, except in (few!) cases where we decided not fixing the bug would be worse than the breaking change (for now, there's only one known instance of this.) We'd love to hear about any serious regressions since 2.10.0 and will try to fix them before releasing the final version.

This RC will become the final unless new blocker issues are discovered within a week after its release.

by moors at March 06, 2013 12:15 AM

March 04, 2013

Ruminations of a Programmer

An exercise in Refactoring - Playing around with Monoids and Endomorphisms

A language is powerful when it offers sufficient building blocks for library design and adequate syntactic sugar that helps build expressive syntax on top of the lower level APIs that the library publishes. In this post I will discuss an exercise in refactoring while trying to raise the level of abstraction of a modeling problem.

Consider the following modeling problem that I recently discussed in one of the Scala training sessions. It's simple but offers ample opportunities to explore how we can raise the level of abstraction in designing the solution model. We will start with an imperative solution and then incrementally work on raising the level of abstraction to make the final code functional and more composable.

A Problem of Transformation ..

The problem is to compute the salary of a person through composition of multiple salary components calculated based on some percentage of other components. It's a problem of applying repeated transformations to a pipeline of successive computations - hence it can be generalized as a case study in function composition. But with some constraints as we will see shortly.

Let's say that the salary of a person is computed as per the following algorithm :

  1. basic = the basic component of his salary
  2. allowances = 20% of basic
  3. bonus = 10% of (basic + allowances)
  4. tax = 30% of (basic + allowances + bonus)
  5. surcharge = 10% of (basic + allowances + bonus - tax)
Note that the computation process starts with a basic salary, computes successive components taking the input from the previous computation of the pipeline. But there's a catch though, which makes the problem a bit more interesting from the modleing perspective. Not all components of the salary are mandatory - of course the basic is mandatory. Hence the final components of the salary will be determined by a configuration object which can be like the following ..

// an item = true means the component should be activated in the computation
case class SalaryConfig(
surcharge: Boolean = true,
tax: Boolean = true,
bonus: Boolean = true,
allowance: Boolean = true
)

So when we compute the salary we need to take care of this configuration object and activate the relevant components for calculation.

A Function defines a Transformation ..

Let's first translate the above components into separate Scala functions ..

// B = basic + 20%
val plusAllowance = (b: Double) => b * 1.2

// C = B + 10%
val plusBonus = (b: Double) => b * 1.1

// D = C - 30%
val plusTax = (b: Double) => 0.7 * b

// E = D - 10%
val plusSurcharge = (b: Double) => 0.9 * b

Note that every function computes the salary up to the stage which will be fed to the next component computation. So the final salary is really the chained composition of all of these functions in a specific order as determined by the above stated algorithm.

But we need to selectively activate and deactivate the components depending on the SalaryConfig passed. Here's the version that comes straight from the imperative mindset ..

The Imperative Solution ..

// no abstraction, imperative, using var
def computeSalary(sc: SalaryConfig, basic: Double) = {
var salary = basic
if (sc.allowance) salary = plusAllowance(salary)
if (sc.bonus) salary = plusBonus(salary)
if (sc.tax) salary = plusTax(salary)
if (sc.surcharge) salary = plusSurcharge(salary)
salary
}

Straight, imperative, mutating (using var) and finally rejected by our functional mindset.

Thinking in terms of Expressions and Composition ..

Think in terms of expressions (not statements) that compose. We have functions defined above that we could compose together and get the result. But, but .. the config, which we somehow need to incorporate as part of our composable expressions.

So direct composition of functions won't work because we need some conditional support to take care of the config. How else can we have a chain of functions to compose ?

Note that all of the above functions for computing the components are of type (Double => Double). Hmm .. this means they are endomorphisms, which are functions that have the same argument and return type - "endo" means "inside" and "morphism" means "transformation". So an endomorphism maps a type on to itself. Scalaz defines it as ..

sealed trait Endo[A] {
/** The captured function. */
def run: A => A
//..
}

But the interesting part is that there's a monoid instance for Endo and the associative append operation of the monoid for Endo is function composition. That seems mouthful .. so let's dissect what we just said ..

As you all know, a monoid is defined as "a semigroup with an identity", i.e.

trait Monoid[A] {
def append(m1: A, m2: A): A
def zero: A
}

and append has to be associative.

Endo forms a monoid where zero is the identity endomorphism and append composes the underlying functions. Isn't that what we need ? Of course we need to figure out how to sneak in those conditionals ..

implicit def endoInstance[A]: Monoid[Endo[A]] = new Monoid[Endo[A]] {
def append(f1: Endo[A], f2: => Endo[A]) = f1 compose f2
def zero = Endo.idEndo
}

But we need to append the Endo only if the corresponding bit in SalaryConfig is true. Scala allows extending a class with custom methods and scalaz gives us the following as an extension method on Boolean ..

/**
* Returns the given argument if this is `true`, otherwise, the zero element
* for the type of the given argument.
*/
final def ??[A](a: => A)(implicit z: Monoid[A]): A = b.valueOrZero(self)(a)

That's exactly what we need to have the following implementation of a functional computeSalary that uses monoids on Endomorphisms to compose our functions of computing the salary components ..

// compose using mappend of endomorphism
def computeSalary(sc: SalaryConfig, basic: Double) = {
val e =
sc.surcharge ?? plusSurcharge.endo |+|
sc.tax ?? plusTax.endo |+|
sc.bonus ?? plusBonus.endo |+|
sc.allowance ?? plusAllowance.endo
e run basic
}

More Generalization - Abstracting over Types ..

We can generalize the solution further and abstract upon the type that represents the collection of component functions. In the above implementation we are picking each function individually and doing an append on the monoid. Instead we can abstract over a type constructor that allows us to fold the append operation over a collection of elements.

Foldable[] is an abstraction which allows its elements to be folded over. Scalaz defines instances of Foldable[] typeclass for List, Vector etc. so we don't care about the underlying type as long as it has an instance of Foldable[]. And Foldable[] has a method foldMap that makes a Monoid out of every element of the Foldable[] using a supplied function and then folds over the structure using the append function of the Monoid.

trait Foldable[F[_]]  { self =>
def foldMap[A,B](fa: F[A])(f: A => B)(implicit F: Monoid[B]): B
//..
}

In our example, f: A => B is the endo function and the append is the append of Endo which composes all the functions that form the Foldable[] structure. Here's the version using foldMap ..

def computeSalary(sc: SalaryConfig, basic: Double) = {
val components =
List((sc.surcharge, plusSurcharge),
(sc.tax, plusTax),
(sc.bonus, plusBonus),
(sc.allowance, plusAllowance)
)
val e = components.foldMap(e => e._1 ?? e._2.endo)
e run basic
}

This is an exercise which discusses how to apply transformations on values when you need to model endomorphisms. Instead of thinking in terms of generic composition of functions, we exploited the types more, discovered that our tranformations are actually endomorphisms. And then applied the properties of endomorphism to model function composition as monoidal appends. The moment we modeled at a higher level of abstraction (endomorphism rather than native functions), we could use the zero element of the monoid as the composable null object in the sequence of function transformations.

In case you are interested I have the whole working example in my github repo.

by Debasish Ghosh (noreply@blogger.com) at March 04, 2013 05:30 AM

March 03, 2013

Dave Ray

Clawk = Awk cut with Clojure

I know Clojure pretty well. I don’t know Awk quite as well and don’t need it quite enough to ever learn it deeply. I don’t want Perl either. I’d rather just write Clojure code for simple text processing. So, lately I’ve been using Clawk. It works for me. Bad idea, or worst idea? A tiny example:

$ echo -e "1\n2\n3\n" | clawk -r '(* $ $)'
1
4
9

by dave at March 03, 2013 05:07 PM

February 28, 2013

scala-lang.org

Scala 2.9.3 is now available!

We are happy to announce the final release of 2.9.3 in the Scala 2.9.x maintenance series!

This release includes the following improvements:

  • a backport of the implementation of SIP-14 to Scala 2.9,
  • numerous fixes that are leveraged by the Scala IDE to improve stability and responsiveness,
  • compiler fixes to allow faster incremental compilation.

Scala IDE for Eclipse

You may install the Scala IDE 3.0-RC1 for Scala 2.9.3 through one of the following update-sites:

by moors at February 28, 2013 01:07 AM

Scala 2.10.1-RC2 now available!

We are pleased to announce the second release candidate of Scala 2.10.1!

The Scala team and contributors fixed 184 issues since 2.10.0! In total, 242 pull requests (+ 7 for RC2) were opened on GitHub, of which 225 were merged (+ 6 for RC2) after having been tested and reviewed.

Please give 2.10.1-RC2 a spin! It's designed to be a drop-in replacement for 2.10.0. We'd love to hear about any regressions since 2.10.0 and will try to fix them before releasing the final version.

There will be an RC3 one week after this release, which will become the final unless new blocker issues are discovered within a week after its release.

by moors at February 28, 2013 12:52 AM

February 27, 2013

scala-lang.org

Scala Workshop 2013 (Scala 2013) Announced! Call for papers open!

July 2nd, Montpellier, France
Now co-located with the European Conference on Object-Oriented Programming (ECOOP).

Call for papers, demos, and talks open!

The Scala Workshop is the leading forum for research and development on or related to the Scala programming language. 

Join us for a full day of research talks, tool demos, and opportunities to engage with researchers, practitioners, and students working with or interested in the Scala programming language. The Scala workshop is a venue for exploring ongoing work on programming languages and software engineering, intent on bridging the gap between industry and academia.

by heathermiller at February 27, 2013 01:56 AM

Tony Sloane

Pattern Matching with String Interpolation

So far in this series we’ve seen how to use string interpolation in Scala 2.10 to construct values of various types. Optionally, we can perform run-time or compile-time checking to make sure that the processed strings are sensible.

In this post we consider how to use string interpolation with pattern matching to deconstruct values. Instead of interpolating expression values, we will interpolate arbitrary patterns and thereby be able to extend pattern matching in nice ways.

The complete code for all examples in this series will be made available in the accompanying BitBucket project.

A more general version of s

As it stands, the library interpolator s can only be used to construct string values.

println (s"One plus one is ${1 + 1}")
One plus one is 2

Suppose that we want to use s to pattern match as well. In other words, we want to get exact matches for the constant strings and to recursively match interpolated sub-patterns.

Here’s an example of what we want to achieve. Our new interpolator is called mys. Construction works just as for s.

println (mys"One plus one is ${1 + 1}")

We also want to use mys in a pattern-matching context; e.g., in a case of a match expression. In this example, we match the string "The sky is blue" against the pattern "The $thing is $colour".

val msg = "The sky is blue" match {
case mys"The $thing is $colour" =>
mys"A $colour thing is $thing"
case _ =>
"no match"
}
println (msg)

The constant strings "The ", " is " and an empty string at the end match exactly. The interpolated patterns thing and colour match the characters between the constant strings. In this case the interpolated patterns are variable patterns so the effect is to bind those variables to the strings that are matched in those positions. The bound variables are used in the body of the case to construct the result.

One plus one is 2
A blue thing is sky

Construction and deconstruction together

Our first step toward building mys is to consider how we can make it both construct and deconstruct. In our earlier examples, the function that performed the interpolation was a method of the context implicit class. Clearly we can’t make a single method both construct and deconstruct and keep the same interface. But we can use an object to provide both directions. We rely on the fact that an object with an apply method can be used as a method.

The overall structure of MySContext is as follows.

implicit class MySContext (val sc : StringContext) {

object mys {

def apply (args : Any*) : String =
sc.s (args : _*)

...

}

}

Instead of a mys method, we now have a mys object. The mys.apply method implements the construction direction by calling s. We can add other methods to mys as necessary. In particular, we will add an unapplySeq method to make mys into an extractor object that can be called in a pattern matching situation.

Desugaring pattern matching

Before we finish writing MySContext, we need to understand how a processed string desugars when it is used in a pattern context. The general form is the same as before, except that the interpolated pieces marked with dollars are patterns now, not expressions.

id"text0${pat1}text1 ... ${patn}textn"

This general form is translated by the compiler into

StringContext ("text0", "text1", ... , "textn").id (pat1, ... , patn)

The assumption is that id is an extractor object. If you are hazy on how extractor objects work you can find a quick overview in the Tour of Scala.

The unappply (or more usually, unapplySeq) method of id is responsible for doing the matching and returning values that are to be recursively matched against pat1 to patn.

Making mys into an extractor object

Now that we know how an interpolated pattern desugars, we need to add an unapplySeq method to the mys object. We will use unapplySeq instead of unapply because we want to be able to return an arbitrary number of results when a match is found. We can’t predict how many nested patterns there will be.

Our method for implementing the matching process is to construct a regular expression and to rely on regular expression matching to do the hard work. The constant strings are left unchanged, while each interpolated pattern is represented by (.+). The parentheses define a group so when we run the match we will be given the value that matched this sub-regexp.

Here is the full version of mys with unapplySeq.

implicit class MySContext (val sc : StringContext) {

object mys {

def apply (args : Any*) : String =
sc.s (args : _*)

def unapplySeq (s : String) : Option[Seq[String]] = {
val regexp = sc.parts.mkString ("(.+)").r
regexp.unapplySeq (s)
}

}

}

Many readers will observe that this implementation is ripe for an injection attack. unapplySeq does not deal with a situation where the constant strings contain regular expression notation. You might like to extend the code to escape that notation before trying the match.

Using more complex sub-patterns

Let’s look at a more complex example of using this interpolator where sub-patterns are used to constrain what can match.

Suppose that we want to match strings like "val age = 34". Furthermore, we want to make sure that the value name is a valid identifier and that the assigned value is a number. We assume that identifiers are any string of letters and digits that begins with a letter, and that numbers are a non-empty sequence of digits.

The method matchValDef uses an interpolation to match the overall structure of the strings we want to accept. Sub-patterns defined using regular expressions further constrain the non-constant parts. Since the interpolated parts are just patterns, we can use any valid Scala pattern matching syntax. matchValDef returns a pair of the matched non-constant parts if the match succeeds, or None if it doesn’t.

def matchValDef (s : String) : Option[(String,String)] = {

val Ident = """(\p{Alpha}\p{Alnum}*)""".r
val Value = """(\d+)""".r

s match {
case mys"val ${Ident (ident)} = ${Value (value)}" =>
Some ((ident, value))
case _ =>
None
}

}

Now we can use matchValDef to pull different candidate strings apart.

println (matchValDef ("val age = 34"))
println (matchValDef ("val sum88 = 12345"))
println (matchValDef ("val age = bob"))
println (matchValDef ("val 99 = 34"))
println (matchValDef ("age = 34"))
println (matchValDef ("val age 34"))
Some((age,34))
Some((sum88,12345))
None
None
None
None

We could easily convert matchValDef into an extractor that could be used directly in a case.

What’s next?

The ability to extend pattern matching using extractors is very powerful. Connecting extractors to string interpolation as we’ve seen in this post makes that power more natural to use since the fixed parts are written explicitly.

Our examples have been confined to deconstructing strings. We can also build deconstructing interpolators that take non-strings as input and return non-string values. In the following posts in this series, we will show examples where the values being matched and returned are abstract syntax trees that represent more complex structures. Construction and deconstruction will use a formal language syntax but the values being manipulated will be trees. This capability is extremely useful if we want to write programs that manipulate structured data.

by inkytonik (noreply@blogger.com) at February 27, 2013 12:28 AM

Syntax checking in Scala String Interpolators

In the second post of this seriesI showed how you can write your own string interpolators for Scala 2.10. The examples were designed to work regardless of the content of the processed string. In many other cases, we care what the string looks like and its form affects what we want to do. Often we want to complain if the content of the string is not acceptable. Options are a run-time error or a compile-time one. The latter case requires us to extend the compiler, which I will do using 2.10’s experimental macro features.

The complete code for all examples in this series will be made available in the accompanying BitBucket project.

Octal number literals

Suppose that we want to use the notation o"177" to stand for an integer literal whose value is specified in octal (base 8). Our interpolator will have to perform a numeric conversion or complain if the string literal is not a legal octal number. (For simplicity, we ignore interpolated expressions in this example.)

We can define a simple octal number interpolator as follows.

implicit class OctalContext (val sc : StringContext) {

def o () : Int = {
val orig = sc.parts.mkString
val OctalNum = "[0-7]+".r
orig match {
case OctalNum () =>
Integer.parseInt (orig, 8)
case _ =>
sys.error ("Can only contain 0-7 characters")
}
}

}

We access the string literal using the parts method of the StringContext which returns a list of all of the context string parts from the literal. In this case there will only be one since we are not supporting interpolated expressions.

Once we have the string, a regular expression pattern checks whether the literal is in the correct form or not. If the form is ok, we convert the string and return its integer value. Otherwise, we throw an error to complain about the format violation.

println (o"177")
println (o"49")
127
java.lang.RuntimeException: Can only contain 0-7 characters
at scala.sys.package$.error(package.scala:27)
at Octal$OctalContext.o(Octal.scala:12)
at Octal$.main(Octal.scala:20)

Compile-time checking

Run-time errors are fine in some situations, but many of us would like to get stronger guarantees about our code. In particular, we’d like the compiler to complain if we try to write an octal number literal but get the format wrong.

One way to achieve this kind of checking is to extend the compiler with a macro. Macros are a new experimental feature in Scala 2.10 and are planned to be fully-supported in 2.11. In a nutshell, a macro is given access to the compiler’s abstract syntax tree (AST) for a method call. It can return a new AST that the compiler will use instead of the original call. Thus, the macro can replace the call that a user writes with any legal expression.

(Strictly speaking, the macros we use here are def macros because they implement the bodies of def constructs. Other macro styles in development will be able to replace types and other Scala constructs.)

Another alternative to get this kind of checking is a full-blown compiler plugin. A plugin can be more powerful than a macro, because it has full access to the whole compilation unit. However, writing a plugin is harder since much of the plumbing and book-keeping must be implemented. In contrast, the macro system takes care of many of the details of hooking into the compilation process, accessing the abstract syntax tree, and so on. We can focus on the actual replacement that we want to construct.

In our experience Scala 2.10 macros are pretty reliable, but you should be aware that they are experimental so it might be dangerous to rely on them. It’s also easy to get yourself or the compiler in a tangle when writing a macro since you are essentially working with the compiler’s internal representations.

An octal number literal macro

Our plan is to replace the octal number interpolator we wrote above with one that is implemented by a macro. The advantage is that the macro will execute at compile time so it will be able to issue compile-time errors if we get the string literal wrong. In the non-error case the macro will be able to perform the number conversion, thereby saving the program from having to perform it at run-time.

First, we write the o method, but instead of giving the full implementation, we use the macro keyword to indicate that this method is a macro that is implemented by the OctalImpl.oImpl method. This simple notation suffices to get the compiler to call the macro at compile-time and use its result.

implicit class OctalContext (val sc : StringContext) {

def o () : Int =
macro OctalImpl.oImpl

}

An import of scala.language.experimental.macros or the corresponding command-line option will be necessary to enable the macros feature. It is also necessary to ensure that the macro and its uses are not in the same compilation unit, since the compiler needs to have access to the compiled macro implementation when it compiles the uses.

The macro signature

The signature of a macro is closely related to that of the method that it implements. The signature of the oImpl method that implements o is as follows.

def oImpl (c : Context) () : c.Expr[Int]

The first parameter list contains a Context argument that can be used by the macro to find out about the context in which it has been called. The second parameter list contains one argument for each argument of the method. The arguments passed here are the Scala AST representations of the argument expressions used in the original method call. In our case, the o method has no parameters so the second parameter list of the macro is empty. Finally, the return value of the macro is a Scala AST that represents the expression which we want to use to replace the macro call. The return type is path-dependent on the context and says that the value must be an expression whose type is the type of the original method. Thus, we have a return type of c.Expr[Int] here because o returns an Int.

The context and the trees

The context gives us access to many wonderful things. For example, the context provides position information for the macro call and a way to issue error messages.

The context’s universe gives us the Scala AST node definitions which we will need to query and construct trees. To save space in the code, we introduce a local alias u for the universe.

import c.{universe => u}

and import everything

import u._

Importantly for the octal number literal macro, the context gives us access to the AST on which the method call in question has been made. This tree is returned by c.prefix.tree. The call o"177" desugars to

OctalContext (StringContext ("177")).o ()

after the implicit conversion has been applied. The prefix tree represents the bit without the method call.

OctalContext (StringContext ("177"))

A common development approach is to print out the trees to see what the compiler is giving you. The u.show and u.showRaw methods are particularly useful for this purpose. For example, u.show (c.prefix.tree) returns the following in the macro we are defining for the call o"177" (modulo some formatting). This tree has fully-qualified names and explicitly calls the applymethod to construct the string context, but otherwise is the same as above.

OctalMacros.OctalContext (scala.StringContext.apply ("177"))

We can see the actual tree nodes used in the representation of this expression in the compiler by printing u.showRaw (c.prefix.tree)(reformatted to make the structure clear).

Apply (
Select (Ident (OctalMacros), newTermName ("OctalC")),
List (
Apply (
Select (Select (Ident (scala), scala.StringContext), newTermName ("apply")),
List (
Literal (Constant ("177"))))))

Thus, we can see that inside the compiler the prefix will be represented by an AST containing Apply nodes where methods are applied and a literal constant node containing the string "177".

Getting at the literal string

Armed with our knowledge about the structure of the prefix tree, we can easily pattern match the literal string out of the tree and call it orig.

val Apply (
_,
List (
Apply (
_,
List (
Literal (Constant (orig : String)))))) =
c.prefix.tree

The layout of this pattern parallels the layout of the output of showRaw above.

Check, convert or complain

Now that we have the string literal in our grasp, the macro can proceed to check its format. The code is similar to the code we use earlier in our run-time version.

val OctalNum = "[0-7]+".r

orig match {

case OctalNum () =>
c.Expr[Int] (Literal (Constant (Integer.parseInt (orig, 8))))

case _ =>
c.error (c.enclosingPosition, "Must only contain 0-7 characters")
c.Expr[Int] (Literal (Constant (0)))

}

The differences are in what is returned. If the format is ok, we perform the conversion to get the integer value. However, instead of returning that number, we return an AST that represents an expression that evaluates to that number. That expression is

Literal (Constant (Integer.parseInt (orig, 8))))

In other words, a literal integer constant containing the value we want. The c.Expr[Int] constructor combines the expression tree with its type representation.

In the error case, we call the c.error method to report a compile-time error. c.enclosingPosition gives us the source code position of the macro call. c.error is a Unit method, so we need a dummy return value to satisfy the return type.

Our macro implementation is complete. In summary, the macro is given the AST that represents the call. We delve into the AST to access the string literal from the call. We process that literal to check its format. If the format is ok, we convert it to a decimal value and return an AST that represents that value. If the format is not ok, we trigger a compile-time error that says so.

Using the macro-based interpolator

The macro can be used (in a different compilation unit) in the same way as our previous non-macro implementation. This abstraction is nice since you can switch the implementation to a macro without requiring users to rewrite their code. Of course, they must recompile it.

def main (args : Array[String]) {
println (o"177")
}
127

If we try a literal that is not a valid octal number, we get the expected compile-time error.

OctalWithMacro.scala:8: error: Must only contain 0-7 characters
println (o"49")
^

Why use a string interpolator?

Some readers may be wondering why we bother with a string interpolation for this example. All of the work is being done by the macro and the interpolation is not really contributing very much. In this case that is true, although I quite like the o"177" syntax, compared to something like o ("177") which we would have to use with a pure macro implementation.

String interpolations come much more into their own as an interface to macros when the string format is more complex and the processed strings can contain interpolated expressions. Having a standard and concise syntax to indicate where the expressions are to be placed is a big advantage. Otherwise, every macro writer needs to invent their own convention to pass the pieces to the macro.

What’s next?

The octal number example is a simple case of a very general problem. The format of a processed string can be quite tightly defined, perhaps by a formal syntax. We would like to be informed by the compiler if we err in the syntax of a string. Later posts will revisit this issue.

First though, in the next post we flip the problem around. Instead of using processed strings to construct values, we will use them to deconstruct values via pattern matching. We will see that the processed string syntax leads to quite concise pattern matching of complex structures.

by inkytonik (noreply@blogger.com) at February 27, 2013 12:27 AM

Writing your own Scala 2.10 String Interpolators

In the first post of this seriesI showed how Scala 2.10’s new processed string syntax allow us to interpolate expression values into literal strings. We saw the s, raw and f interpolators that are provided by the Scala library. Now we will see how to write your own interpolators.

The complete code for all examples in this series will be made available in the accompanying BitBucket project.

Syntactic sugar for processed strings

The first step to writing your own interpolators is to understand how the Scala compiler interprets the new processed string syntax. A processed string has this general form:

id"text0${expr1}text1 ... ${exprn}textn"

where id is an identifier, the text pieces are constant string fragments, and the expr pieces are arbitrary expressions. The general form of processed string is translated into an expression of the following form.

StringContext ("text0", "text1", ... , "textn").id (expr1, ... , exprn)

The constant parts of the string literal are extracted and passed to the constructor of the Scala library’s StringContext class. The id method of the StringContext object is called and the interpolated expressions are passed as arguments.

What does s do?

More concretely, the expression

s"You are ${age / 10} decades old, $name!"

is really

StringContext ("You are ", " decades old, ", "!").s (age / 10, name)

The StringContext.s method takes the constant parts, interprets any escape sequences they contain, and interleaves them with the values of the expression arguments. The value returned is equivalent to

"You are " + (age / 10) + " decades old, " + (name) + "!"

which is probably what we would have written prior to Scala 2.10.

Adding methods to classes using an implicit conversion

It should be clear by now that to write your own interpolator, all you need is to add a method to the StringContext class. Of course, you can’t actually modify StringContext, but you can use an implicit conversion to achieve a similar effect.

Prior to 2.10 we would implement an implicit conversion as follows. Suppose we want to add a method sayhello to values of type Int. We can declare a new class MyInt that has the sayhello method and an implicit conversion that wraps Int values in an instance of the MyInt class.

class MyInt (val i : Int) {
def sayhello = s"Hello there: $i"
}

implicit def IntToMyInt (i : Int) : MyInt =
new MyInt (i)

Now we can use the sayhello method on an Int

println (42.sayhello)

which is treated by the compiler as

println (new MyInt (42).sayhello)

so we get the expected output:

Hello there: 42

Implicit classes

In 2.10 we can write the same conversion in a shorter way using another new feature: implicit classes.

implicit class MyInt (i : Int) {
def sayhello = s"Hello there: $i"
}
println (42.sayhello)
Hello there: 42

The implicit modifier on the class MyInt causes the compiler to synthesise the IntToMyInt conversion that we wrote earlier.

In practice, you would probably also want to make MyInt a value class(another new feature in 2.10) to avoid the overhead of the object wrapping. We will avoid value classes here to keep the examples simple.

Writing our own interpolator

Armed with implicit conversions, it is easy to write a new interpolator. As our first example, let’s write one that performs exactly like sbut returns the reverse of the result string.

implicit class ReverseContext (val sc : StringContext) {
def rev (args : Any*) : String = {
val orig = sc.s (args : _*)
orig.reverse
}
}

The ReverseContext class wraps a StringContext and adds the revmethod. rev passes all of its expression arguments to the s method of the StringContext and then returns the reverse of the result.

val msg = "Hello world!"
println (rev"Backwards version of $msg")
!dlrow olleH fo noisrev sdrawkcaB

Constructing values that are not strings

Even though processed strings start out as a form of string literal, the value that they stand for does not have to be a string. This observation leads to much of the power of processed strings.

For example, the following code is a simple interpolator that first applies s and then counts the number of space characters in the result. The count is returned in a Count object.

case class Count (num : Int)

implicit class SpaceCountC (val sc : StringContext) {
def nspaces (args : Any*) : Count = {
val orig = sc.s (args : _*)
Count (orig.count (_.isSpaceChar))
}
}
val msg1 = "Hello world!"
val msg2 = s"a b $msg1 c d"
println (nspaces"$msg1")
println (nspaces"$msg2")
Count(1)
Count(5)

What’s next?

These examples have been chosen to be simple to make the mechanism clear. In the next post, we consider a more realistic example: octal number literals. As well as requiring a (slightly) more complex interpolator, octal numbers prompt us to think about the form of the literal that we can accept and what to do if the literal’s form is not legal.

by inkytonik (noreply@blogger.com) at February 27, 2013 12:27 AM

February 26, 2013

Paul Chiusano

Why functional code is shorter

Was rereading John Carmack's Functional Programming in C++ post. For the most part, it's an excellent discussion of FP and its benefits, but there are a few points I wanted to dissect. Here, Carmack suggests the conciseness of functional programs has more to do with features like pattern matching and list comprehensions:

The process of refactoring towards purity generally involves disentangling computation from the environment it operates in, which almost invariably means more parameter passing. This seems a bit curious – greater verbosity in programming languages is broadly reviled, and functional programming is often associated with code size reduction. The factors that allow programs in functional languages to sometimes be more concise than imperative implementations are pretty much orthogonal to the use of pure functions — garbage collection, powerful built in types, pattern matching, list comprehensions, function composition, various bits of syntactic sugar, etc. For the most part, these size reducers don’t have much to do with being functional, and can also be found in some imperative languages.

Features like pattern matching and list comprehensions can indeed make code shorter, and they are convenient, but they're just constant factors. Like Carmack says, imperative programs could have access to these features too. The real decrease in code size when using FP comes from there being exponentially more code reuse possible due to the compositionality of pure code. I don't mean compositionality in the narrow sense of function composition, I mean it in the sense of being able to assemble complex functionality by sticking together simpler components in various ways. And compositionality dwarfs all other factors affecting our ability to write complex software.

The 'convenience' features are important more because they reduce the friction of composition (for instance, if your syntax for function literals are too heavyweight you are discouraged from writing your list-processing code by composing use of higher-order functions like map and filter, and you are discouraged from ever writing higher-order functions yourself).

We have only just begun to appreciate how compositionality can change the nature of software. I keep getting surprised by it. The progression of functional programming shows the rabbit hole goes very deep--yes, we can write loops with less boilerplate using map and filter, but we can also write combinator libraries, which let us assemble programs compositionally within a domain. Going further, we can abstract over commonalities across libraries, letting us assemble libraries for a domain from a preexisting suite of compositional library construction components (typeclasses like Monad, Applicative, and so on). And further patterns emerge from there--FP has now discovered patterns of architecture, like stream processing libraries, in which the typeclasses themselves are simply building blocks.

Moving on, another point Carmack mentioned was that we can't maintain purity throughout our programs since we eventually have to interact with the outside world:

Not everything can be pure; unless the program is only operating on its own source code, at some point you need to interact with the outside world. It can be fun in a puzzly sort of way to try to push purity to great lengths, but the pragmatic break point acknowledges that side effects are necessary at some point, and manages them effectively.

One of the big discoveries of FP is that while effects need to occur, observable side effects are not necessary. This is more than an accounting trick; we can indeed write programs that have effects on the outside world and which mutate data, etc, all while preserving referential transparency of the overall system and letting us program in a nice compositional style. FP is not in any way a restriction on what can be expressed, only a restriction on how it is expressed. It only requires us to be 'honest' about what is occurring, not limiting what functionality is expressible.

by Paul Chiusano (noreply@blogger.com) at February 26, 2013 10:49 PM

February 25, 2013

The Careful Programmer

Redline Smalltalk: so long and thanks for all the fish

This is the last push for the Redline Smalltalk indiegogo campaign.

I give my heartfelt thanks to everybody who lent a hand, be it a donation or getting the word out.

We got an amazing spectrum of persons helping, from friends who donated so I would stop pestering them about Redline to pillars of our field like Grady Booch.

From the Redliners emerge these qualities: kindness, excellent programming chops, and a non-negligible amount of geekness 8)

To those who already donated: Please push out our campaign far and wide with your communication canals. Challenge your friends and coworkers to match or exceed your donation. Make a friendly competition out of it. Get your company to make a corporate donation.

To those who have yet to donate:  be kind, be excellent, be a really hoopy frood who knows where his or her towel is. Be a Redliner!

by The Careful Programmer (noreply@blogger.com) at February 25, 2013 10:05 PM

February 24, 2013

Coderspiel

Making Meetup: Measuring Scala 2.10

Making Meetup: Measuring Scala 2.10:

makingmeetup:

We upgraded from Scala 2.9.1-1 to 2.10.0 and it made some stuff faster.

Upper bound of response times

@softprops did the 2.10 upgrade in a branch a few weeks back with the minimal code changes it required. We were surprised at the time to see some improvements in our benchmarks. I was eager to see if those translated…

February 24, 2013 12:07 AM

February 22, 2013

Yuvi Masory

February 15, 2013

James Iry

The Best-Laid Schemes O' Mice An' Constructors Gang Aft Agley

Last time I talked about how the Scala compiler currently translates try/catch expressions used as arguments to method or constructor call into separate method definitions which in turn may require boxing of mutable variables. That's because an exception in the try would blow away the local stack of stuff needed to make the original method/constructor call. So I proposed a plan for the Scala

by James Iry (noreply@blogger.com) at February 15, 2013 06:41 PM

Ruminations of a Programmer

A DSL with an Endo - monoids for free

When we design a domain model, one of the issues that we care about is abstraction of implementation from the user level API. Besides making the published contract simple, this also decouples the implementation and allows post facto optimization to be done without any impact on the user level API.

Consider a class like the following ..

// a sample task in a project
case class Task(name: String)

// a project with a list of tasks & dependencies amongst the
// various tasks
case class Project(name: String,
startDate: java.util.Date,
endDate: Option[java.util.Date] = None,
tasks: List[Task] = List(),
deps: List[(Task, Task)] = List())

We can always use the algebraic data type definition above to add tasks and dependencies to a project. Besides being cumbersome as a user level API, it also is a way to program too close to the implementation. The user is coupled to the fact that we use a List to store tasks, making it difficult to use any alternate implementation in the future. We can offer a Builder like OO interface with fluent APIs, but that also adds to the verbosity of implementation, makes builders mutable and is generally more difficult to compose with other generic functional abstractions.

Ideally we should be having a DSL that lets users create projects and add tasks and dependencies to them.

In this post I will discuss a few functional abstractions that will stay behind from the user APIs, and yet provide the compositional power to wire up the DSL. This is a post inspired by this post which discusses a similar DSL design using Endo and Writers in Haskell.

Let's address the issues one by one. We need to accumulate tasks that belong to the project. So we need an abstraction that helps in this accumulation e.g. concatenation in a list, or in a set or in a Map .. etc. One abstraction that comes to mind is a Monoid that gives us an associative binary operation between two objects of a type that form a monoid.

trait Monoid[T] {
def append(m1: T, m2: T): T
def zero: T
}

A List is a monoid with concatenation as the append. But since we don't want to expose the concrete data structure to the client API, we can talk in terms of monoids.

The other data structure that we need is some form of an abstraction that will offer us the writing operation into the monoid. A Writer monad is an example of this. In fact the combination of a Writer and a Monoid is potent enough to have such a DSL in the making. Tony Morris used this combo to implement a logging functionality ..

for {
a <- k withvaluelog ("starting with " + _)
b <- (a + 7) withlog "adding 7"
c <- (b * 3).nolog
d <- c.toString.reverse.toInt withvaluelog ("switcheroo with " + _)
e <- (d % 2 == 0) withlog "is even?"
} yield e
We could use this same technique here. But we have a problem - Project is not a monoid and we don't have a definition of zero for a Project that we can use to make it a Monoid. Is there something that would help us get a monoid from Project i.e. allow us to use Project in a monoid ?

Enter Endo .. an endomorphism which is simply a function that takes an argument of type T and returns the same type. In Scala, we can state this as ..
sealed trait Endo[A] {
// The captured function
def run: A => A
//..
}
Scalaz defines Endo[A] and provides a lot of helper functions and syntactic sugars to use endomorphisms. Among its other properties, Endo[A] provides a natural monoid and allows us to use A in a Monoid. In other words, endomorphisms of A form a monoid under composition. In our case we can define an Endo[Project] as a function that takes a Project and returns a Project. We can then use it with a Writer (as above) and implement the accumulation of tasks within a Project.

Exercise: Implement Tony Morris' logger without side-effects using an Endo.

Here's how we would like to accumulate tasks in our DSL ..
for {
_ <- task("Study Customer Requirements")
_ <- task("Analyze Use Cases")
a <- task("Develop code")
} yield a


Let's define a function that adds a Task to a Project ..
// add task to a project
val withTask = (t: Task, p: Project) => p.copy(tasks = t :: p.tasks)


and use this function to define the DSL API task which makes an Endo[Project] and passes it as a Monoid to the Writer monad. In the following snippet, (p: Project) => withTask(t, p) is a mapping from Project => Project, which gets converted to an Endo and then passed to the Writer monad for adding to the task list of the Project.
def task(n: String): Writer[Endo[Project], Task] = {
val t = Task(n)
for {
_ <- tell(((p: Project) => withTask(t, p)).endo)
} yield t
}

The DSL snippet above is a monad comprehension. Let's add some more syntax to the DSL by defining dependencies of a Project. That's also a mapping from one Project state to another and can be realized using a similar function like withTask ..
// add project dependency
val withDependency = (t: Task, on: Task, p: Project) =>
p.copy(deps = (t, on) :: p.deps)

.. and define a function dependsOn to our DSL that allows the user to add the explicit dependencies amongst tasks. But this time instead of making it a standalone function we will make it a method of the class Task. This is only for getting some free syntactic sugar in the DSL. Here's the modified Task ADT ..
case class Task(name: String) {
def dependsOn(on: Task): Writer[Endo[Project], Task] = {
for {
_ <- tell(((p: Project) => withDependency(this, on, p)).endo)
} yield this
}
}
Finally we define the last API of our DSL that glues together the building of the Project and the addition of tasks and dependencies without directly coupling the user to some of the underlying implementation artifacts.
def project(name: String, startDate: Date)(e: Writer[Endo[Project], Task]) = {
val p = Project(name, startDate)
e.run._1(p)
}
And we can finally create a Project along with tasks and dependencies using our DSL ..
project("xenos", now) {
for {
a <- task("study customer requirements")
b <- task("analyze usecases")
_ <- b dependsOn a
c <- task("design & code")
_ <- c dependsOn b
d <- c dependsOn a
} yield d
}
In case you are interested I have the whole working example in my github repo.

by Debasish Ghosh (noreply@blogger.com) at February 15, 2013 12:05 PM

February 14, 2013

Tony Sloane

String Interpolation in Scala 2.10

I spoke at a recent ScalaSyd meeting about string interpolation in the Scala 2.10 release. You can find the slides here, but since there is little in the way of explanation in them, I’ll be blogging here to explain the examples.

I begin in this post with the basics of string interpolation via processed strings in Scala 2.10. Later posts show how to write your own interpolators that implement custom value construction as well as pattern matching. The final posts show how macros can be used to get compile-time guarantees about the content of your processed strings.

The complete code for all examples in this series will be made available in the accompanying BitBucket project.

The story so far

Scala from before 2.10 supports basic string literals delimited by single quotes. Single-quoted literals can contain escape sequences but not newlines.

"A string on one line"

A triple-quoted form is also available in which non-Unicode escape sequences are not interpreted and newlines can occur.

"""A long string with only Unicode escapes and


possibly newlines in it
"
""

Commonly used techniques for constructing strings include using the plus operator to concatenate the pieces.

"The " + animal1 + " jumped over the " + animal2

Fans of printf-style string formatting can use the format method to build their strings.

"The %s jumped over the %s".format (animal1, animal2)

Processed strings

Scala 2.10 extended the syntax by adding processed strings that allow expression values to be interpolated (inserted) into the middle of a string literal.

Interpolation is requested by prefixing a string literal with an identifier. There must be nothing between the identifier and the literal. The examples I show use the single-quoted form of string literal, but tripled-quoted strings can also be processed in an analogous fashion.

Processed strings can include arbitrary expressions marked by dollar signs. The way in which the dollar-marked expressions are processed depends on the details of interpolator.

For example, the Scala library provides an interpolator called sthat can be used as follows.

val answer = 42
println (s"answer is $answer, dollar is $$")

val animal1 = "fox"
val animal2 = "dog"
println (s"The $animal1 jumped over the $animal2")

println (s"One plus one is ${1 + 1}")

println (s"The inserted expressions are blocks ${
val x =
"
!"
x * 3
}
"
)
answer is 42, dollar is $
The fox jumped over the dog
One plus one is 2
The inserted expressions are blocks !!!

The s interpolator produces a string value by concatenating the constant parts of the string literal (after interpreting escape sequences) with the values of the embedded expressions. A dollar sign is obtained in the output by including two consecutive dollar signs in the literal. If the expression marked by a dollar sign is more than a single identifier it must be enclosed in braces. The final example shows that the expressions are actually block expressions so they can contain local declarations.

The Scala library also contains an interpolator called raw that behaves just like s except that it doesn’t interpret escape sequences in the constant parts of the string literal.

It is important to realise that the expressions embedded in a processed string are checked in the usual way by the Scala compiler. Errors will be reported at compile time.

Formatted interpolation

The string interpolation equivalent of the format method is provided by the Scala library’s f interpolator.

val pi = 3.14159
println (f"pi ($pi) = $pi%1.3f")

val msg = "G'day!"
println (f"msg.length = ${msg.length}%5d")
pi (3.14159) = 3.142
msg.length = 6

The difference between s/raw and f is that in an f string the embedded expressions can be followed by format specifiers beginning with a percent sign. If no format specifier is given for an expression then it defaults to %s so the string value of the expression is used.

An interesting aspect of f is that the interpolation process checks compatibility between the embedded expressions and the format specifiers at compile time. For example, if msg is a string, the following will not compile since a string cannot be formatted as a floating-point value.

f"msg can't be formatted as $msg%1.3f"

This kind of checking goes above and beyond the normal checking that the compiler will do. In the example, the compiler will normally ensure that msg is in scope of its use in the string. The extra checking to make sure that msg is compatible with %1.3fis performed by the interpolation process. Since we want the extra checking to be performed at compile time, it is implemented by a macro that augments the compiler’s capabilities. I’ll show some examples of using macros for this kind of checking later in this series.

What’s next?

As you might expect, the interpolators s, raw and f are not particularly special. It’s easy to write your own and in the next post I’ll show you how.

by inkytonik (noreply@blogger.com) at February 14, 2013 09:06 PM

scala-lang.org

Scala 2.9.3-RC2 now available!

We are happy to announce a new Release Candidate in the Scala 2.9.x maintenance series!

This release candidate is made available for testing purposes only and is not intended for production environments: a final release will follow at the end of the RC cycle. Please help us with the testing of this candidate, and let us know of any issues that you may encounter.

by moors at February 14, 2013 06:55 PM

Scala 2.10.1-RC1 now available!

We are pleased to announce the first release candidate of Scala 2.10.1!

The Scala team and contributors fixed 177 issues since 2.10.0! In total, 242 pull requests were opened on GitHub, of which 225 were merged after having been tested and reviewed.

Please give 2.10.1-RC1 a spin! It's designed to be a drop-in replacement for 2.10.0. We'd love to hear about any regressions since 2.10.0 and will try to fix them before releasing the final version.

by moors at February 14, 2013 06:53 PM

February 13, 2013

Daniel Sobral

Adding numbers the hard way

I finally got to read the second part of Robey Pointer "How to add numbers" blog posts. Thinking about hardware "algorithms" can be interesting because distributed systems face similar problems sometimes. Below is my implementation of Kogge-Stone addition for 32 bits integers. Brent-Kung hybrid is very clever, but I couldn't figure out an obvious "step" for Brent-Kung that could be recursed into.

def add(x: Int, y: Int, cin: Boolean) = {
def intToBooleanArray(n: Int): Array[Boolean] = {
(0 until 32 map ((1).<<) map (n.&) map (0.!=)).toArray
}

val xs: Array[Boolean] = intToBooleanArray(x)
val ys: Array[Boolean] = intToBooleanArray(y)

// P means cout depends on cin
// G means cout is 1 regardless of cin
case class PG(p: Boolean, g: Boolean)
def p(a: Boolean, b: Boolean) = a ^ b
def g(a: Boolean, b: Boolean) = a & b

// initial PG from input alone
val pg0 = (xs, ys).zipped map ((a, b) => PG(g = g(a, b), p = p(a, b)))

// Execute combine step until all PGs are final
def combStep(lastPGs: Array[PG], finalPGs: Array[PG], step: Int): Array[PG] = {
// Combines PGs formed by adjacent block of bits
def comb(pga: PG, pgb: PG): PG = PG(p = pgb.p & pga.p,
g = pgb.g | (pgb.p & pga.g))

if (lastPGs.isEmpty) finalPGs
else {
val (newFinalPGs, tempPGs) = lastPGs splitAt (step - step / 2)
val nextPGs = (finalPGs ++ lastPGs, tempPGs).zipped map comb

combStep(nextPGs, finalPGs ++ newFinalPGs, step << 1)
}
}

val pgs = combStep(pg0, finalPGs = Array.empty, step = 1)

// Carry for each bit
def cout(cin: Boolean)(pg: PG): Boolean = pg.g | (pg.p & cin)
val cn = pgs map cout(cin)

// Final result for each bit
val sn = (pg0 map (_.p), cin +: (cn take 31)).zipped map ((p, c) => p ^ c)

// Convert boolean array back into an int
val result = (sn.zipWithIndex map {
case (s, i) => if (s) 1 << i else 0
}).sum
(result, cn.last)
}

by Daniel Sobral (noreply@blogger.com) at February 13, 2013 04:07 AM

Paul Chiusano

The design of a decentralized currency (part 1)

This is the first of a series of blog posts discussing issues involved in the design of a decentralized (central bank free) system of digital currency. The goal is a currency that avoids typical pitfalls associated with fiat currency and central banking, as well as problems associated with commodity-backed currencies like a gold standard or simplistic digital systems like Bitcoin. I am interested in exploring the idea that currency design is essentially a computational problem, and that the functions of a central bank might be better handled by a decentralized 'smart' currency that becomes possible with software in the digital world.

This should hopefully be a very beginner-friendly series of posts, though it should be of interest and accessible to both economists and programmers. I will start from elementary first principles and use the opportunity to introduce topics and terms that are relevant. (For instance, for programmer and computer scientist readers, I won't assume any prior knowledge of finance, monetary systems or banking, and for economist readers, I won't assume any knowledge of distributed algorithms, cryptography, and so on)

The purpose of money

Before launching into the design of a monetary system, we need to answer a fundamental question: what is the purpose of money?

Let's consider a hypothetical economy in which there are just two goods, apples and oranges. Suppose that hypothetical market participant Alice has an apple, and Bob has an orange, but Alice would really prefer to eat an orange (Bob's, or really any comparable orange!) and likewise Bob would really prefer an apple. Even without money involved, in a strictly barter economy, Alice and Bob can trade--Alice gives Bob her apple in exchange for Bob's orange. (Aside: Alice and Bob are common placeholder names used in discussions of crypography)

Let's notice some things:

  • If this transaction is voluntary, which we have assumed it is, then both Alice and Bob are better off as a result of making this exchange, otherwise they would not have agreed to it. We say the transaction is mutually beneficial to all parties involved.
  • Actually, to be a bit more precise, Alice and Bob will agree to the exchange so long as they both believe they will be better off. In reality, each may learn that they are not better off after all. For instance, Alice may discover that Bob's orange was in fact rotten and regret the exchange (after all, a perfectly good apple is better than a rotten orange)--that is, the exchange may have been made with bad or imperfect information. Or Bob may be giving up his orange when in fact, if he were really being honest with himself, he much prefers oranges--that is, Bob may be irrational. Although these are real problems and econonomists have studied them extensively, we are not going to talk about them here, since they are orthogonal to the problem of designing a monetary system.

Let's now introduce currency. Suppose Alice has $1, and an apple, but would prefer an orange, and Bob has an orange. Assume the 'going rate' (the price) for oranges in this economy is $1--Alice pays Bob $1 for his orange, and he turns around and pays Alice $1 for her apple (again, assuming the going rate for oranges is $1). The same exchange has been conducted as before, but it has been mediated by the use of currency. We can think of currency as enforcing a very simple tit-for-tat system. If Alice provides something of value to Bob (say, her apple), she obtains a token (the $1) that she can use to obtain something of equivalent value from someone else (or multiple people), perhaps Bob, or perhaps 50 others, from whom she purchases a sunflower seed each (for Alice, assuming she would trade her apple for 50 sunflower seeds).

But the use of money can conveniently facilitate a number of other mutually beneficial transactions beyond what simple bartering can achieve, for instance:

  • Suppose Alice's true favorite fruit is mangos and Carol has a mango but would prefer an orange. Alice can buy Bob's orange for $1, then use the proceeds to purchase a mango from Carol, which Carol then uses to purcahse an apple from Alice. A mutually beneficial exchange has taken place in which Alice, Bob, and Carol each end up with their preferred fruit, but Bob and Carol did not even need to know about each other, nor did the three parties have to get together and agree on any sort of explicit contract!
  • Alice may decide she's getting a bit tired of mangos. She sells her mango now, and a few weeks later, after her love of mangos has returned, she buys another mango from someone else. Alice has traded 1 mango in the present in exchange for a mango in the future. This sort of exchange, trading something now for something in the future, raises further questions that we'll discuss in the next post.
  • Fast-forward to the modern economy. Alice may work as a rocket scientist for Acme Rockets, Inc., where she earns a salary. She buys a cup of coffee one morning for $5. She has effectively traded a portion of her time and expertise as a rocket scientist for a cup of coffee. The coffee shop does not need to know or care about rockets--the shop will use the proceeds from the sale to purchase other goods and services it values.

Even these do not even begin to exhaust the possible mutually beneficial transactions that we can imagine. But in the case of each such transaction, we could imagine writing up a contract that specifies who is to provide and receive what, and getting all parties to agree to and sign the contract. For instance:

Effective June 3, 1999, at 3:45pm EDT:

Alice is to provide an apple,
Carol is to provide a mango, and
Bob is to provide an orange,

AND

Alice is to receive a mango,
Carol is to receive an orange, and
Bob is to receive an apple.

Signed,

Alice: ___________________
Bob: ___________________
Carol: ___________________

Currency can be thought of as a much more convenient way to arrange and execute these sorts of contracts, which could in principle be arbitrarily complex, involve thousands of people, multiple timescales, and so on (we'll see examples of this later).

The more technical way to state that money is a more convenient is that transactions mediated by currency have lower transaction costs than direct barter or using explicit contracts. In general, when multiple parties enter into some mutually beneficial transaction, there are some costs to doing so. I am using 'cost' in a more abstract sense here. The cost could be a literal fee, like a sales tax imposed by a government, or it could simply be some burden the parties to the contract will have to bear for the transaction to take place. In a direct barter economy, these costs would include, among other things, the cost to Alice and/or Bob of traveling to the same physical location to conduct the exchange (or having to pay for some shipping service), the extra work that Alice must do to first to obtain an orange from Bob before finding Carol and exchanging for her mango, and so on. If transactions are conducted with explicit contracts, there will be overhead to writing up the contract, having all parties read and understand it, perhaps hire lawyers, etc. (Not to mention paying for the system of government likely needed to enforce such contracts, mediate disputes, and so forth)

Even more abstract, if buyers and sellers have difficulty finding each other, this can result in a form of transaction costs if the seller has to accept a lower price than he would otherwise be able to obtain if buyers and sellers could find each other more easily. For instance, suppose Alice doesn't know about Bob, but only knows about Dave, who demands two apples in exchange for an orange. Alice may decide this transaction wouldn't benefit her and decide to keep her apple, when if she and Bob could have been paired up they could have made an exchange that benefited them both. The technical way to state this is that insufficient liquidity can result in higher transaction costs. We will discuss this much more in a later part.

Transaction costs are a kind of friction in the economy. They prevent what would otherwise be mutually beneficial transactions from taking place. It would be preferable to Alice not to have to travel to wherever Bob and Carol are; it would be preferable to Alice not to have to pay a lawyer to write up an explicit fruit-exchange contract, or even to have to read a contract at all, even if comes in an email and she has to spend fifteen seconds reading it before replaying 'Yes'.

We can now state the primary goal of a monetary system: facilitate mutually beneficial transactions, with a minimum of transaction costs. We can see that, all else being equal, we would prefer that transaction costs be as low as possible, to promote the maximum number of mutually beneficial transactions.

From here, things will get more complicated. From here we will discuss banking, loans, interest rates, inflation, economic growth, the velocity of money, aggregate demand, etc. It's easy to get lost in these discussions, to forget that money has no intrinsic value (nor should it!) and is merely a more convenient way of arranging, agreeing to, and executing mutually beneficial contracts. Any collection of exchanges of money and goods could in principle be represented using explicit contracts--even exchanges involving billions of dollars, multiple countries, timescales, and huge numbers of people. When deciding on how to engineer a monetary system, we will return again and again to the perspective: what will ensure that parties can participate in mutually beneficial transactions, with a minimum of transaction costs?

By the way, I realize these examples involving Alice, Bob, and Carol might seem silly or trivial. And this is exactly the point; simple examples like this are often the most illuminating. I'll end with a relevant quote from Paul Krugman, from an essay where he uses a hypothetical economy producing only hot dogs and hot dog buns (!) to reason about questions of international trade:

You can't do serious economics unless you are willing to be playful. Economic theory is not a collection of dictums laid down by pompous authority figures. Mainly, it is a menagerie of thought experiments--parables, if you like--that are intended to capture the logic of economic processes in a simplified way. In the end, of course, ideas must be tested against the facts. But even to know what facts are relevant, you must play with those ideas in hypothetical settings.

We're only just getting started! Stay tuned for part 2.

by Paul Chiusano (noreply@blogger.com) at February 13, 2013 03:15 AM

February 11, 2013

Detering Dirk

Functors in images

During the long break between my posts in the end of last year I pondered how a visual representation for all that Functor, Monad, Applicative stuff would look like. I myself are a visual thinker, i.e. thinking always involves images moving and rotating through my head. Visual representation now helps to get an imagination of concepts and make thinks more clear, at least to me.

Now, this is the first post where I try to present a visual representation. Fiddling with images on a computer now is not my kind of art, and time is scarce. But I didn’t want to wait longer to present a first result, so bare with some lack of aesthetics in the pictures.

This post is about Functors, or more precisely the map method, and is especially related to my post: Functors, Monads, Applicatives – different implementations . It is therefore recommended that you (re)read that one, and best its predecessor,  before. (I thought about changing those posts to include the images, but decided against it).

Now, it all starts with an instance of class MyBox[A] and a function from A to B, in this case a MyBox[String] and a function String=>Int :

FunctorsInGraphics-1

As we have seen in the post referenced above, map can be implemented as a standalone function or as a method on class MyBox. We will begin with the latter. Let us now turn the MyBox image slightly in our direction:

FunctorsInGraphics-2

We now have this MyBox thing and the function. Let us now think about the function to be some sort of pipe that can take a string on one end and outputs an integer on the other end. Calling the map method/function is now comparable to plumbing the pipe to the MyBox thing:

FunctorsInGraphics-3

Ok, here we “put the pipe on it”. Beneath the picture you see three different ways to implement map, and the first one in black is the one this image is about .

When we now let the plumbing do its work due to the map method, it not only gives the result value of the mapped method, but this value is wrapped in a MyBox again.

So this is the complete picture of this scene:

FunctorsInGraphics-4

This is very easily grokkable, and holds for may cases where map is implemented as a method on a class. For example for Option, or for collections:

FunctorsInGraphics-8

Here is how it is applied maybe to a List. The original box has many values, and the plumbing puts each into the pipe and produces a similar box with the output values.

That was easy and matches mostly the first experiences we make with map in Scala. But the original notion presented in the first post was more according to this:

FunctorsInGraphics-6

Map is here thought as being a function that converts (lifts) a function from A to B into a function from M[A] to M[B]. Fed with a function String=>Int, it produces a function  MyBox[String]=>MyBox[Int]. And this “lifting”, presented in the right way, seems more in the line of thinking of category theorists.

FunctorsInGraphics-7-2

We now have  a world with some values of some types on the left, together with functions between those types. In the picture the types are String (value “hello”) and Int (value 5), and the function is what you see in the red box. In this imagination we consider the function map now being a pipe, in fact one which moves the function from the left side world to the right side world, where any value is hidden in a box of some type (here: MyBox). And as surely as the original function could work with “hello” and produce the 5, the transfered function can handle a “hello” in a MyBox and produce a MyBox with a 5 in it.

Hopefully you found this representation of map with images as useful as I did. I try to use this way of representation in further posts, perhaps even inline in the “Functors, Monads, Applicative” series.


Filed under: FunctionalProgramming, Scala

by de.velopmind at February 11, 2013 02:40 PM

February 10, 2013

Coderspiel

The tag left on the sofa says everything you need to know about...



The tag left on the sofa says everything you need to know about this airbnb.

February 10, 2013 02:19 PM

February 09, 2013

Coderspiel

Here’s @avibryant engaging in some hand waving about approximate...



Here’s @avibryant engaging in some hand waving about approximate collections, #nescala day 1

February 09, 2013 06:10 PM

February 07, 2013

Yuvi Masory