Common Patterns: Difference between revisions
Line 23: | Line 23: | ||
'''Description:''' Function to find the differences between members of an array. Comes in various flavors. |
'''Description:''' Function to find the differences between members of an array. Comes in various flavors. |
||
'''Examples''' |
'''Examples:''' |
||
<code>-':1 2 3 4 -> 1 1 1 1 /the builtin eachp prepends an array with zero before evaluation, so the output is the same size</code> |
<code>-':1 2 3 4 -> 1 1 1 1 /the builtin eachp prepends an array with zero before evaluation, so the output is the same size</code> |
||
Line 30: | Line 31: | ||
<code>{y/(1_;-1_)@\:x}[6 7 8;-] -> 1 1 /you can substitute the default eachp with your own for different behaviour, like this shakti example that just evaluates over each pair in x without prepending 0</code> |
<code>{y/(1_;-1_)@\:x}[6 7 8;-] -> 1 1 /you can substitute the default eachp with your own for different behaviour, like this shakti example that just evaluates over each pair in x without prepending 0</code> |
||
<code>/the difference is highlighted here |
<code>-':6 7 8 -> 6 1 1 /the difference is highlighted here</code> |
||
-':6 7 8 -> 6 1 1</code> |
|||
'''Dialects''': all, syntax may vary |
'''Dialects''': all, syntax may vary |
Revision as of 20:01, 28 July 2021
This page is intended as a list of commonly used snippets and functions that can be used in K code.
If you would like to add to this list, modify the template given in Format: Common Pattern and paste it in.
Patterns
Sum
+/
Description: function to sum an array. Can be used with initial value.
Examples
+/1 2 3 4 5 -> 15
+/(1 2;2 3) -> 3 5
Dialects: all
Deltas
-':
Description: Function to find the differences between members of an array. Comes in various flavors.
Examples:
-':1 2 3 4 -> 1 1 1 1 /the builtin eachp prepends an array with zero before evaluation, so the output is the same size
-':5 2 4 8 -> 5 -3 2 4
{y/(1_;-1_)@\:x}[6 7 8;-] -> 1 1 /you can substitute the default eachp with your own for different behaviour, like this shakti example that just evaluates over each pair in x without prepending 0
-':6 7 8 -> 6 1 1 /the difference is highlighted here
Dialects: all, syntax may vary