Beginning Ceylon
Cylon
So, this is the second time that I have taken a look at the programming language Ceylon. Ceylon is a language which is currently being sponsored by Red Hat. It fits into the space of languages in the same family with Java, but attempting to build a mondern strongly-typed language within that family; Scala is the best-known language that falls into the same category. Of course, Scala has a 10 year head-start on Ceylon, but so far I am really QUITE impressed with Ceylon as a language.
The first time I looked at Ceylon, I simply read through their excellent tutorial (which I strongly recommend) and I came away with the impression that the language was really VERY well-designed. The second time I came back to it and had exactly the same impression. So I decided to play around and try it out some. The program below is my first attempt at building anything (even a toy) in Ceylon. Because there seemed to be very little available in the way of sample Ceylon code posted online, I thought I would post it here.
File: source/com/mcherm/example/module.ceylon
native("jvm") module com.mcherm.example "1.0.0" { import java.base "8"; import ceylon.net "1.2.1"; import ceylon.regex "1.2.1"; }
File: source/com/mcherm/example/package.ceylon
shared package com.mcherm.example;
File: source/com/mcherm/example/run.ceylon
import ceylon.net.uri {parseURI = parse} import ceylon.regex {regex, MatchResult} String? loadContentFromCeylonDownloadsWebsite() { value uri = parseURI("http://ceylon-lang.org/download-archive/"); value response = uri.get().execute(); if (response.status == 200) { return response.contents; } else { return null; } } /* * A good HTML parser would be the BEST way to handle this, * but for this example we'll just use a regex to find what * we need. That works, but it is a bit fragile if the page * ever changes. */ String? getFirstVersionFromCeylonDownloadsPage(String pageHtml) { value h3TagRegex = regex("<h3 id=\"(.+)\">"); MatchResult? match = h3TagRegex.find(pageHtml); if (!exists match) { return null; } return match.groups.first; } "This is the main method that runs the whole program." shared void main() { String? pageHtml = loadContentFromCeylonDownloadsWebsite(); if (!exists pageHtml) { print("Could not access website."); return; } else { String? version = getFirstVersionFromCeylonDownloadsPage(pageHtml); if (!exists version) { print("Could not get the version from the page."); } else { print("The latest released version of Ceylon is ``version``."); } } }
Posted Sat 27 February 2016 by mcherm in Programming