• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Scala-Robotics-Simulator / PPS-22-srs / #189

05 Aug 2025 07:44PM UTC coverage: 83.668% (-3.5%) from 87.129%
#189

Pull #27

github

GiuliaNardicchia
refactor: redesign Action trait to support effect types, with the tagless final pattern and introduce MovementAction for speed-based actions
Pull Request #27: refactor: actuators and actions

42 of 50 new or added lines in 4 files covered. (84.0%)

17 existing lines in 2 files now uncovered.

584 of 698 relevant lines covered (83.67%)

12.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/scala/io/github/srs/model/entity/dynamicentity/behavior/Behavior.scala
1
package io.github.srs.model.entity.dynamicentity.behavior
2

3
import scala.annotation.targetName
4

5
/**
6
 * A Behavior is a function that takes an input of type `I` and produces a sequence of actions of type `A`.
7
 *
8
 * @param I
9
 *   the input type, which can be any type that provides context for the behavior
10
 * @param A
11
 *   the output/action type, which can be any type that represents an action or effect
12
 */
13
opaque type Behavior[-I, +A] = I => Seq[A]
UNCOV
14

×
15
/**
16
 * Constructors and combinators for creating and combining behaviors.
17
 */
18
object Behavior:
UNCOV
19

×
20
  /**
21
   * Lift an existing function into a [[Behavior]].
22
   *
23
   * @param f
24
   *   a function from `I` to [[Seq[A]]]
25
   * @tparam I
26
   *   the input type
27
   * @tparam A
28
   *   the output/action type
29
   * @return
30
   *   a [[Behavior]] wrapping `f`
31
   */
32
  inline def apply[I, A](f: I => Seq[A]): Behavior[I, A] = (in: I) => f(in)
UNCOV
33

×
34
  /**
35
   * Create a [[Behavior]] that always returns the same action.
36
   *
37
   * @param a
38
   *   the action to return
39
   * @tparam I
40
   *   the input type
41
   * @tparam A
42
   *   the output/action type
43
   * @return
44
   *   a [[Behavior]] that always returns `a`
45
   */
46
  inline def pure[I, A](a: A): Behavior[I, A] = (_: I) => Seq(a)
47

48
  /**
49
   * Create a [[Behavior]] that produces no actions.
50
   *
51
   * @tparam I
52
   *   the input type
53
   * @tparam A
54
   *   the output/action type
55
   * @return
56
   *   a [[Behavior]] that produces an empty sequence of actions
57
   */
58
  def empty[I, A]: Behavior[I, A] = (_: I) => Seq.empty
UNCOV
59

×
60
  /**
61
   * Create a [[Behavior]] that executes actions based on a predicate.
62
   *
63
   * @param p
64
   *   a predicate function that takes an input of type `I` and returns a Boolean
65
   * @param actions
66
   *   the actions to execute if the predicate is true
67
   * @tparam I
68
   *   the input type
69
   * @tparam A
70
   *   the output/action type
71
   * @return
72
   *   a [[Behavior]] that executes `actions` if `p(in)` is true, otherwise produces no actions
73
   */
74
  def when[I, A](p: I => Boolean)(actions: => Seq[A]): Behavior[I, A] =
UNCOV
75
    (in: I) => if p(in) then actions else Seq.empty
×
UNCOV
76

×
77
  /**
78
   * Extension methods for the `[[Behavior]]` for manipulation and execution.
79
   */
80
  extension [I, A](self: Behavior[I, A])
81
    /**
82
     * Execute the behavior with the given input.
83
     *
84
     * @param in
85
     *   the input to the behavior
86
     * @return
87
     *   a sequence of actions produced by the behavior
88
     */
89
    inline def execute(in: I): Seq[A] = self(in)
90

91
    /**
92
     * Transform the actions produced by the behavior using a function.
93
     *
94
     * @param f
95
     *   the function to apply to each action
96
     * @tparam B
97
     *   the type of the transformed actions
98
     * @return
99
     *   a new Behavior that produces actions of type `B`
100
     */
101
    def map[B](f: A => B): Behavior[I, B] = (i: I) => self(i).map(f)
UNCOV
102

×
103
    /**
104
     * Filter the actions produced by the behavior based on a predicate.
105
     * @param p
106
     *   the predicate function to filter actions
107
     * @return
108
     *   a new Behavior that only produces actions satisfying the predicate
109
     */
110
    def filter(p: A => Boolean): Behavior[I, A] = (i: I) => self(i).filter(p)
UNCOV
111

×
112
    /**
113
     * Combine two behaviors into one.
114
     *
115
     * @param that
116
     *   the other behavior to combine with
117
     * @return
118
     *   a new [[Behavior]] that produces actions from both behaviors
119
     */
120
    @targetName("concat")
UNCOV
121
    infix def ++(that: Behavior[I, A]): Behavior[I, A] =
×
UNCOV
122
      (i: I) => self(i) ++ that(i)
×
UNCOV
123

×
124
    /**
125
     * Alias for `++`, providing a more readable syntax for combining behaviors.
126
     * @param that
127
     *   the other behavior to combine with
128
     * @return
129
     *   a new [[Behavior]] that produces actions from both behaviors
130
     */
131
    infix def andAlso[B](that: Behavior[I, A]): Behavior[I, A] = (i: I) => self(i) ++ that(i)
UNCOV
132

×
133
    /**
134
     * Combine two behaviors such that the second behavior is executed only if the first produces no actions.
135
     *
136
     * @param that
137
     *   the other behavior to combine with
138
     * @return
139
     *   a new [[Behavior]] that produces actions from the first behavior, or from the second if the first is empty
140
     */
141
    @targetName("alt")
UNCOV
142
    infix def <|>(that: Behavior[I, A]): Behavior[I, A] =
×
UNCOV
143
      (i: I) =>
×
UNCOV
144
        val as = self(i)
×
145
        if as.nonEmpty then as else that(i)
146

147
    /**
148
     * Alias for `<|>`, providing a more readable syntax for combining behaviors.
149
     * @param that
150
     *   the other behavior to combine with
151
     * @return
152
     *   a new [[Behavior]] that produces actions from the first behavior, or from the second if the first is empty
153
     */
154
    infix def orElse(that: Behavior[I, A]): Behavior[I, A] = self <|> that
UNCOV
155
  end extension
×
156

157
end Behavior
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc