Skip to content

Instantly share code, notes, and snippets.

@menduz

menduz/findIndex.dwl

Last active Nov 10, 2017
Embed
What would you like to do?
FindIndex in DataWeave
fun indexOf<T>(array: Array<T>, elem: T, carry: Number = 0): Number =
array match {
case [] -> -1
case [head ~ tail] ->
if(head == elem)
carry
else
findIndex(tail, elem, carry + 1)
}
var HelloWorld = "Hello World!" splitBy ''
---
HelloWorld findIndex 'H' // 0
HelloWorld findIndex 'o' // 4
HelloWorld findIndex 'aaaaa' // -1
@leansh

This comment has been minimized.

Copy link

@leansh leansh commented Nov 10, 2017

fun indexOf <T>(array: Array<T>, elem: T, carry: Number = 0): Number =
    array match {
        case [] -> -1
        case [head ~ tail] ->
            if(head == elem)
                carry
            else
                indexOf(tail, elem, carry + 1)
    }

var HelloWorld = "Hello World!" splitBy ''

---
[
  HelloWorld indexOf 'H', // 0
  HelloWorld indexOf 'o', // 4
  HelloWorld indexOf 'aaaaa' // -1
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.