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

Show parent comments

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. :)

1

u/RJPisscat Jun 01 '21 edited Jun 01 '21

I forgot, I have used ByRef in an extension:

''' <summary>
''' Swap two Integer values.
''' </summary>
''' <param name="i1">Value that will become i2.</param>
''' <param name="i2">Value that will become i1.</param>
''' <returns></returns>
<Extension> Public Function Swap(ByRef i1 As Integer, ByRef i2 As Integer) As Integer
    Dim iswap As Integer = i1
    i1 = i2
    i2 = iswap
    Return i1
End Function

You're doing a swap, and so is my extension. A difference is mine can be called either

A.Swap(B)

or

Integer.Swap(A, B)

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.

1

u/revennest Jun 01 '21 edited Jun 01 '21

It's not swapping, it like i++ in C, it use i then i += 1 later.

B = A.then(B) Mod B

  1. A

  2. A = B

  3. B = (1.) Mod B

It equal to this.

Dim Tmp = A
A = B
B = Tmp Mod B

A set value to B while return previous A's value back unlike swap, it is swapping value of A and B directly.

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

A thousands deaths to this bot.