r/visualbasic • • 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

5 Upvotes

9 comments sorted by

View all comments

2

u/RJPisscat Jun 01 '21

Ignore comments below if this is meant as VB6 and it works there. I 'm using .Net 5.0.

This one is a bit slopppy. It took me a while to realize you mean gcd as greatest common divisor. You may want to use the standard comment: Click on the line with the Function declaration, then select Edit/Intellisense/Insert Comment; or right-click the line with the Function declaration and select Snippet/Insert Comment. It will insert this that you can fill in:

''' <summary>
''' 
''' </summary>
''' <param name="A"></param>
''' <param name="B"></param>
''' <returns></returns>

Make it look similar to this:

''' <summary>
''' Return the greatest common divisor (gcd) of two integers
''' </summary>
''' <param name="A">Dividend</param>
''' <param name="B">Divisor</param>
''' <returns>Greatest common divisor</returns>

Now when you start to type the function as in

            Dim N = gcd(Top, Low)

as soon as you type "gcd(", Intellisense will pop up with information about what the function is, what parameters it wants, and what it returns.

Where you say Int do you mean Integer, Int16, Int32, or Int64? "Int" is not a valid keyword.

This will not compile:

                B = A.then(B) Mod B

It has a syntax error: There is no function "then" defined for Integer. Perhaps you've written it as an extension elsewhere and didn't show us? And to affect the value of A you'd have to pass it ByRef. I've never done that in an extension so I don't know if it will compile.

1

u/revennest Jun 01 '21

I'm sorry, I forgot to put then extension method into example code, it also an example for next post of this series.

Function [then](Of T)(ByRef Input As T, Update As T) As T
    [then] = Input
    Input = Update
End Function
Function gcd(A As Integer, B As Integer) As Integer
    If A = 0 Or B = 0 Then Return 1

    While B <> 0
        B = A.then(B) Mod B
    End While

    Return A
End Function

int is Int64(Long), I'm alias it in project file(.vbproj) and forgot to replace it when use as example.

<Import Include="int = System.Int64" />

gcd I'm also sorry on this one too, it use to has at lease summary on it and I intent to rename it to greatest_common_divisor on this post.

I really sloppy on this post, I will double check it in next post, thanks for the help. :)

0

u/backtickbot Jun 01 '21

Fixed formatting.

Hello, revennest: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

1

u/RJPisscat Jun 01 '21

Ok, that's annoying and incorrect.

backtickopt6