Skip to content

Commit 679e82a

Browse files
committed
Introduce the 'flat_map' built-in method in the ''Iterable' type
1 parent 860a3b2 commit 679e82a

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

lkql_jit/language/src/main/java/com/adacore/lkql_jit/built_ins/methods/IterableMethods.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,26 @@ protected BaseLKQLLazyList onIterable(Iterable iterable) {
121121
return new LKQLFlattenResult(iterable);
122122
}
123123
}
124+
125+
@BuiltInMethod(
126+
name = "flat_map",
127+
doc = """
128+
Given an iterable and a function that takes one argument and return another iterable \
129+
value, return a new iterable, result of the function application on all elements, flatten \
130+
in a sole iterable value. The returned iterable value is lazy."""
131+
)
132+
abstract static class FlatMapExpr extends BuiltInBody {
133+
134+
@Specialization(
135+
limit = Constants.SPECIALIZED_LIB_LIMIT,
136+
guards = "function.parameterNames.length == 1"
137+
)
138+
protected BaseLKQLLazyList onIterable(
139+
Iterable iterable,
140+
LKQLFunction function,
141+
@CachedLibrary("function") InteropLibrary functionLibrary
142+
) {
143+
return new LKQLFlattenResult(new LKQLMapResult(iterable, function, functionLibrary));
144+
}
145+
}
124146
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Test a simple flat map
2+
val lst = [1, 2, 3]
3+
val fm_1 = lst.flat_map((x) => [x * x, x.img])
4+
print(fm_1.to_list)
5+
6+
# Ensure flat map is lazy
7+
val fm_2 = lst.flat_map((x) => { print("Processing " & x.img); [x * x, x.img] })
8+
print(fm_2[1].img)
9+
print(fm_2[2].img)
10+
print(fm_2[5].img)
11+
print(fm_2[4].img)
12+
13+
# Check that function not returning iterables triggers an error
14+
val fm_3 = lst.flat_map((x) => x*2)
15+
print(fm_3[1])
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[1, "1", 4, "2", 9, "3"]
2+
Processing 1
3+
1
4+
"1"
5+
Processing 2
6+
Processing 3
7+
9
8+
"2"
9+
error: Type error: expected Iterable but got Int
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
driver: 'interpreter'
2+
project: 'default_project/default.gpr'
3+
lkt_refactor: True

user_manual/generated/std.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ Methods for `LazyList`
173173

174174
Return the content of the iterable object with each element associated to its index in a tuple: [(<index>, <elem>), ...]
175175

176+
.. method:: LazyList.flat_map(this, function)
177+
178+
Given an iterable and a function that takes one argument and return another iterable value, return a new iterable, result of the function application on all elements, flatten in a sole iterable value. The returned iterable value is lazy.
179+
176180
.. method:: LazyList.flatten(this)
177181

178182
Given an iterable of iterables, flatten all of them in a resulting iterable value. The returned value is lazy.
@@ -220,6 +224,10 @@ Methods for `List`
220224

221225
Return the content of the iterable object with each element associated to its index in a tuple: [(<index>, <elem>), ...]
222226

227+
.. method:: List.flat_map(this, function)
228+
229+
Given an iterable and a function that takes one argument and return another iterable value, return a new iterable, result of the function application on all elements, flatten in a sole iterable value. The returned iterable value is lazy.
230+
223231
.. method:: List.flatten(this)
224232

225233
Given an iterable of iterables, flatten all of them in a resulting iterable value. The returned value is lazy.
@@ -543,6 +551,10 @@ Methods for `SelectorList`
543551

544552
Return the content of the iterable object with each element associated to its index in a tuple: [(<index>, <elem>), ...]
545553

554+
.. method:: SelectorList.flat_map(this, function)
555+
556+
Given an iterable and a function that takes one argument and return another iterable value, return a new iterable, result of the function application on all elements, flatten in a sole iterable value. The returned iterable value is lazy.
557+
546558
.. method:: SelectorList.flatten(this)
547559

548560
Given an iterable of iterables, flatten all of them in a resulting iterable value. The returned value is lazy.

0 commit comments

Comments
 (0)