aaron.harnly.net » geek http://harnly.net Sì, abbiamo un'anima. Ma è fatta di tanti piccoli robot. Sat, 23 Aug 2008 04:59:59 +0000 http://wordpress.org/?v=abc en Lazy vals with implicit parameters http://harnly.net/2008/blog/geek/lang/scala/lazy-vals-with-implicit-parameters/ http://harnly.net/2008/blog/geek/lang/scala/lazy-vals-with-implicit-parameters/#comments Thu, 17 Jul 2008 15:04:54 +0000 aaronharnly http://harnly.net/?p=176 Lazy vals were an excellent addition to Scala 2.6.0. They allow you to defer calculation of a value until it is actually needed, after which the calculated value is cached.

Unfortunately, this doesn’t play so well with another wonderful Scala feature — implicit parameters. It’s often the case that you’ll want to defer resolution of an implicit until a method call, rather than in the scope of object creation. But lazy values cannot take any parameters, implicit or otherwise — so it seems you’re stuck.

Memoized to the rescue! This is a simple helper class that takes care of the caching of a result. Where you might have had the following method with an implicit parameter:

def foo(implicit bar: Bar): ResultType = { ... }

you instead write the following:

private var _foo = Memoized[ResultType]
def foo(implicit bar: Bar) = _foo or { ... }

(complete source below the fold)

That’s it; nothing fancy. This is not a true memoization solution for a function that takes a parameter — check out the memo package in Scalaz for that. But I’ve found it handy, and you might too.

class Memoized[T] {
	private var value: Option[T] = None
	def isEmpty = value.isEmpty
	def isDefined = ! isEmpty
	def get: T = value.get
	def set(newValue: T) { value = Some(newValue) }
	def getOrSet( newValue: => T): T = if (isDefined) get else {
		set(newValue)
		get
	}
	def or( newValue: => T): T = getOrSet(newValue)
	override def toString = value.toString
}
 
object Memoized {
	def apply[T] = new Memoized[T]
}
]]>
http://harnly.net/2008/blog/geek/lang/scala/lazy-vals-with-implicit-parameters/feed/
The Future Is Now: Treasures in the Carbon docs http://harnly.net/2008/blog/geek/macosx/the-future-is-now-treasures-in-the-carbon-docs/ http://harnly.net/2008/blog/geek/macosx/the-future-is-now-treasures-in-the-carbon-docs/#comments Sun, 10 Feb 2008 18:28:38 +0000 aaronharnly http://harnly.net/2008/blog/geek/macosx/the-future-is-now-treasures-in-the-carbon-docs/ Ran across these funny notes in the Carbon documentation:

This warning that “The Future” could arrive any day:

Important: In the future, individual application processes may not always share the same address space, so in general you should never attempt to access code or data in another process.

and this one is just precious:

Note: Because multitasking allows an operating system to attend to several different operations at the same time, these operations may appear to occur simultaneously on even a single-processor system, due to the speed of the processor.

You don’t say.

]]>
http://harnly.net/2008/blog/geek/macosx/the-future-is-now-treasures-in-the-carbon-docs/feed/
Submitted without comment http://harnly.net/2007/blog/geek/microsoft/submitted-without-comment/ http://harnly.net/2007/blog/geek/microsoft/submitted-without-comment/#comments Wed, 07 Nov 2007 20:59:19 +0000 aaronharnly http://harnly.net/2007/blog/geek/microsoft/submitted-without-comment/ Make of it what you will:

]]>
http://harnly.net/2007/blog/geek/microsoft/submitted-without-comment/feed/
Somebody oughta… http://harnly.net/2007/blog/geek/lang/java/somebody-oughta/ http://harnly.net/2007/blog/geek/lang/java/somebody-oughta/#comments Wed, 25 Apr 2007 20:57:57 +0000 aaronharnly http://harnly.net/2007/blog/geek/lang/java/somebody-oughta/ …write a JavaDoc Doclet so I can use Markdown instead of nasty old HTML in source code comments. How nice would that be?

While we’re at it, somebody oughta write a Markdown plugin for Apple Mail, too. But I think that somebody should be me, so I’ll put off thinking about it for awhile.

]]>
http://harnly.net/2007/blog/geek/lang/java/somebody-oughta/feed/
Somebody oughta… http://harnly.net/2007/blog/geek/lang/java/somebody-oughta-2/ http://harnly.net/2007/blog/geek/lang/java/somebody-oughta-2/#comments Wed, 25 Apr 2007 20:57:57 +0000 aaronharnly http://harnly.net/2007/blog/autobiography/somebody-oughta-2/ …write a JavaDoc Doclet so I can use Markdown instead of nasty old HTML in source code comments. How nice would that be?

While we’re at it, somebody oughta write a Markdown plugin for Apple Mail, too. But I think that somebody should be me, so I’ll put off thinking about it for awhile.

]]>
http://harnly.net/2007/blog/geek/lang/java/somebody-oughta-2/feed/
TextMate bundle for Scala http://harnly.net/2007/blog/geek/macosx/textmate-bundle-for-scala/ http://harnly.net/2007/blog/geek/macosx/textmate-bundle-for-scala/#comments Wed, 25 Apr 2007 18:52:11 +0000 aaronharnly http://harnly.net/2007/blog/computers/macosx/textmate-bundle-for-scala/ I’ve put together a stub of a TextMate bundle for Scala.

At the moment, it just contains a bare-bones language definition, a template for Ant tasks, and a couple of snippets. I’d like to keep improving it, and would welcome suggestions and modifications.

I’ll be setting up anonymous SVN access one of these days (yeah, right), but for the moment, you can download the bundle directly:

ScalaAWH.tmbundle.zip

]]>
http://harnly.net/2007/blog/geek/macosx/textmate-bundle-for-scala/feed/
NLTK’s “ing words”: variations http://harnly.net/2007/blog/geek/lang/ruby/nltks-ing-words-variations/ http://harnly.net/2007/blog/geek/lang/ruby/nltks-ing-words-variations/#comments Sun, 22 Apr 2007 13:36:23 +0000 aaronharnly http://harnly.net/2007/blog/computers/ruby/nltks-ing-words-variations/ NLTK, the “natural language toolkit” for Python, is a wonderful lightweight framework that provides a wealth of NLP tools. The other day, in reading through its documentation, I came across a little appendix describing the advantages of Python for implementing and (especially) teaching NLP.

The authors show a simple sample program to find and list words ending in “ing” from the standard input:

import sys for line in sys.stdin.readlines(): for word in line.split(): if word.endswith('ing'): print word

and contrast this elegant Python implementation with a variety of monstrosities in other languages. I won’t disagree that the Python is nice, but it seemed like a good little exercise to see whether I can’t produce something almost as good in my languages de jour.

To wit, a Ruby version: for line in ARGF for word in line.split if word.match(/ing$/) then puts word
end end end

which is almost identical to the Python version, though showing Ruby’s not-exactly-pretty fascination with the ‘end’ keyword.

And a Scala version using for-comprehensions. Note to Scala creators: It’s really frustrating having the various ways of executing Scala — as a script, as an object, etc. — all disagree slightly on how the outermost wrapper of a procedure should be formatted.

import scala.io._ object IngWords extends Application { for ( val line <- Source.fromInputStream(System.in).getLines; val word <- line.split(" "); word.endsWith("ing") ) Console.println(word)
}

(Aside: I need a decent syntax highlighting package for WP, it seems.)

]]>
http://harnly.net/2007/blog/geek/lang/ruby/nltks-ing-words-variations/feed/
NLTK’s “ing words”: variations http://harnly.net/2007/blog/geek/lang/ruby/nltks-ing-words-variations-2/ http://harnly.net/2007/blog/geek/lang/ruby/nltks-ing-words-variations-2/#comments Sun, 22 Apr 2007 13:36:23 +0000 aaronharnly http://harnly.net/2007/blog/autobiography/nltks-ing-words-variations-2/ NLTK, the “natural language toolkit” for Python, is a wonderful lightweight framework that provides a wealth of NLP tools. The other day, in reading through its documentation, I came across a little appendix describing the advantages of Python for implementing and (especially) teaching NLP.

The authors show a simple sample program to find and list words ending in “ing” from the standard input:

import sys for line in sys.stdin.readlines(): for word in line.split(): if word.endswith('ing'): print word

and contrast this elegant Python implementation with a variety of monstrosities in other languages. I won’t disagree that the Python is nice, but it seemed like a good little exercise to see whether I can’t produce something almost as good in my languages de jour.

To wit, a Ruby version: for line in ARGF for word in line.split if word.match(/ing$/) then puts word
end end end

which is almost identical to the Python version, though showing Ruby’s not-exactly-pretty fascination with the ‘end’ keyword.

And a Scala version using for-comprehensions. Note to Scala creators: It’s really frustrating having the various ways of executing Scala — as a script, as an object, etc. — all disagree slightly on how the outermost wrapper of a procedure should be formatted.

import scala.io._ object IngWords extends Application { for ( val line <- Source.fromInputStream(System.in).getLines; val word <- line.split(" "); word.endsWith("ing") ) Console.println(word)
}

(Aside: I need a decent syntax highlighting package for WP, it seems.)

]]>
http://harnly.net/2007/blog/geek/lang/ruby/nltks-ing-words-variations-2/feed/
each_line do |line| in Scala http://harnly.net/2007/blog/geek/lang/scala/each_line-do-line-in-scala/ http://harnly.net/2007/blog/geek/lang/scala/each_line-do-line-in-scala/#comments Wed, 28 Mar 2007 04:32:16 +0000 aaronharnly http://harnly.net/2007/blog/computers/scala/each_line-do-line-in-scala/ Unit) { val [...]]]> I’m so used to the each_line idiom in Ruby, I felt utterly lost in Scala without it.

Here’s an approximation of it, though I’d love to hear that there’s an even more scala-ish (scalic?) way of doing this:

def eachline(stream : InputStream)(f : String => Unit) { val buf = new BufferedReader( new InputStreamReader( stream ) ) var line = buf.readLine while (line != null) { f(line) line = buf.readLine } }

I use this with a block as so: eachline(System.in) { line => Console.println(line) }

Not bad, but it could be better, I think. Can I tickle this to be a method on streams, instead of a funny little function sitting on its own?

]]>
http://harnly.net/2007/blog/geek/lang/scala/each_line-do-line-in-scala/feed/
each_line do |line| in Scala http://harnly.net/2007/blog/geek/lang/scala/each_line-do-line-in-scala-2/ http://harnly.net/2007/blog/geek/lang/scala/each_line-do-line-in-scala-2/#comments Wed, 28 Mar 2007 04:32:16 +0000 aaronharnly http://harnly.net/2007/blog/autobiography/each_line-do-line-in-scala-2/ Unit) { val [...]]]> I’m so used to the each_line idiom in Ruby, I felt utterly lost in Scala without it.

Here’s an approximation of it, though I’d love to hear that there’s an even more scala-ish (scalic?) way of doing this:

def eachline(stream : InputStream)(f : String => Unit) { val buf = new BufferedReader( new InputStreamReader( stream ) ) var line = buf.readLine while (line != null) { f(line) line = buf.readLine } }

I use this with a block as so: eachline(System.in) { line => Console.println(line) }

Not bad, but it could be better, I think. Can I tickle this to be a method on streams, instead of a funny little function sitting on its own?

]]>
http://harnly.net/2007/blog/geek/lang/scala/each_line-do-line-in-scala-2/feed/