# How can i acheive the following Elm?

**URL:** <https://discourse.elm-lang.org/t/how-can-i-acheive-the-following-elm/4481>\
**Category:** Learn\
**Created:** [October 14, 2019, 5:30pm UTC](https://discourse.elm-lang.org/t/how-can-i-acheive-the-following-elm/4481 "2019-10-14T17:30:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hari\_Roshan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/hari_roshan/32/2588_2.png) [@Hari\_Roshan](https://discourse.elm-lang.org/u/Hari_Roshan)\
**Post date:** [October 14, 2019, 5:30pm UTC](https://discourse.elm-lang.org/t/how-can-i-acheive-the-following-elm/4481/1 "2019-10-14T17:30:47Z")

</div>

Hello everyone, I’m trying to create a function to generate id. For Example 1, 2, 3, … so on Not UUID. Just simple number.  
In JS we can write a function like following to achieve this

```
function () {
   let count = -1
   return {
         getNext: () => {
                count = count + 1
                return count
         }
      }
}

```

how can we achieve something similar in elm. I tried to look into Random module to see how random keeps track of state but i dont understand anything.  
Can somebody help me?. I have a solution to keep a counter in the state and keep incrementing. Is it the right way to do it. Or is there a better way ?

---

<div class="post-metadata">

**Author:** ![lydell](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lydell/32/178_2.png) [@lydell](https://discourse.elm-lang.org/u/lydell)\
**Post date:** [October 14, 2019, 5:38pm UTC](https://discourse.elm-lang.org/t/how-can-i-acheive-the-following-elm/4481/2 "2019-10-14T17:38:00Z")

</div>

Yes, keeping a counter in the state/model and incrementing it is the way to go!

If you find that you keep forgetting to increment the counter, you could make a module that does clever things that forces you to update the counter while reading from it. But no need to complicate things until you run into bugs!

In general, if you want to update something over time you _have_ to store it in your `Model`. That’s the only way of keeping state.

---

<div class="post-metadata">

**Author:** ![Hari\_Roshan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/hari_roshan/32/2588_2.png) [@Hari\_Roshan](https://discourse.elm-lang.org/u/Hari_Roshan)\
**Post date:** [October 14, 2019, 6:56pm UTC](https://discourse.elm-lang.org/t/how-can-i-acheive-the-following-elm/4481/3 "2019-10-14T18:56:43Z")

</div>

Thanks a lot!! 😀

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [October 24, 2019, 6:56pm UTC](https://discourse.elm-lang.org/t/how-can-i-acheive-the-following-elm/4481/4 "2019-10-24T18:56:43Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
