option.rb
August 4th, 2012Because I’m nothing if not consistent, I’ve made Yet Another Option Library, this time in Ruby: option.rb
Because I’m nothing if not consistent, I’ve made Yet Another Option Library, this time in Ruby: option.rb
Because my hobby seems to be writing Option libraries in various languages, I’ve written another, in Python: option.py. Enjoy.
Quick note for fellow Lift developers: I was happily using a Menu.param when I decided to use a Menu.params instead (see the ScalaDocs) in the hopes of both more descriptive URLs and less to calculate on the actual page. While implementing it wasn’t too hard, my new URL pattern for the menu entry was a little complicated:
Menu.params[MyObject]( ... ) / "something" / * / * / "to" / * |
I had a something.html Template but it wasn’t getting picked up and I was getting a weird 0 (yes, zero) status code returned when I requested the page, with the MyObject being initialized correctly.
The solution I came up with was to manually specify the Template for the Menu:
Menu.params[MyObject]( ... ) / "something" / * / * / "to" / * >> Loc.Template( () => Templates(List("comparison")) openOr <div>Couldn't find template</div> ) |
Thought it was worth sharing…
For iFebo I built a server last year to handle sending push messages to Urban Airship, which we use in front of the Apple Push Notification service due to its great scheduling features. To do this I needed a way to interact with the UA API and while it’s got lots of features it’s not terribly… consistent.
So, I wrote my own wrapper library which attempts to give you a more consistent and type-safe way to interact with the API. It’s taken me a long time to make it public and I’ve got some work to do to make it up to date, but I’m glad to give you BFUrbanAirship. You can use it like so:
import com.bubblefoundry.bfurbanairship._ val api = new UrbanAirship(app_token, app_secret, app_master_secret, appengine = false) // schedule a message to be pushed val message = SimplePushMessage(aps = Some(APS("A push message"))) api.push(message) // get a Stream of all registered devices api.devices |
Because hey, why not?
In Python:
>>> 1 in [1, 2, 3] True |
In Scala:
class In(a: Any) { def in[T](s: Seq[T]) = s.contains(a) } implicit def any2In(a : Any) = new In(a) scala> "a" in List(1, 2, "a") res0: Boolean = true scala> "a" in List(1, 2, "b") res1: Boolean = false |
This took me a while to figure out, so I figure it’s worth sharing here. In the end it’s quite simple thanks to Parser’s ^? method, but it took me a while to figure out:
Scala doesn’t have dependent types but I wanted to see if I could wrassle up something that would approximate it for non-negative natural numbers. I’ve posted my results on GitHub.
Ruby code often has code with trailing conditionals, which can look pretty neat.
person = Person.get(name) unless name.nil? |
(Note: I really don’t know Ruby, I just saw syntax like this and thought was cool.)
So, of course I had to implement it in Scala:
You get optional values:
Pretty cool, no?
I tried and failed to come up with some sort of fancy extractor object that would let me use unless in pattern matching:
1001 match { case n unless n % 2 == 0 => println(n) } |
Can you figure out a way?
Because I’ve wasted too much time on Casbah tonight, I have to pass it on. Simply, not much is done for you automatically. Connect like this:
val uri = MongoURI("mongodb://username:password@server:port/database") val mongo = MongoConnection(uri) val db = mongo(uri.database) db.authenticate(uri.username, uri.password.foldLeft("")(_ + _.toString)) val collection = db("something") |
If you call db.authenticate() a second time it will throw an exception. Lovely.
I need to tell the Dutch government when renewing my permit if I’ve been outside of the country for more than three months. Here’s how I calculated it thanks to ScalaTime: