Can't agree with idempotent examples. Given number 10 you can call add_one(10) multiple times and each time add_one(10) will be 11. Changing the number to -10 is changing the input so it's valid to get different result. The same goes for abs(abs(10)). Abs is giving a result effectively changing the input. It's not about idempotence.
You could have a functio that uses a global counter (variable) and for each call it increases so calling it multiple times with same inputs it gives different result. That's non-idempotent. Then you can have a function accepting the start counter as a parameter and increasing it by 1. That's idempotent as calling it multiple times with same inputs will result in same values.
Take for example POST and PUT examples. Calling POST multiple times will create multiple different records (different ID for instance) for the exact same body. Calling PUT with ID in the body multiple times will always return same result for that same body
thank you pointing out the mistakes, I do agree that func add_one is not a suitable example for illustration and will correct it soon.
but for the abs() function, i took this example from textbooks. Its is the de-facto example given to understand idempotency. Searching more the internet, i came across this : stackoverflow.com/questions/107741...
After reading both the stack thread and your comment, i do think there are better other better example to explain it
Thanks for the link. It has a nice explanation on a slight difference in mathematics and programming. I didn't think of mathematical approach just assumed most people would head on to network approach where you can do multiple inputs to a program from external side. However it's good to always keep in both things. Really glad you replyed with this.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Can't agree with idempotent examples. Given number 10 you can call add_one(10) multiple times and each time add_one(10) will be 11. Changing the number to -10 is changing the input so it's valid to get different result. The same goes for abs(abs(10)). Abs is giving a result effectively changing the input. It's not about idempotence.
You could have a functio that uses a global counter (variable) and for each call it increases so calling it multiple times with same inputs it gives different result. That's non-idempotent. Then you can have a function accepting the start counter as a parameter and increasing it by 1. That's idempotent as calling it multiple times with same inputs will result in same values.
Take for example POST and PUT examples. Calling POST multiple times will create multiple different records (different ID for instance) for the exact same body. Calling PUT with ID in the body multiple times will always return same result for that same body
thank you pointing out the mistakes, I do agree that func add_one is not a suitable example for illustration and will correct it soon.
but for the abs() function, i took this example from textbooks. Its is the de-facto example given to understand idempotency. Searching more the internet, i came across this : stackoverflow.com/questions/107741...
After reading both the stack thread and your comment, i do think there are better other better example to explain it
thank you again.
Thanks for the link. It has a nice explanation on a slight difference in mathematics and programming. I didn't think of mathematical approach just assumed most people would head on to network approach where you can do multiple inputs to a program from external side. However it's good to always keep in both things. Really glad you replyed with this.