Skip to content

Instantly share code, notes, and snippets.

@menduz
Last active November 10, 2017 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save menduz/ef370473ecec2f1f2c6a71b1632ee88e to your computer and use it in GitHub Desktop.
Save menduz/ef370473ecec2f1f2c6a71b1632ee88e to your computer and use it in GitHub Desktop.
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
Copy link

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