Scan: Difference between revisions

From The K Language Wiki
Content added Content deleted
("Add category")
("More lowercasing")
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
{{primitive|f\y<br>x f\y}}
{{primitive|f\y<br>x f\y}}


'''Scan''', also known as '''cumulative reduce''' or '''scanl''' takes a dyadic function <code>f</code> and reduces an array <code>y</code> using it, while providing an array of intermediate values.
'''scan''', also known as '''cumulative reduce''' or '''scanl''' takes a dyadic function <code>f</code> and reduces an array <code>y</code> using it, while providing an array of intermediate values.


Scan works very similarly to [[fold]].
Scan works very similarly to [[fold]].
Line 30: Line 30:


[[Category:Adverbs]]
[[Category:Adverbs]]
[[Category:Primitives]]

Latest revision as of 04:43, 9 July 2022

Scan
f\y
x f\y

scan, also known as cumulative reduce or scanl takes a dyadic function f and reduces an array y using it, while providing an array of intermediate values.

Scan works very similarly to fold.

 +\1 2 3 4 5
1 3 6 10 15
 ,\("ab";1;`d`a`b)
("ab"
 ("a";"b";1)
 ("a";"b";1;`d;`a;`b))

Specifying a left argument x uses it as an initial value.

 3 +\1 2 3 4
4 6 9 13

When used with functions that take more than 2 arguments, scan can take multiple arguments, of which the first one is always the initial value.

 {x,y,z}\["A";1 2 3;"BCD"]
(("A";1;"B")
 ("A";1;"B";2;"C")
 ("A";1;"B";2;"C";3;"D"))