r/visualbasic • u/revennest • Jun 01 '21
Tips & Tricks Art of Code : Pure Function
Pure function is concept from functional paradigm but it very useful when you adapt it to your code, it's about create a function with sufficiently argument(not use other variant then its method arguments), any change of value while process this method is limit to method argument only and always get the same result when give the same argument, it's make pure function very transparency and no side effect on run.
Example
Function add(A As Integer, B As Integer) As Integer
Return A + B
End Function
It look too simple, right ? yet when you adapt to your code it become very hard to keep it pure like an example because OOP is very opposite of pure function, you don't know what's change within an object when you invoke its instance method.
List.Add(5)
It hard to tell what's happen or change within List when you look at OOP object, without source you can only trust it name and document what it does, you never know what will be a trigger to cause an error from it.
Array(0) = 5
This one look fine, right ? but only reason you feel it should be fine is its name suggest it's an array type which you know and trust it going to assist value 5 to position index 0 of array data without do any extra process but if it's not an array type, do you still trust it ? but whatever you trust it or not, you might be have no choice but to deal with it.
So how should we adapt pure function concept ?
- It should be static method.
- It should accept value type argument by value only.
- It should return result of process from its arguments.
- Constant is acceptable.
- You maybe use local variant if you don't too serious about pure function and transparency but it better to avoid to do it.
Most easy way to adapt pure function is overload operator of structure, it's static method with ByVal argument only.
Function [then](Of T)(ByRef Input As T, Update As T) As T
[then] = Input
Input = Update
End Function
Function greatest_common_divisor(A As Long, B As Long) As Long
If A = 0 Or B = 0 Then Return 1
While B <> 0
B = A.then(B) Mod B
End While
Return A
End Function
Public Structure fraction
Public ReadOnly top, low As Long
Sub New(Top As Long, Low As Long)
Dim N = greatest_common_divisor(Top, Low)
Me.top = Top / N
Me.low = Low / N
End Sub
Public Shared Operator *(A As fraction, B As Long) As fraction
Return New fraction(A.top * B, A.low)
End Operator
Public Shared Operator /(A As fraction, B As Long) As fraction
Return New fraction(A.top, A.low * B)
End Operator
Public Shared Operator +(A As fraction, B As Long) As fraction
Return New fraction(A.top + B * A.low, A.low)
End Operator
Public Shared Operator -(A As fraction, B As Long) As fraction
Return New fraction(A.top - B * A.low, A.low)
End Operator
Public Shared Operator *(A As fraction, B As fraction) As fraction
Return New fraction(A.top * B.top, A.low * B.low)
End Operator
Public Shared Operator /(A As fraction, B As fraction) As fraction
Return New fraction(A.top / B.top, A.low / B.low)
End Operator
Public Shared Operator +(A As fraction, B As fraction) As fraction
Return If(A.low = B.low,
New fraction(A.top + B.top, A.low),
New fraction(A.top * B.low + B.top * A.low, A.low * B.low))
End Operator
Public Shared Operator -(A As fraction, B As fraction) As fraction
Return If(A.low = B.low,
New fraction(A.top - B.top, A.low),
New fraction(A.top * B.low - B.top * A.low, A.low * B.low))
End Operator
End Structure
update - 1 : fixed missing [then] extension, rename gcd method to greatest_common_divisor and int to Long
1
u/RJPisscat Jun 01 '21 edited Jun 01 '21
I forgot, I have used ByRef in an extension:
You're doing a swap, and so is my extension. A difference is mine can be called either
or
No big deal, some people prefer one or the other.
Edit: Have you tested this version of gcd? It looks like it always returns B. Maybe I'm not reading it correctly.