Struggling with a simple need to add an item to an Array #791
-
Hello, I am struggling with a simple need to add an item, normally and conditionally, to an Array.
In Javascript adding item to an array is quite intuitive.
In Ruby also it is quite straightforward
Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hello 👋
Yes, it's a bit difficult switching to functional programming mindset! What helped me at the time was to think about it as transformation instead of mutation. Most values in Mint are immutable, which means that they can't be mutated. In OO, variables are mutated in a functional language it's transformed - a new version of the data is returned, not the old one mutated. // This "transforms" `arr` and returns a new version of it where the new value is added at the end.
// The original `arr` is not modified.
Array.push(arr, myFunc1) You can use shadowing to achieve the same thing as you would in OO: let arr = ["A"]
dbg arr
let arr = Array.push(arr, "B")
dbg arr I hope this helps 🙂 Mint does support mutation using component Main {
state arr : Array(String) = ["A"]
fun componentDidMount {
next { arr: Array.push(arr, "B") }
}
fun render {
<>arr</>
}
} |
Beta Was this translation helpful? Give feedback.
It needs to be the same
let arr =
as before, there is no naked assignment in Mint.else
branch:which is the same as:
else
branch correctly: