juan_gandhi: (Default)
[personal profile] juan_gandhi
scala caseclass in javascript
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

Date: 2010-02-09 07:42 pm (UTC)
From: [identity profile] ex-chrobin.livejournal.com
http://w3future.com/weblog/stories/2008/06/16/adtinjs.xml

Date: 2010-02-10 01:49 am (UTC)
From: [identity profile] vityokr.livejournal.com
is this like a note to self?

Date: 2010-02-10 02:04 am (UTC)
From: [identity profile] ivan-gandhi.livejournal.com
Это серьёзное выражение восторга, и зависти, что сам не догадался. Ведь нефиг делать в джаваскрипте сбацать callcc, folds, shift/reset, state monad, и т.д. и т.п.

Date: 2010-02-10 02:47 am (UTC)
From: [identity profile] vityokr.livejournal.com
it's all greek to me :)

Date: 2010-02-10 03:54 am (UTC)
From: [identity profile] ivan-gandhi.livejournal.com
Греческий простой язык.

Date: 2010-02-10 11:37 am (UTC)
From: [identity profile] vityokr.livejournal.com
мне кажется нет - встречал много иностранцев в греции, все говорили что тяжелый

Date: 2010-02-10 09:59 pm (UTC)
From: [identity profile] ivan-gandhi.livejournal.com
Я там как дома себя чувстовал. По-наглому начинал говорить по-гречески.

Date: 2010-02-11 12:46 am (UTC)
From: [identity profile] vityokr.livejournal.com
ну они javascript тоже не знали, что с них взять :)

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

May 2025

S M T W T F S
    1 2 3
456 7 8 9 10
11 121314151617
181920 21 222324
25262728293031

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated May. 24th, 2025 03:59 am
Powered by Dreamwidth Studios