Skip to content

Commit d8c77e2

Browse files
committed
inline everything
GitPrivacy: Du2oDCnk+iPfCtoWzB5CZElHEt4kRwLKFpHcVlgnycXq0E3PyHOl7IxUsMY4ymK5pA+y4ZpIty0= IPcj48PFrcDUfGyyt2jGgh9WC7DrzWjWZYeOsisnJj556aC+Nmjsi3NwCh1n1Q9vAKxq9/HXeW0=
1 parent d46af18 commit d8c77e2

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

src/main/scala/ScalaPlayground/Lift/Immutable/LiftImmutable.scala

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ case class Person(
2020
case _ if destination > position => Up
2121
case _ if destination < position => Down
2222

23-
def matchesDirection(lift: Lift): Boolean = desiredDirection == lift.direction
24-
def isBelow(lift: Lift): Boolean = position < lift.position
25-
def isAbove(lift: Lift): Boolean = position > lift.position
23+
inline def matchesDirection(lift: Lift): Boolean = desiredDirection == lift.direction
24+
inline def isBelow(lift: Lift): Boolean = position < lift.position
25+
inline def isAbove(lift: Lift): Boolean = position > lift.position
2626
}
2727

2828
case class Lift(
@@ -31,15 +31,15 @@ case class Lift(
3131
people: Queue[Person],
3232
capacity: Int
3333
) {
34-
def isFull: Boolean = people.size == capacity
35-
def hasRoom: Boolean = people.size != capacity
36-
def hasPeople: Boolean = people.nonEmpty
37-
def isEmpty: Boolean = people.isEmpty
34+
val isFull: Boolean = people.size == capacity
35+
val hasRoom: Boolean = people.size != capacity
36+
val hasPeople: Boolean = people.nonEmpty
37+
val isEmpty: Boolean = people.isEmpty
3838

39-
def accepts(person: Person): Boolean =
39+
inline def accepts(person: Person): Boolean =
4040
hasRoom && person.desiredDirection == direction
4141

42-
def nearestPassengerTarget: Option[Floor] =
42+
inline def nearestPassengerTarget: Option[Floor] =
4343
people.filter(_.matchesDirection(this)).map(_.destination).minByOption(floor => Math.abs(floor - position))
4444

4545
@tailrec final def pickup(building: Building): (Lift, Building) =
@@ -52,14 +52,14 @@ case class Lift(
5252
val emptierBuilding = building.copy(floors = building.floors.updated(position, emptierQueue))
5353
fullerLift.pickup(emptierBuilding)
5454

55-
def fixDirection(building: Building): Lift = position match
55+
inline def fixDirection(building: Building): Lift = position match
5656
case 0 => copy(direction = Up)
5757
case p if p == building.floors.length - 1 => copy(direction = Down)
5858
case _ => this
5959

60-
def dropOff: Lift = copy(people = people.filter(_.destination != position))
60+
inline def dropOff: Lift = copy(people = people.filter(_.destination != position))
6161

62-
def align(building: Building): Lift =
62+
inline def align(building: Building): Lift =
6363
List(nearestPassengerTarget, building.nearestRequestInSameDirection(this)).flatten
6464
.minByOption(floor => Math.abs(floor - position))
6565
.match
@@ -79,19 +79,19 @@ case class Lift(
7979
case class Building(
8080
floors: Array[Queue[Person]]
8181
) {
82-
def isEmpty: Boolean = floors.forall(_.isEmpty)
83-
def hasPeople: Boolean = !isEmpty
82+
val isEmpty: Boolean = floors.forall(_.isEmpty)
83+
val hasPeople: Boolean = !isEmpty
8484

85-
private def peopleGoing(direction: Direction): List[Person] =
85+
inline private def peopleGoing(direction: Direction): List[Person] =
8686
floors.flatMap(queue => queue.filter(_.desiredDirection == direction)).toList
8787

88-
def lowestFloorGoingUp(lift: Lift): Option[Floor] =
88+
inline def lowestFloorGoingUp(lift: Lift): Option[Floor] =
8989
peopleGoing(Up).filter(_.isBelow(lift)).map(_.position).minOption
9090

91-
def highestFloorGoingDown(lift: Lift): Option[Floor] =
91+
inline def highestFloorGoingDown(lift: Lift): Option[Floor] =
9292
peopleGoing(Down).filter(_.isAbove(lift)).map(_.position).maxOption
9393

94-
def nearestRequestInSameDirection(lift: Lift): Option[Floor] =
94+
inline def nearestRequestInSameDirection(lift: Lift): Option[Floor] =
9595
lift.direction match
9696
case Up => peopleGoing(Up).filter(_.isAbove(lift)).map(_.position).minOption
9797
case Down => peopleGoing(Down).filter(_.isBelow(lift)).map(_.position).maxOption
@@ -102,15 +102,15 @@ case class LiftSystem(
102102
lift: Lift,
103103
stops: List[Floor]
104104
) {
105-
private def fixDirection: LiftSystem = copy(lift = lift.fixDirection(building))
106-
private def dropOff: LiftSystem = copy(lift = lift.dropOff)
107-
private def align: LiftSystem = copy(lift = lift.align(building))
105+
inline private def fixDirection: LiftSystem = copy(lift = lift.fixDirection(building))
106+
inline private def dropOff: LiftSystem = copy(lift = lift.dropOff)
107+
inline private def align: LiftSystem = copy(lift = lift.align(building))
108108

109-
private def pickup: LiftSystem =
109+
inline private def pickup: LiftSystem =
110110
val (lift2, building2) = lift.pickup(building)
111111
copy(lift = lift2, building = building2)
112112

113-
private def registerStop: LiftSystem =
113+
inline private def registerStop: LiftSystem =
114114
stops.lastOption match
115115
case Some(lastStop) if lastStop == lift.position => this
116116
case _ => copy(stops = stops :+ lift.position)

0 commit comments

Comments
 (0)