Skip to content

Instantly share code, notes, and snippets.

View pahill's full-sized avatar
👩‍🎤

Pamela Hill pahill

👩‍🎤
View GitHub Profile
/**
* A generic class that describes data with a status
*/
class Resource<T> constructor(val status: Status, var data: T? = null, val message: String? = null, val date: Date? = null) {
companion object {
fun <T> success(data: T?, date: Date?): Resource<T> {
return Resource(status = Status.SUCCESS, data = data, date = date)
}
class UserRepository (private val webservice: UserApi, private val userDao: UserDao, private val appExecutors: AppExecutors){
fun loadUser(userId: Int) : LiveData<Resource<User>>{
return object : NetworkBoundResource<User, User> (appExecutors) {
override fun loadFromDb(): User? {
return userDao.load(userId)
}
override fun getDataFetchDate(data: User?): Date? {
return data?.fetchDate
class MoveAction {
operator fun invoke(units: Int, direction: String) {...}
  operator fun invoke(targetPoint: Point) {...}
}
val move = MoveAction()
val targetPoint = Point(90, 90)
move(5, "left")
move(targetPoint)
class SpaceShuttle {
operator fun invoke(units: Int, direction: String) {...}
operator fun invoke(targetPoint: Point) {...}
}
val spaceShuttle = SpaceShuttle()
val targetPoint = Point(90, 90)
spaceShuttle(5, "left")
spaceShuttle(targetPoint)
fun return1And2(): Pair<String, String> {
return "returnValue1" to "returnValue2"
}
val (r1, r2) = return1And2()
data class FlightPath(private val point1: Point, private val point2: Point){
operator fun contains(point: Point): Boolean {
//Do some maths to determine if the point is on the flightpath
}
operator fun iterator(): Iterator<Point>{
//Iterate over certain checkpoints in the flightpath
}
}
val spaceShuttlePosition = Point(10.0, 10.0)
@pahill
pahill / Index.kt
Last active November 27, 2018 09:58
class Point(private var x: Double,
private var y: Double) {
operator fun get(index: Int): Double { return if(index == 0) x else y }
operator fun set(index: Int, value: Double) { if(index == 0) x = value else y = value}
}
val spaceShuttlePosition = Point(10.0, 10.0)
//Determine the spaceshuttle's x coordinate using the index operator
spaceShuttlePosition[0]
data class Point(private val x: Double,
private val y: Double) {
operator fun minus(other: Point): Double {...}
operator fun compareTo(other: Point): Int {
val distanceToOrigin = this - Point(0.0, 0.0)
val otherDistanceToOrigin = other - Point(0.0, 0.0)
return when {
distanceToOrigin > otherDistanceToOrigin -> 1
distanceToOrigin == otherDistanceToOrigin -> 0
else -> -1
@pahill
pahill / Point.kt
Last active November 27, 2018 10:01
data class Point(private val x: Double,
private val y: Double) {
operator fun minus(other: Point): Double {
//Pythagorian algorithm - not important here!
return sqrt(pow(x - other.x, 2.0) + pow(y - other.y, 2.0))
}
}
val spaceStationPosition = Point(100.0, 100.0)
val spaceShuttlePosition = Point(10.0, 10.0)
@pahill
pahill / Undo.kt
Last active November 27, 2018 10:04
class Memento(private val originalText: String) {
}
interface UndoCommand {
operator fun invoke()
}
class UndoTypingCommand(private val memento: Memento) : UndoCommand {
override operator fun invoke() {
//restore the state using the memento object