How to resolve the algorithm Partial function application step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Partial function application step by step in the Nim programming language

Table of Contents

Problem Statement

Partial function application   is the ability to take a function of many parameters and apply arguments to some of the parameters to create a new function that needs only the application of the remaining arguments to produce the equivalent of applying all arguments to the original function. E.g:

Note that in the partial application of a parameter, (in the above case param1), other parameters are not explicitly mentioned. This is a recurring feature of partial function application.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Partial function application step by step in the Nim programming language

Source code in the nim programming language

import sequtils

type

  Func = proc(n: int): int
  FuncS = proc(f: Func; s: seq[int]): seq[int]

proc fs(f: Func; s: seq[int]): seq[int] = s.map(f)

proc partial(fs: FuncS; f: Func): auto =
  result = proc(s: seq[int]): seq[int] = fs(f, s)

proc f1(n: int): int = 2 * n
proc f2(n: int): int = n * n

when isMainModule:

  const Seqs = @[@[0, 1, 2, 3], @[2, 4, 6, 8]]

  let fsf1 = partial(fs, f1)
  let fsf2 = partial(fs, f2)

  for s in Seqs:
    echo fs(f1, s)    # Normal.
    echo fsf1(s)      # Partial.
    echo fs(f2, s)    # Normal.
    echo fsf2(s)      # Partial.
    echo ""


  

You may also check:How to resolve the algorithm Mandelbrot set step by step in the TXR programming language
You may also check:How to resolve the algorithm Bitcoin/public point to address step by step in the Haskell programming language
You may also check:How to resolve the algorithm Literals/Floating point step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Execute Brain step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Compiler/virtual machine interpreter step by step in the Go programming language