Skip to content

Commit 2a3abd3

Browse files
Add Expr.ofMap family methods
1 parent 4b3fdf6 commit 2a3abd3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

library/src-bootstrapped/scala/quoted/Expr.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,30 @@ object Expr {
130130
def ofList[T](xs: Seq[Expr[T]])(using Type[T], QuoteContext): Expr[List[T]] =
131131
if (xs.isEmpty) '{ Nil } else '{ List(${Varargs(xs)}: _*) }
132132

133+
/** Lifts the Map the values of which are expressions into an expression of Map. */
134+
def ofMapValues[K: Type, V: Type](m: Map[K, Expr[V]])(
135+
using QuoteContext, Liftable[List[K]]): Expr[Map[K, V]] =
136+
val keys: Expr[List[K]] = Expr(m.keys.toList)
137+
val values: Expr[List[V]] = Expr.ofList(m.values.toList)
138+
'{ ${ keys }.zip(${ values }).toMap }
139+
end ofMapValues
140+
141+
/** Lifts the Map the keys of which are expressions into an expression of Map. */
142+
def ofMapKeys[K: Type, V: Type](m: Map[Expr[K], V])(
143+
using QuoteContext, Liftable[List[V]]): Expr[Map[K, V]] =
144+
val keys: Expr[List[K]] = Expr.ofList(m.keys.toList)
145+
val values: Expr[List[V]] = Expr(m.values.toList)
146+
'{ ${ keys }.zip(${ values }).toMap }
147+
end ofMapKeys
148+
149+
/** Lifts the Map the keys and values of which are expressions into an expression of Map. */
150+
def ofMapKeyValues[K: Type, V: Type](m: Map[Expr[K], Expr[V]])(
151+
using QuoteContext): Expr[Map[K, V]] =
152+
val keys: Expr[List[K]] = Expr.ofList(m.keys.toList)
153+
val values: Expr[List[V]] = Expr.ofList(m.values.toList)
154+
'{ ${ keys }.zip(${ values }).toMap }
155+
end ofMapKeyValues
156+
133157
/** Lifts this sequence of expressions into an expression of a tuple
134158
*
135159
* Transforms a sequence of expression

0 commit comments

Comments
 (0)