How can I iterate over the list of records to apply a function

650 views
Skip to first unread message

alexmu...@gmail.com

unread,
Dec 26, 2016, 12:36:24 PM12/26/16
to Elm Discuss
Hello,
I am new to elm syntax. I have a list of records with name and age. I want to apply a test function to each record. The function takes one record as input and returns a boolean value. Depending on True or false, I want to take a specific action for each record. How can I implement this in elm? If it was Python, I will just iterate over each item in the list and pass it as a parameter to the function.


Duane Johnson

unread,
Dec 26, 2016, 12:42:54 PM12/26/16
to elm-d...@googlegroups.com
Hi Alex,

Perhaps this helps?

$ elm repl
---- elm-repl 0.17.1 -----------------------------------------------------------
 :help for help, :exit to exit, more at <https://github.com/elm-lang/elm-repl>
--------------------------------------------------------------------------------
> a = [1, 2, 3, 4, 5]
[1,2,3,4,5] : List number

> isOdd n = (n % 2 == 1)
<function:_user$project$Repl$isOdd> : Int -> Bool

> odds = List.filter isOdd a
[1,3,5] : List Int

> doAction n = n + 1
<function:_user$project$Repl$doAction> : number -> number

> List.map doAction odds
[2,4,6] : List Int

Here, I take a list of integers (in your case it would be records) and filter on just the ones I want (in this case, odd numbers). Then I use the `map` function to apply the `doAction` function to each.



On Mon, Dec 26, 2016 at 6:40 AM, <alexmu...@gmail.com> wrote:
Hello,
I am new to elm syntax. I have a list of records with name and age. I want to apply a test function to each record. The function takes one record as input and returns a boolean value. Depending on True or false, I want to take a specific action for each record. How can I implement this in elm? If it was Python, I will just iterate over each item in the list and pass it as a parameter to the function.


--
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 elm-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aaron VonderHaar

unread,
Dec 26, 2016, 12:43:36 PM12/26/16
to elm-d...@googlegroups.com
Hello, the answer depends a bit on what you intend to do with the Bools after you have them...

But if, as you describe, you just want to end up with a List of Bools, then

    List.map (\r -> r.age >= 21) yourRecords

But I expect you may also be interested in one of `List.filter`, `List.all`, or `List.any`.  http://package.elm-lang.org/packages/elm-lang/core/5.0.0/List


On Mon, Dec 26, 2016 at 5:40 AM, <alexmu...@gmail.com> wrote:
Hello,
I am new to elm syntax. I have a list of records with name and age. I want to apply a test function to each record. The function takes one record as input and returns a boolean value. Depending on True or false, I want to take a specific action for each record. How can I implement this in elm? If it was Python, I will just iterate over each item in the list and pass it as a parameter to the function.


Reply all
Reply to author
Forward
0 new messages