Skip to content

Instantly share code, notes, and snippets.

View sellmair's full-sized avatar
🤪

Sebastian Sellmair sellmair

🤪
View GitHub Profile
@sellmair
sellmair / Request.kt
Last active May 10, 2018 12:33
Phantom Types in Kotlin: Modelling a Pipeline. Request Inhertiance
/**
* This is our base request.
* It contains the image taken by the camera
* The timestamp when it was taken (because it obviously matters)
* And some additional settings for the measure algorithm
*/
open class Request(
val image: Image,
val timestamp: Timestamp,
val settings: MeasureSettings)
@sellmair
sellmair / Request.kt
Last active May 10, 2018 15:39
Phantom Types in Kotlin: Modelling a Pipeline: Request<Stage>
/*
* This is our Request type. It is basically a holder for all
* information we could wish from it. But we cannot read anything from it directly.
* It has a type param T.
* T is not used anywhere in Request directly, hence why it is called Phantom Type.
*/
class Request<T> (
private val image: Image?,
private val timestamp: Timestamp?,
private val measureSettings: MeasureSettings?,
@sellmair
sellmair / ReadRights.kt
Created May 10, 2018 15:41
Phantom Types in Kotlin: Modelling a Pipeline 'Read-Right-Interfaces'
interface ReadRawImage
interface ReadTimestamp
interface ReadMeasureSettings
interface ReadPreparedImage
interface ReadShapes
interface ReadFeatures
interface ReadPeople
@sellmair
sellmair / Request.kt
Created May 10, 2018 15:49
Phantom Types in Kotlin: Modelling a Pipeline Request with static accessor
class Request<T>(
private val image: Image?,
private val timestamp: Timestamp?,
private val measureSettings: MeasureSettings?,
private val preparedImage: Mat?,
private val scaledPreparedImage: Mat?,
private val shapes: List<Shape>?,
private val features: List<Feature>?,
private val people: List<Person>?) {
@sellmair
sellmair / RequestExtensions.kt
Created May 10, 2018 15:56
Phantom Types in Kotlin: Modelling a Pipeline RequestExtensions
val Request<out ReadRawImage>.image get() = Request.imageOf(this)
val Request<out ReadTimestamp>.timestamp get() = Request.timestampOf(this)
val Request<out ReadMeasureSettings>.measureSettings get() = Request.measureSettingsOf(this)
val Request<out ReadPreparedImage>.image get() = Request.preparedImageOf(this)
val Request<out ReadPreparedImage>.scaledImage get() = Request.scaledPreparedImageOf(this)
val Request<out ReadShapes>.shapes get() = Request.shapesOf(this)
val Request<out ReadFeatures>.features get() = Request.featuresOf(this)
val Request<out ReadPeople>.people get() = Request.peopleOf(this)
@sellmair
sellmair / Stage.kt
Created May 10, 2018 16:01
Phantom Types in Kotlin: Modelling a Pipeline Stages
interface Stage : ReadMeasureSettings, ReadTimestamp {
interface Pending : Stage, ReadRawImage
interface ShapeDetection : Stage, ReadPreparedImage
interface FeatureDetection : Stage, ReadPreparedImage, ReadShapes
interface PersonDetection : Stage, ReadPreparedImage, ReadFeatures
interface Measurement : Stage, ReadPeople
}
```
interface PatternDetector {
fun findPatterns(data: List<Weight>): List<Pattern>
}
```
class OverlapFreePatternDetectorConenctor (
private val first: PatternDetector,
private val second: PatternDetector): PatternDetector {
override fun findPatterns(data: List<Weight>): List<Pattern> {
val firstPatterns = first(data)
val secondPatterns = second(data)
val overlaps = overlaps(firstPatterns, secondPatterns)
fun PatternDetector.combineWithoutOverlap(other: PatternDetector): PatternDetector {
return OverlapFreePatternConnector(this, other)
}
val patternDetector: PatternDetector = (TrendDownPatternDetector().combineWithoutOverlap(TrendUpPatternDetector()))
.combine(CheatDayPatternDetector().combineWithoutOverlap(SelfLyingPatternDetector())
.combine(DietPatternDetector())
.combine(MotivationBoostPatternDetector())
.combine(...)
...