Can you tell us more about what you're trying to do? You almost certainly
do not actually need an array or a tuple, but the functional solution might
not be familiar to you.

If I'm guessing right, you want the first two matches? You just pattern
match:

case matches of
  (firstMatch : secondMatch : rest ) -> doSomethingWith firstMatch
secondMatch
  _ -> errorCase --whatever to do when there are less than two matches

Elm tuples aren't like Python tuples: you need to know in advance exactly
how many elements the tuple will have. You can't tell at compile time how
many matches a regex will have, so it can't be a tuple.

Array is generally reserved for things where you really *need* O(1) lookup.
There could be cases where this would happen, and at some point somebody
might write an industrial strength regex matcher, but at this point,
everyone using it has deemed List good enough.

Incidentially, you can get the nth element of a list with the function
(take n >> head).

That said, if you're iterating through the whole list by index, you're
doing it wrong. Map or foldl or foldr is what you want in this case.

On Wed, Jul 20, 2016 at 3:50 PM, Duane Johnson <[email protected]>
wrote:

> I have a regex match whose result is guaranteed to have two submatches, if
> any match at all:
>
> ```
> import Regex exposing (HowMany(All), regex, find)
> find All (regex "(\\d+)?([A-Z])") someString
> ```
>
> the result looks like this when someString is "HE2LO":
>
> ```
> [{ match = "H", submatches = [Nothing,Just "H"], index = 0, number = 1 }
> ,{ match = "E", submatches = [Nothing,Just "E"], index = 1, number = 2 }
> ,{ match = "2L", submatches = [Just "2",Just "L"], index = 2, number = 3 }
> ,{ match = "O", submatches = [Nothing,Just "O"], index = 4, number = 4 }
> ]
> ```
>
> Why are the submatches of type List rather than Tuple? At the very least,
> shouldn't it be an array so we can access the submatches by index?
>
> Instead, it seems we need to deconstruct the submatch list in an awkward
> way, something like:
>
> ```
> -- for instance, let's say submatches = [Nothing,Just "H"]
> pair =
>   (submatches |> |> List.head |> Maybe.withDefault Nothing |>
> Maybe.withDefault ""
>   ,submatches |> List.tail |> Maybe.withDefault [Nothing] |> List.head |>
> Maybe.withDefault Nothing |>   Maybe.withDefault ""
>   )
> ```
>
> Is there a better way to get the first and second match out?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Elm Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to