scala invading javascript!
Feb. 9th, 2010 11:07 am![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
scala caseclass in javascript
scala option in javascript
partial functions
actors
caseclass.js is an attempt to duplicate the functionality of Scala's case classes in Javascript. This is a work in progress and so far only the class construction works: Create a case class based upon a name and property list like so: CaseClass.create("Person", ["name", "age"]); Instances of the case class can be created like so: var obama = CaseClass.Person("Barack Obama", 47); var mccain = CaseClass.Person("John McCain", 72); var obama2 = CaseClass.Person("Barack Obama", 47); Note that the 'new' keyword is not needed, just like with Scala's case classes. The class instance properties are easily accessible: obama.name // -> "Barack Obama" mccain.age // -> 72 The case class instances can be compared using the equals method: obama.equals(mccain); // -> false obama.equals(obama2); // -> true Case classes can be matched against each other like so: obama.match( { caseTest: mccain, caseFunction: function() { return 1; } }, { caseTest: obama2, caseFunction: function() { return 2; } }, ); // -> 2
scala option in javascript
Note that passing non-values to Some will lead to None being returned: Some(); // -> None Some(undefined); // -> None Get a value: opt.get(); // -> "my option" opt.getOrElse(); // -> "my option" None.get(); // -> Error thrown None.getOrElse("nothing here"); // -> "nothing here" Get an Option's status: opt.isEmpty(); // -> false opt.isDefined(); // -> true None.isEmpty(); // -> true None.isDefined(); // -> false Treat the Option like a sequence: // It's monadic!!! -- vp opt.foreach(function(input) { alert(input) }); // alert("my option") called None.foreach(function(input) { alert(input) }) // nothing called opt.filter(function(input) { return input == "my option" }); // -> Some("my option") opt.filter(function(input) { return input == "my option!" }); // -> None None.filter(function(input) { return input == "my option" }); // -> None; anonymous function not called
partial functions
actors
no subject
Date: 2010-02-09 07:42 pm (UTC)no subject
Date: 2010-02-10 01:49 am (UTC)no subject
Date: 2010-02-10 02:04 am (UTC)no subject
Date: 2010-02-10 02:47 am (UTC)no subject
Date: 2010-02-10 03:54 am (UTC)no subject
Date: 2010-02-10 11:37 am (UTC)no subject
Date: 2010-02-10 09:59 pm (UTC)no subject
Date: 2010-02-11 12:46 am (UTC)