Created
September 20, 2014 10:45
-
-
Save donnut/e43a197f7b245a721020 to your computer and use it in GitHub Desktop.
Typescript implementation of https://github.com/Dobiasd/articles/blob/master/from_oop_to_fp_-_inheritance_and_the_expression_problem.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference path='dst/ramda.d.ts' /> | |
import R = require('ramda'); | |
import Basic = require('./base'); | |
import Base = Basic.Base; | |
module Bar { | |
export function bar(s: string): Base { | |
return { | |
step: stepBar(s), | |
display: displayBar(s) | |
}; | |
} | |
var stepBar = R.curry(function(s: string, delta: number): Base { | |
return bar(s + ' ' + delta.toString()); | |
}); | |
var displayBar = function(s: string): string { | |
return s; | |
} | |
} | |
export = Bar; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Base { | |
export interface Base { | |
step(i: number): Base; | |
display: string; | |
} | |
} | |
export = Base; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference path='dst/ramda.d.ts' /> | |
import R = require('ramda'); | |
import Basic = require('./base'); | |
import Base = Basic.Base; | |
module Foo { | |
export function foo(i: number): Base { | |
return { | |
step: stepFoo(i), | |
display: displayFoo(i) | |
}; | |
} | |
var stepFoo = R.curry(function(i: number, delta: number): Base { | |
return foo(i+delta); | |
}); | |
var displayFoo = function(i: number): string { | |
return i.toString(); | |
} | |
} | |
export = Foo; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference path='dst/ramda.d.ts' /> | |
import R = require('ramda'); | |
import Basic = require('./base'); | |
import Foo = require('./foo'); | |
import Bar = require('./bar'); | |
import Base = Basic.Base; | |
function stepAll(l: Base[]): Base[] { | |
return R.map(function(b: Base) { | |
return b.step(1); | |
}, l); | |
} | |
function displayAll(l: Base[]) { | |
return R.map(function(b: Base) { | |
return b.display; | |
}, l).join('\n'); | |
} | |
(function main() { | |
var l: Base[] = [Foo.foo(0), Bar.bar('')]; | |
var l1 = stepAll(stepAll(l)); | |
console.log(displayAll(l1)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment